001
014
015 package com.liferay.portlet.dynamicdatamapping.lar;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
021 import com.liferay.portal.kernel.lar.PortletDataContext;
022 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.LocaleUtil;
025 import com.liferay.portal.kernel.util.LocalizationUtil;
026 import com.liferay.portal.kernel.util.MapUtil;
027 import com.liferay.portal.kernel.xml.Element;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portal.service.UserLocalServiceUtil;
030 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
031 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
032 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
033
034 import java.util.Locale;
035 import java.util.Map;
036
037
041 public class DDMStructureStagedModelDataHandler
042 extends BaseStagedModelDataHandler<DDMStructure> {
043
044 public static final String[] CLASS_NAMES = {DDMStructure.class.getName()};
045
046 @Override
047 public void deleteStagedModel(
048 String uuid, long groupId, String className, String extraData)
049 throws PortalException, SystemException {
050
051 DDMStructure ddmStructure =
052 DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
053 uuid, groupId);
054
055 if (ddmStructure != null) {
056 DDMStructureLocalServiceUtil.deleteStructure(ddmStructure);
057 }
058 }
059
060 @Override
061 public String[] getClassNames() {
062 return CLASS_NAMES;
063 }
064
065 @Override
066 public String getDisplayName(DDMStructure structure) {
067 return structure.getNameCurrentValue();
068 }
069
070 @Override
071 protected void doExportStagedModel(
072 PortletDataContext portletDataContext, DDMStructure structure)
073 throws Exception {
074
075 Element structureElement = portletDataContext.getExportDataElement(
076 structure);
077
078 if (structure.getParentStructureId() !=
079 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {
080
081 DDMStructure parentStructure =
082 DDMStructureLocalServiceUtil.getStructure(
083 structure.getParentStructureId());
084
085 StagedModelDataHandlerUtil.exportStagedModel(
086 portletDataContext, parentStructure);
087
088 portletDataContext.addReferenceElement(
089 structure, structureElement, parentStructure,
090 PortletDataContext.REFERENCE_TYPE_PARENT, false);
091 }
092
093 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
094 structure.getCompanyId());
095
096 if (defaultUserId == structure.getUserId()) {
097 structureElement.addAttribute("preloaded", "true");
098 }
099
100 portletDataContext.addClassedModel(
101 structureElement, ExportImportPathUtil.getModelPath(structure),
102 structure, DDMPortletDataHandler.NAMESPACE);
103 }
104
105 @Override
106 protected void doImportStagedModel(
107 PortletDataContext portletDataContext, DDMStructure structure)
108 throws Exception {
109
110 prepareLanguagesForImport(structure);
111
112 long userId = portletDataContext.getUserId(structure.getUserUuid());
113
114 if (structure.getParentStructureId() !=
115 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {
116
117 Element structureElement =
118 portletDataContext.getReferenceDataElement(
119 structure, DDMStructure.class,
120 structure.getParentStructureId());
121
122 StagedModelDataHandlerUtil.importStagedModel(
123 portletDataContext, structureElement);
124 }
125
126 Map<Long, Long> structureIds =
127 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
128 DDMStructure.class);
129
130 long parentStructureId = MapUtil.getLong(
131 structureIds, structure.getParentStructureId(),
132 structure.getParentStructureId());
133
134 ServiceContext serviceContext = portletDataContext.createServiceContext(
135 structure, DDMPortletDataHandler.NAMESPACE);
136
137 DDMStructure importedStructure = null;
138
139 if (portletDataContext.isDataStrategyMirror()) {
140 Element element =
141 portletDataContext.getImportDataStagedModelElement(structure);
142
143 boolean preloaded = GetterUtil.getBoolean(
144 element.attributeValue("preloaded"));
145
146 DDMStructure existingStructure = null;
147
148 if (!preloaded) {
149 existingStructure =
150 DDMStructureLocalServiceUtil.
151 fetchDDMStructureByUuidAndGroupId(
152 structure.getUuid(),
153 portletDataContext.getScopeGroupId());
154 }
155 else {
156 existingStructure = DDMStructureLocalServiceUtil.fetchStructure(
157 portletDataContext.getScopeGroupId(),
158 structure.getClassNameId(), structure.getStructureKey());
159 }
160
161 if (existingStructure == null) {
162 existingStructure =
163 DDMStructureLocalServiceUtil.
164 fetchDDMStructureByUuidAndGroupId(
165 structure.getUuid(),
166 portletDataContext.getCompanyGroupId());
167 }
168
169 if (existingStructure == null) {
170 serviceContext.setUuid(structure.getUuid());
171
172 importedStructure = DDMStructureLocalServiceUtil.addStructure(
173 userId, portletDataContext.getScopeGroupId(),
174 parentStructureId, structure.getClassNameId(),
175 structure.getStructureKey(), structure.getNameMap(),
176 structure.getDescriptionMap(), structure.getXsd(),
177 structure.getStorageType(), structure.getType(),
178 serviceContext);
179 }
180 else if (portletDataContext.isCompanyStagedGroupedModel(
181 existingStructure)) {
182
183 return;
184 }
185 else {
186 importedStructure =
187 DDMStructureLocalServiceUtil.updateStructure(
188 existingStructure.getStructureId(), parentStructureId,
189 structure.getNameMap(), structure.getDescriptionMap(),
190 structure.getXsd(), serviceContext);
191 }
192 }
193 else {
194 importedStructure = DDMStructureLocalServiceUtil.addStructure(
195 userId, portletDataContext.getScopeGroupId(), parentStructureId,
196 structure.getClassNameId(), structure.getStructureKey(),
197 structure.getNameMap(), structure.getDescriptionMap(),
198 structure.getXsd(), structure.getStorageType(),
199 structure.getType(), serviceContext);
200 }
201
202 portletDataContext.importClassedModel(
203 structure, importedStructure, DDMPortletDataHandler.NAMESPACE);
204
205 structureIds.put(
206 structure.getStructureId(), importedStructure.getStructureId());
207 }
208
209 protected void prepareLanguagesForImport(DDMStructure structure)
210 throws PortalException {
211
212 Locale defaultLocale = LocaleUtil.fromLanguageId(
213 structure.getDefaultLanguageId());
214
215 Locale[] availableLocales = LocaleUtil.fromLanguageIds(
216 structure.getAvailableLanguageIds());
217
218 Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
219 DDMStructure.class.getName(), structure.getPrimaryKey(),
220 defaultLocale, availableLocales);
221
222 structure.prepareLocalizedFieldsForImport(defaultImportLocale);
223 }
224
225 }