001
014
015 package com.liferay.portlet.dynamicdatamapping.lar;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
019 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
020 import com.liferay.portal.kernel.lar.PortletDataContext;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.LocaleUtil;
023 import com.liferay.portal.kernel.util.LocalizationUtil;
024 import com.liferay.portal.kernel.xml.Element;
025 import com.liferay.portal.service.ServiceContext;
026 import com.liferay.portal.service.UserLocalServiceUtil;
027 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
028 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
029 import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMStructureUtil;
030
031 import java.util.Locale;
032
033
037 public class DDMStructureStagedModelDataHandler
038 extends BaseStagedModelDataHandler<DDMStructure> {
039
040 public static final String[] CLASS_NAMES = {DDMStructure.class.getName()};
041
042 @Override
043 public String[] getClassNames() {
044 return CLASS_NAMES;
045 }
046
047 @Override
048 protected void doExportStagedModel(
049 PortletDataContext portletDataContext, DDMStructure structure)
050 throws Exception {
051
052 Element structureElement =
053 portletDataContext.getExportDataStagedModelElement(structure);
054
055 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
056 structure.getCompanyId());
057
058 if (defaultUserId == structure.getUserId()) {
059 structureElement.addAttribute("preloaded", "true");
060 }
061
062 portletDataContext.addClassedModel(
063 structureElement, ExportImportPathUtil.getModelPath(structure),
064 structure, DDMPortletDataHandler.NAMESPACE);
065 }
066
067 @Override
068 protected void doImportStagedModel(
069 PortletDataContext portletDataContext, DDMStructure structure)
070 throws Exception {
071
072 prepareLanguagesForImport(structure);
073
074 long userId = portletDataContext.getUserId(structure.getUserUuid());
075
076 ServiceContext serviceContext = portletDataContext.createServiceContext(
077 structure, DDMPortletDataHandler.NAMESPACE);
078
079 DDMStructure importedStructure = null;
080
081 if (portletDataContext.isDataStrategyMirror()) {
082 Element element =
083 portletDataContext.getImportDataStagedModelElement(structure);
084
085 boolean preloaded = GetterUtil.getBoolean(
086 element.attributeValue("preloaded"));
087
088 DDMStructure existingStructure = null;
089
090 if (!preloaded) {
091 existingStructure = DDMStructureUtil.fetchByUUID_G(
092 structure.getUuid(), portletDataContext.getScopeGroupId());
093 }
094 else {
095 existingStructure = DDMStructureUtil.fetchByG_C_S(
096 portletDataContext.getScopeGroupId(),
097 structure.getClassNameId(), structure.getStructureKey());
098 }
099
100 if (existingStructure == null) {
101 serviceContext.setUuid(structure.getUuid());
102
103 importedStructure = DDMStructureLocalServiceUtil.addStructure(
104 userId, portletDataContext.getScopeGroupId(),
105 structure.getParentStructureId(),
106 structure.getClassNameId(), structure.getStructureKey(),
107 structure.getNameMap(), structure.getDescriptionMap(),
108 structure.getXsd(), structure.getStorageType(),
109 structure.getType(), serviceContext);
110 }
111 else {
112 importedStructure =
113 DDMStructureLocalServiceUtil.updateStructure(
114 existingStructure.getStructureId(),
115 structure.getParentStructureId(),
116 structure.getNameMap(), structure.getDescriptionMap(),
117 structure.getXsd(), serviceContext);
118 }
119 }
120 else {
121 importedStructure = DDMStructureLocalServiceUtil.addStructure(
122 userId, portletDataContext.getScopeGroupId(),
123 structure.getParentStructureId(), structure.getClassNameId(),
124 structure.getStructureKey(), structure.getNameMap(),
125 structure.getDescriptionMap(), structure.getXsd(),
126 structure.getStorageType(), structure.getType(),
127 serviceContext);
128 }
129
130 portletDataContext.importClassedModel(
131 structure, importedStructure, DDMPortletDataHandler.NAMESPACE);
132 }
133
134 protected void prepareLanguagesForImport(DDMStructure structure)
135 throws PortalException {
136
137 Locale defaultLocale = LocaleUtil.fromLanguageId(
138 structure.getDefaultLanguageId());
139
140 Locale[] availableLocales = LocaleUtil.fromLanguageIds(
141 structure.getAvailableLanguageIds());
142
143 Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
144 DDMStructure.class.getName(), structure.getPrimaryKey(),
145 defaultLocale, availableLocales);
146
147 structure.prepareLocalizedFieldsForImport(defaultImportLocale);
148 }
149
150 }