All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.commerce.initializer.util.DDMFormImporter Maven / Gradle / Ivy

The newest version!
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.commerce.initializer.util;

import com.liferay.dynamic.data.lists.model.DDLRecordSet;
import com.liferay.dynamic.data.lists.model.DDLRecordSetConstants;
import com.liferay.dynamic.data.lists.model.DDLRecordSetSettings;
import com.liferay.dynamic.data.lists.service.DDLRecordSetLocalService;
import com.liferay.dynamic.data.mapping.io.DDMFormDeserializer;
import com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeRequest;
import com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeResponse;
import com.liferay.dynamic.data.mapping.io.DDMFormLayoutDeserializer;
import com.liferay.dynamic.data.mapping.io.DDMFormLayoutDeserializerDeserializeRequest;
import com.liferay.dynamic.data.mapping.io.DDMFormLayoutDeserializerDeserializeResponse;
import com.liferay.dynamic.data.mapping.io.DDMFormValuesDeserializer;
import com.liferay.dynamic.data.mapping.io.DDMFormValuesDeserializerDeserializeRequest;
import com.liferay.dynamic.data.mapping.io.DDMFormValuesDeserializerDeserializeResponse;
import com.liferay.dynamic.data.mapping.model.DDMForm;
import com.liferay.dynamic.data.mapping.model.DDMFormInstance;
import com.liferay.dynamic.data.mapping.model.DDMFormInstanceSettings;
import com.liferay.dynamic.data.mapping.model.DDMFormLayout;
import com.liferay.dynamic.data.mapping.model.DDMStructure;
import com.liferay.dynamic.data.mapping.model.DDMStructureConstants;
import com.liferay.dynamic.data.mapping.model.UnlocalizedValue;
import com.liferay.dynamic.data.mapping.service.DDMFormInstanceLocalService;
import com.liferay.dynamic.data.mapping.service.DDMStructureLocalService;
import com.liferay.dynamic.data.mapping.storage.DDMFormFieldValue;
import com.liferay.dynamic.data.mapping.storage.DDMFormValues;
import com.liferay.dynamic.data.mapping.storage.StorageType;
import com.liferay.dynamic.data.mapping.util.DDMFormFactory;
import com.liferay.dynamic.data.mapping.util.DDMFormFactoryHelper;
import com.liferay.dynamic.data.mapping.util.DDMUtil;
import com.liferay.petra.function.transform.TransformUtil;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactory;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.model.Role;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.ClassNameLocalService;
import com.liferay.portal.kernel.service.ResourcePermissionLocalService;
import com.liferay.portal.kernel.service.RoleLocalService;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.UserLocalService;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.StringUtil;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Steven Smith
 */
@Component(service = DDMFormImporter.class)
public class DDMFormImporter {

	public void importDDMForms(
			JSONArray jsonArray, long scopeGroupId, long userId)
		throws Exception {

		User user = _userLocalService.fetchUser(userId);

		ServiceContext serviceContext = new ServiceContext();

		serviceContext.setCompanyId(user.getCompanyId());
		serviceContext.setScopeGroupId(scopeGroupId);
		serviceContext.setUserId(userId);

		for (int i = 0; i < jsonArray.length(); i++) {
			_addDDMFormInstance(
				jsonArray.getJSONObject(i), userId, scopeGroupId,
				serviceContext);
		}
	}

	protected void updatePermissions(
			long companyId, String name, String primKey, JSONArray jsonArray)
		throws PortalException {

		if (jsonArray == null) {
			jsonArray = _jsonFactory.createJSONArray(
				"[{\"actionIds\": [\"VIEW\", \"ADD_FORM_INSTANCE_RECORD\"]," +
					"\"roleName\": \"Site Member\", \"scope\": 4}]");
		}

		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);

			int scope = jsonObject.getInt("scope");

			String roleName = jsonObject.getString("roleName");

			Role role = _roleLocalService.getRole(companyId, roleName);

			String[] actionIds = new String[0];

			JSONArray actionIdsJSONArray = jsonObject.getJSONArray("actionIds");

			if (actionIdsJSONArray != null) {
				for (int j = 0; j < actionIdsJSONArray.length(); j++) {
					actionIds = ArrayUtil.append(
						actionIds, actionIdsJSONArray.getString(j));
				}
			}

			_resourcePermissionLocalService.setResourcePermissions(
				companyId, name, scope, primKey, role.getRoleId(), actionIds);
		}
	}

	private void _addDDMFormInstance(
			JSONObject jsonObject, long userId, long scopeGroupId,
			ServiceContext serviceContext)
		throws Exception {

		// DDM Form

		Locale locale = serviceContext.getLocale();

		Map nameMap = HashMapBuilder.put(
			locale, jsonObject.getString("name")
		).build();

		Map descriptionMap = HashMapBuilder.put(
			locale, jsonObject.getString("description")
		).build();

		String jsonForm = jsonObject.getString("ddmStructure");

		String jsonFormSettings = jsonObject.getString("ddmFormSettings");

		DDMStructure ddmStructure = _createDDMStructure(
			nameMap, descriptionMap, jsonForm, jsonFormSettings,
			serviceContext);

		String jsonFormLayout = jsonObject.getString("ddmStructureLayout");

		_updateDDMStructureLayout(
			ddmStructure, ddmStructure.getDDMForm(), jsonFormLayout,
			serviceContext);

		DDMFormValuesDeserializerDeserializeRequest
			ddmFormValuesDeserializerDeserializeRequest =
				DDMFormValuesDeserializerDeserializeRequest.Builder.newBuilder(
					jsonFormSettings,
					DDMFormFactory.create(DDMFormInstanceSettings.class)
				).build();

		DDMFormValuesDeserializerDeserializeResponse
			ddmFormValuesDeserializerDeserializeResponse =
				_ddmFormValuesDeserializer.deserialize(
					ddmFormValuesDeserializerDeserializeRequest);

		DDMFormValues settingsDDMFormValues =
			ddmFormValuesDeserializerDeserializeResponse.getDDMFormValues();

		DDMFormInstance ddmFormInstance =
			_ddmFormInstanceLocalService.addFormInstance(
				userId, scopeGroupId, ddmStructure.getStructureId(), nameMap,
				descriptionMap, settingsDDMFormValues, serviceContext);

		JSONArray permissionsJSONArray = jsonObject.getJSONArray("permissions");

		if ((permissionsJSONArray != null) &&
			(permissionsJSONArray.length() > 0)) {

			updatePermissions(
				ddmFormInstance.getCompanyId(),
				ddmFormInstance.getModelClassName(),
				String.valueOf(ddmFormInstance.getPrimaryKey()),
				permissionsJSONArray);
		}
		else {

			// Give site members view and add form instance permissions

			updatePermissions(
				ddmFormInstance.getCompanyId(),
				ddmFormInstance.getModelClassName(),
				String.valueOf(ddmFormInstance.getPrimaryKey()), null);
		}
	}

	private List _createDDMFormFieldValues(
			DDMForm ddmForm, String jsonFormSettings)
		throws Exception {

		JSONObject jsonObject = _jsonFactory.createJSONObject(jsonFormSettings);

		return TransformUtil.transform(
			ddmForm.getDDMFormFields(),
			formField -> {
				DDMFormFieldValue ddmFormFieldValue = new DDMFormFieldValue();

				ddmFormFieldValue.setName(formField.getName());

				UnlocalizedValue unlocalizedValue = new UnlocalizedValue(
					jsonObject.getString(formField.getName()));

				ddmFormFieldValue.setValue(unlocalizedValue);

				return ddmFormFieldValue;
			});
	}

	private DDMStructure _createDDMStructure(
			Map nameMap, Map descriptionMap,
			String jsonForm, String jsonFormSettings,
			ServiceContext serviceContext)
		throws Exception {

		jsonForm = StringUtil.replace(
			jsonForm, "[$LOCALE$]",
			LocaleUtil.toLanguageId(serviceContext.getLocale()));

		DDMFormDeserializerDeserializeRequest
			ddmFormDeserializerDeserializeRequest =
				DDMFormDeserializerDeserializeRequest.Builder.newBuilder(
					jsonForm
				).build();

		DDMFormDeserializerDeserializeResponse
			ddmFormDeserializerDeserializeResponse =
				_ddmFormDeserializer.deserialize(
					ddmFormDeserializerDeserializeRequest);

		DDMForm ddmForm = ddmFormDeserializerDeserializeResponse.getDDMForm();

		DDMFormLayout defaultDDMFormLayout = DDMUtil.getDefaultDDMFormLayout(
			ddmForm);

		defaultDDMFormLayout.setPaginationMode(StringPool.BLANK);

		long userId = serviceContext.getUserId();
		long groupId = serviceContext.getScopeGroupId();

		DDMStructure ddmStructure = _ddmStructureLocalService.addStructure(
			null, userId, groupId,
			DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
			_classNameLocalService.getClassNameId(DDLRecordSet.class),
			StringPool.BLANK, nameMap, descriptionMap, ddmForm,
			defaultDDMFormLayout, StorageType.DEFAULT.toString(),
			DDMStructureConstants.TYPE_AUTO, serviceContext);

		DDLRecordSet ddlRecordSet = _ddlRecordSetLocalService.addRecordSet(
			userId, groupId, ddmStructure.getStructureId(), null, nameMap,
			descriptionMap, DDLRecordSetConstants.MIN_DISPLAY_ROWS_DEFAULT,
			DDLRecordSetConstants.SCOPE_FORMS, serviceContext);

		DDMFormFactoryHelper ddmFormFactoryHelper = new DDMFormFactoryHelper(
			DDLRecordSetSettings.class);

		DDMForm settingsDDMForm = ddmFormFactoryHelper.createDDMForm();

		DDMFormValues ddmFormValues = new DDMFormValues(settingsDDMForm);

		ddmFormValues.setDefaultLocale(serviceContext.getLocale());
		ddmFormValues.addAvailableLocale(serviceContext.getLocale());
		ddmFormValues.setDDMFormFieldValues(
			_createDDMFormFieldValues(settingsDDMForm, jsonFormSettings));

		_ddlRecordSetLocalService.updateRecordSet(
			ddlRecordSet.getRecordSetId(), ddmFormValues);

		return ddmStructure;
	}

	private void _updateDDMStructureLayout(
			DDMStructure structure, DDMForm ddmForm, String jsonFormLayout,
			ServiceContext serviceContext)
		throws Exception {

		long userId = serviceContext.getUserId();

		DDMFormLayoutDeserializerDeserializeRequest
			ddmFormLayoutDeserializerDeserializeRequest =
				DDMFormLayoutDeserializerDeserializeRequest.Builder.newBuilder(
					jsonFormLayout
				).build();

		DDMFormLayoutDeserializerDeserializeResponse
			ddmFormLayoutDeserializerDeserializeResponse =
				_ddmFormLayoutDeserializer.deserialize(
					ddmFormLayoutDeserializerDeserializeRequest);

		DDMFormLayout formLayout =
			ddmFormLayoutDeserializerDeserializeResponse.getDDMFormLayout();

		_ddmStructureLocalService.updateStructure(
			userId, structure.getStructureId(), ddmForm, formLayout,
			serviceContext);
	}

	@Reference
	private ClassNameLocalService _classNameLocalService;

	@Reference
	private DDLRecordSetLocalService _ddlRecordSetLocalService;

	@Reference(target = "(ddm.form.deserializer.type=json)")
	private DDMFormDeserializer _ddmFormDeserializer;

	@Reference
	private DDMFormInstanceLocalService _ddmFormInstanceLocalService;

	@Reference
	private DDMFormLayoutDeserializer _ddmFormLayoutDeserializer;

	@Reference
	private DDMFormValuesDeserializer _ddmFormValuesDeserializer;

	@Reference
	private DDMStructureLocalService _ddmStructureLocalService;

	@Reference
	private JSONFactory _jsonFactory;

	@Reference
	private ResourcePermissionLocalService _resourcePermissionLocalService;

	@Reference
	private RoleLocalService _roleLocalService;

	@Reference
	private UserLocalService _userLocalService;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy