Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lpd 34676 init #6067

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
LPD-34676 Commerce Site Initializer addFDSEntries
  • Loading branch information
gianmarcobrunialti committed Sep 23, 2024
commit 9e190c2b16baebc1f3c13590d51009048ca6ad79
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dependencies {
compileOnly project(":apps:commerce:headless:headless-commerce:headless-commerce-admin-catalog-api")
compileOnly project(":apps:commerce:headless:headless-commerce:headless-commerce-admin-channel-api")
compileOnly project(":apps:commerce:headless:headless-commerce:headless-commerce-admin-order-api")
compileOnly project(":apps:object:object-api")
compileOnly project(":apps:object:object-rest-api")
compileOnly project(":apps:portal-configuration:portal-configuration-module-configuration-api")
compileOnly project(":apps:portal-vulcan:portal-vulcan-api")
compileOnly project(":apps:site-initializer:site-initializer-extender:site-initializer-extender-api")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@
import com.liferay.headless.commerce.admin.channel.resource.v1_0.ChannelResource;
import com.liferay.headless.commerce.admin.order.dto.v1_0.OrderType;
import com.liferay.headless.commerce.admin.order.resource.v1_0.OrderTypeResource;
import com.liferay.object.model.ObjectDefinition;
import com.liferay.object.model.ObjectEntry;
import com.liferay.object.service.ObjectDefinitionLocalService;
import com.liferay.object.service.ObjectEntryService;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.configuration.module.configuration.ConfigurationProvider;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
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;
Expand All @@ -68,6 +73,7 @@
import com.liferay.portal.kernel.service.LayoutLocalService;
import com.liferay.portal.kernel.service.ResourcePermissionLocalService;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.UserLocalService;
import com.liferay.portal.kernel.service.permission.ModelPermissionsFactory;
import com.liferay.portal.kernel.settings.FallbackKeysSettingsUtil;
import com.liferay.portal.kernel.settings.GroupServiceSettingsLocator;
Expand All @@ -89,6 +95,8 @@
import com.liferay.site.initializer.extender.CommerceSiteInitializer;
import com.liferay.site.initializer.extender.SiteInitializerUtil;

import java.io.Serializable;

import java.math.BigDecimal;

import java.net.URL;
Expand All @@ -107,6 +115,7 @@

/**
* @author Rafael Praxedes
* @author Gianmarco Brunialti Masera
*/
@Component(service = CommerceSiteInitializer.class)
public class CommerceSiteInitializerImpl implements CommerceSiteInitializer {
Expand Down Expand Up @@ -171,6 +180,51 @@ public void addCPDefinitions(
_addOrUpdateCommerceOrderTypes(serviceContext, servletContext);
}

@Override
public void addFDSEntries(
Bundle bundle, ServiceContext serviceContext,
ServletContext servletContext,
Map<String, String> stringUtilReplaceValues)
throws Exception {

Enumeration<URL> enumeration = bundle.findEntries(
"/site-initializer/data-sets", StringPool.STAR, true);

if (enumeration == null) {
return;
}

while (enumeration.hasMoreElements()) {
URL url = enumeration.nextElement();

String fileName = url.getFile();

if (fileName.endsWith("/")) {
continue;
}

if (StringUtil.endsWith(fileName, "data-set.json")) {
String json = URLUtil.toString(url);

if (json == null) {
continue;
}

try {
JSONObject fdsEntryJSONObject =
_jsonFactory.createJSONObject(json);

_addFDSEntry(
fdsEntryJSONObject, serviceContext,
stringUtilReplaceValues);
}
catch (Exception exception) {
_log.error(exception);
}
}
}
}

@Override
public void addPortletSettings(
ClassLoader classLoader, ServiceContext serviceContext,
Expand Down Expand Up @@ -622,6 +676,216 @@ private void _addDefaultCPDisplayLayout(
modifiableSettings.store();
}

private void _addFDSEntry(
JSONObject jsonObject, ServiceContext serviceContext,
Map<String, String> stringUtilReplaceValues)
throws PortalException {

ObjectDefinition fdsEntryObjectDefinition =
_objectDefinitionLocalService.
getObjectDefinitionByExternalReferenceCode(
"FDSEntry", serviceContext.getCompanyId());

JSONObject fdsEntryJSONObject = jsonObject.getJSONObject("dataSet");

String externalReferenceCode = fdsEntryJSONObject.getString(
"externalReferenceCode");
String fdsEntryLabel = fdsEntryJSONObject.getString("label");

ObjectEntry fdsObjectEntry = _objectEntryService.addObjectEntry(
0, fdsEntryObjectDefinition.getObjectDefinitionId(),
HashMapBuilder.<String, Serializable>put(
"externalReferenceCode", externalReferenceCode
).put(
"label", fdsEntryLabel
).put(
"label_i18n",
HashMapBuilder.<String, Serializable>put(
serviceContext.getLanguageId(), fdsEntryLabel
).build()
).put(
"name", fdsEntryLabel
).put(
"restApplication",
fdsEntryJSONObject.getString("restApplication")
).put(
"restEndpoint", fdsEntryJSONObject.getString("restEndpoint")
).put(
"restSchema", fdsEntryJSONObject.getString("restSchema")
).build(),
serviceContext);

JSONArray fdsViewsJSONArray = jsonObject.getJSONArray("views");

for (int i = 0; i < fdsViewsJSONArray.length(); i++) {
ObjectEntry fdsViewObjectEntry = _addFDSView(
fdsViewsJSONArray.getJSONObject(i),
fdsObjectEntry.getObjectEntryId(), serviceContext);

String fdsViewObjectEntryERC =
fdsViewObjectEntry.getExternalReferenceCode();

stringUtilReplaceValues.put(
StringBundler.concat("FDS_VIEW_ERC:", fdsViewObjectEntryERC),
String.valueOf(fdsViewObjectEntry.getObjectEntryId()));
}
}

private void _addFDSItemActions(
JSONObject fdsViewJSONObject, long fdsViewObjectEntryId,
ServiceContext serviceContext)
throws PortalException {

ObjectDefinition fdsViewItemActionObjectDefinition =
_objectDefinitionLocalService.
getObjectDefinitionByExternalReferenceCode(
"FDSAction", serviceContext.getCompanyId());

JSONArray fdsItemActionsJSONArray = fdsViewJSONObject.getJSONArray(
"actions");

if (fdsItemActionsJSONArray == null) {
return;
}

for (int i = 0; i < fdsItemActionsJSONArray.length(); i++) {
JSONObject fdsItemActionJSONObject =
fdsItemActionsJSONArray.getJSONObject(i);

_objectEntryService.addObjectEntry(
0, fdsViewItemActionObjectDefinition.getObjectDefinitionId(),
HashMapBuilder.<String, Serializable>put(
"confirmationMessage_i18n",
(Serializable)_jsonFactory.looseDeserialize(
fdsItemActionJSONObject.getString(
"confirmationMessage_i18n"))
).put(
"icon", fdsItemActionJSONObject.getString("icon")
).put(
"label_i18n",
(Serializable)_jsonFactory.looseDeserialize(
fdsItemActionJSONObject.getString("label_i18n"))
).put(
"method", fdsItemActionJSONObject.getString("method")
).put(
"modalSize", fdsItemActionJSONObject.getString("modalSize")
).put(
"permissionKey",
fdsItemActionJSONObject.getString("permissionKey")
).put(
"r_fdsViewFDSItemActionRelationship_c_fdsViewId",
String.valueOf(fdsViewObjectEntryId)
).put(
"title_i18n",
(Serializable)_jsonFactory.looseDeserialize(
fdsItemActionJSONObject.getString("title_i18n"))
).put(
"type", fdsItemActionJSONObject.getString("type")
).put(
"url", fdsItemActionJSONObject.getString("url")
).build(),
serviceContext);
}
}

private void _addFDSTableFields(
JSONArray fdsFieldsJSONArray, long objectEntryId,
ServiceContext serviceContext)
throws PortalException {

ObjectDefinition fdsFieldObjectDefinition =
_objectDefinitionLocalService.
getObjectDefinitionByExternalReferenceCode(
"FDSField", serviceContext.getCompanyId());

for (int i = 0; i < fdsFieldsJSONArray.length(); i++) {
JSONObject fdsFieldJSONObject = fdsFieldsJSONArray.getJSONObject(i);

_objectEntryService.addObjectEntry(
0, fdsFieldObjectDefinition.getObjectDefinitionId(),
HashMapBuilder.<String, Serializable>put(
"label",
fdsFieldJSONObject.getString(
"label", fdsFieldJSONObject.getString("name"))
).put(
"label_i18n",
(Serializable)_jsonFactory.looseDeserialize(
fdsFieldJSONObject.getString("label_i18n"))
).put(
"name", fdsFieldJSONObject.getString("name")
).put(
"r_fdsViewFDSFieldRelationship_c_fdsViewId", objectEntryId
).put(
"renderer",
fdsFieldJSONObject.getString("renderer", "default")
).put(
"sortable", fdsFieldJSONObject.getBoolean("sortable", true)
).put(
"type", fdsFieldJSONObject.getString("type")
).build(),
serviceContext);
}
}

private ObjectEntry _addFDSView(
JSONObject fdsViewJSONObject, long fdsEntryObjectEntryId,
ServiceContext serviceContext)
throws PortalException {

ObjectDefinition fdsViewObjectDefinition =
_objectDefinitionLocalService.
getObjectDefinitionByExternalReferenceCode(
"FDSView", serviceContext.getCompanyId());

ObjectEntry fdsViewObjectEntry = _objectEntryService.addObjectEntry(
0, fdsViewObjectDefinition.getObjectDefinitionId(),
HashMapBuilder.<String, Serializable>put(
"defaultItemsPerPage",
fdsViewJSONObject.getInt("defaultItemsPerPage", 20)
).put(
"description", fdsViewJSONObject.getString("description")
).put(
"externalReferenceCode",
fdsViewJSONObject.getString("externalReferenceCode")
).put(
"label", fdsViewJSONObject.getString("label")
).put(
"label_i18n",
HashMapBuilder.<String, Serializable>put(
serviceContext.getLanguageId(),
fdsViewJSONObject.getString("label")
).build()
).put(
"listOfItemsPerPage",
fdsViewJSONObject.getString(
"listOfItemsPerPage", "4, 8, 20, 40, 60")
).put(
"r_fdsEntryFDSViewRelationship_c_fdsEntryId",
fdsEntryObjectEntryId
).put(
"symbol", fdsViewJSONObject.getString("symbol", "catalog")
).build(),
serviceContext);

JSONObject fdsVisualizationModesJSONObject =
fdsViewJSONObject.getJSONObject("visualizationModes");

if (fdsVisualizationModesJSONObject.has("table")) {
JSONArray fdsFieldsJSONArray =
fdsVisualizationModesJSONObject.getJSONArray("table");

_addFDSTableFields(
fdsFieldsJSONArray, fdsViewObjectEntry.getObjectEntryId(),
serviceContext);
}

_addFDSItemActions(
fdsViewJSONObject, fdsViewObjectEntry.getObjectEntryId(),
serviceContext);

return fdsViewObjectEntry;
}

private void _addModelResourcePermissions(
String className, String primKey, String resourcePath,
ServiceContext serviceContext, ServletContext servletContext)
Expand Down Expand Up @@ -1082,6 +1346,12 @@ else if (StringUtil.equals(propertyType, "UPDATE_PRICE")) {
@Reference
private LayoutLocalService _layoutLocalService;

@Reference
private ObjectDefinitionLocalService _objectDefinitionLocalService;

@Reference
private ObjectEntryService _objectEntryService;

@Reference
private OrderTypeResource.Factory _orderTypeResourceFactory;

Expand All @@ -1098,4 +1368,7 @@ else if (StringUtil.equals(propertyType, "UPDATE_PRICE")) {
@Reference
private ResourcePermissionLocalService _resourcePermissionLocalService;

@Reference
private UserLocalService _userLocalService;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* @author Nilton Vieira
* @author Gianmarco Brunialti Masera
*/
@ProviderType
public interface CommerceSiteInitializer {
Expand Down
Loading