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 void doImportCompanyStagedModel(
062 PortletDataContext portletDataContext, DDMStructure structure)
063 throws Exception {
064
065 DDMStructure existingStructure =
066 DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
067 structure.getUuid(), portletDataContext.getCompanyGroupId());
068
069 Map<Long, Long> structureIds =
070 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
071 DDMStructure.class);
072
073 structureIds.put(
074 structure.getStructureId(), existingStructure.getStructureId());
075
076 Map<String, String> structureKeys =
077 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
078 DDMStructure.class + ".ddmStructureKey");
079
080 structureKeys.put(
081 structure.getStructureKey(), existingStructure.getStructureKey());
082 }
083
084 @Override
085 public String[] getClassNames() {
086 return CLASS_NAMES;
087 }
088
089 @Override
090 public String getDisplayName(DDMStructure structure) {
091 return structure.getNameCurrentValue();
092 }
093
094 @Override
095 protected void doExportStagedModel(
096 PortletDataContext portletDataContext, DDMStructure structure)
097 throws Exception {
098
099 Element structureElement = portletDataContext.getExportDataElement(
100 structure);
101
102 if (structure.getParentStructureId() !=
103 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {
104
105 DDMStructure parentStructure =
106 DDMStructureLocalServiceUtil.getStructure(
107 structure.getParentStructureId());
108
109 StagedModelDataHandlerUtil.exportReferenceStagedModel(
110 portletDataContext, structure, parentStructure,
111 PortletDataContext.REFERENCE_TYPE_PARENT);
112 }
113
114 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
115 structure.getCompanyId());
116
117 if (defaultUserId == structure.getUserId()) {
118 structureElement.addAttribute("preloaded", "true");
119 }
120
121 portletDataContext.addClassedModel(
122 structureElement, ExportImportPathUtil.getModelPath(structure),
123 structure);
124 }
125
126 @Override
127 protected void doImportStagedModel(
128 PortletDataContext portletDataContext, DDMStructure structure)
129 throws Exception {
130
131 prepareLanguagesForImport(structure);
132
133 long userId = portletDataContext.getUserId(structure.getUserUuid());
134
135 if (structure.getParentStructureId() !=
136 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {
137
138 Element structureElement =
139 portletDataContext.getReferenceDataElement(
140 structure, DDMStructure.class,
141 structure.getParentStructureId());
142
143 StagedModelDataHandlerUtil.importStagedModel(
144 portletDataContext, structureElement);
145 }
146
147 Map<Long, Long> structureIds =
148 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
149 DDMStructure.class);
150
151 long parentStructureId = MapUtil.getLong(
152 structureIds, structure.getParentStructureId(),
153 structure.getParentStructureId());
154
155 Map<String, String> structureKeys =
156 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
157 DDMStructure.class + ".ddmStructureKey");
158
159 ServiceContext serviceContext = portletDataContext.createServiceContext(
160 structure);
161
162 DDMStructure importedStructure = null;
163
164 if (portletDataContext.isDataStrategyMirror()) {
165 Element element =
166 portletDataContext.getImportDataStagedModelElement(structure);
167
168 boolean preloaded = GetterUtil.getBoolean(
169 element.attributeValue("preloaded"));
170
171 DDMStructure existingStructure = null;
172
173 if (!preloaded) {
174 existingStructure =
175 DDMStructureLocalServiceUtil.
176 fetchDDMStructureByUuidAndGroupId(
177 structure.getUuid(),
178 portletDataContext.getScopeGroupId());
179 }
180 else {
181 existingStructure = DDMStructureLocalServiceUtil.fetchStructure(
182 portletDataContext.getScopeGroupId(),
183 structure.getClassNameId(), structure.getStructureKey());
184 }
185
186 if (existingStructure == null) {
187 serviceContext.setUuid(structure.getUuid());
188
189 importedStructure = DDMStructureLocalServiceUtil.addStructure(
190 userId, portletDataContext.getScopeGroupId(),
191 parentStructureId, structure.getClassNameId(),
192 structure.getStructureKey(), structure.getNameMap(),
193 structure.getDescriptionMap(), structure.getXsd(),
194 structure.getStorageType(), structure.getType(),
195 serviceContext);
196 }
197 else {
198 importedStructure =
199 DDMStructureLocalServiceUtil.updateStructure(
200 existingStructure.getStructureId(), parentStructureId,
201 structure.getNameMap(), structure.getDescriptionMap(),
202 structure.getXsd(), serviceContext);
203 }
204 }
205 else {
206 importedStructure = DDMStructureLocalServiceUtil.addStructure(
207 userId, portletDataContext.getScopeGroupId(), parentStructureId,
208 structure.getClassNameId(), structure.getStructureKey(),
209 structure.getNameMap(), structure.getDescriptionMap(),
210 structure.getXsd(), structure.getStorageType(),
211 structure.getType(), serviceContext);
212 }
213
214 portletDataContext.importClassedModel(structure, importedStructure);
215
216 structureKeys.put(
217 structure.getStructureKey(), importedStructure.getStructureKey());
218 }
219
220 protected void prepareLanguagesForImport(DDMStructure structure)
221 throws PortalException {
222
223 Locale defaultLocale = LocaleUtil.fromLanguageId(
224 structure.getDefaultLanguageId());
225
226 Locale[] availableLocales = LocaleUtil.fromLanguageIds(
227 structure.getAvailableLanguageIds());
228
229 Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
230 DDMStructure.class.getName(), structure.getPrimaryKey(),
231 defaultLocale, availableLocales);
232
233 structure.prepareLocalizedFieldsForImport(defaultImportLocale);
234 }
235
236 @Override
237 protected boolean validateMissingReference(
238 String uuid, long companyId, long groupId)
239 throws Exception {
240
241 DDMStructure structure =
242 DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
243 uuid, groupId);
244
245 if (structure == null) {
246 return false;
247 }
248
249 return true;
250 }
251
252 }