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.PortletDataContext;
020 import com.liferay.portal.kernel.lar.StagedModelPathUtil;
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 import java.util.Map;
033
034
038 public class DDMStructureStagedModelDataHandler
039 extends BaseStagedModelDataHandler<DDMStructure> {
040
041 @Override
042 public String getClassName() {
043 return DDMStructure.class.getName();
044 }
045
046 @Override
047 protected void doExportStagedModel(
048 PortletDataContext portletDataContext, Element[] elements,
049 DDMStructure structure)
050 throws Exception {
051
052 Element structuresElement = elements[0];
053
054 Element structureElement = structuresElement.addElement("structure");
055
056 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
057 structure.getCompanyId());
058
059 if (defaultUserId == structure.getUserId()) {
060 structureElement.addAttribute("preloaded", "true");
061 }
062
063 portletDataContext.addClassedModel(
064 structureElement, StagedModelPathUtil.getPath(structure), structure,
065 DDMPortletDataHandler.NAMESPACE);
066 }
067
068 @Override
069 protected void doImportStagedModel(
070 PortletDataContext portletDataContext, Element element, String path,
071 DDMStructure structure)
072 throws Exception {
073
074 prepareLanguagesForImport(structure);
075
076 long userId = portletDataContext.getUserId(structure.getUserUuid());
077
078 Map<Long, Long> structureIds =
079 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
080 DDMStructure.class);
081
082 ServiceContext serviceContext = portletDataContext.createServiceContext(
083 path, structure, DDMPortletDataHandler.NAMESPACE);
084
085 DDMStructure importedStructure = null;
086
087 if (portletDataContext.isDataStrategyMirror()) {
088 boolean preloaded = GetterUtil.getBoolean(
089 element.attributeValue("preloaded"));
090
091 DDMStructure existingStructure = null;
092
093 if (!preloaded) {
094 existingStructure = DDMStructureUtil.fetchByUUID_G(
095 structure.getUuid(), portletDataContext.getScopeGroupId());
096 }
097 else {
098 existingStructure = DDMStructureUtil.fetchByG_C_S(
099 portletDataContext.getScopeGroupId(),
100 structure.getClassNameId(), structure.getStructureKey());
101 }
102
103 if (existingStructure == null) {
104 serviceContext.setUuid(structure.getUuid());
105
106 importedStructure = DDMStructureLocalServiceUtil.addStructure(
107 userId, portletDataContext.getScopeGroupId(),
108 structure.getParentStructureId(),
109 structure.getClassNameId(), structure.getStructureKey(),
110 structure.getNameMap(), structure.getDescriptionMap(),
111 structure.getXsd(), structure.getStorageType(),
112 structure.getType(), serviceContext);
113 }
114 else {
115 importedStructure =
116 DDMStructureLocalServiceUtil.updateStructure(
117 existingStructure.getStructureId(),
118 structure.getParentStructureId(),
119 structure.getNameMap(), structure.getDescriptionMap(),
120 structure.getXsd(), serviceContext);
121 }
122 }
123 else {
124 importedStructure = DDMStructureLocalServiceUtil.addStructure(
125 userId, portletDataContext.getScopeGroupId(),
126 structure.getParentStructureId(), structure.getClassNameId(),
127 structure.getStructureKey(), structure.getNameMap(),
128 structure.getDescriptionMap(), structure.getXsd(),
129 structure.getStorageType(), structure.getType(),
130 serviceContext);
131 }
132
133 portletDataContext.importClassedModel(
134 structure, importedStructure, DDMPortletDataHandler.NAMESPACE);
135
136 structureIds.put(
137 structure.getStructureId(), importedStructure.getStructureId());
138 }
139
140 protected void prepareLanguagesForImport(DDMStructure structure)
141 throws PortalException {
142
143 Locale defaultLocale = LocaleUtil.fromLanguageId(
144 structure.getDefaultLanguageId());
145
146 Locale[] availableLocales = LocaleUtil.fromLanguageIds(
147 structure.getAvailableLanguageIds());
148
149 Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
150 DDMStructure.class.getName(), structure.getPrimaryKey(),
151 defaultLocale, availableLocales);
152
153 structure.prepareLocalizedFieldsForImport(defaultImportLocale);
154 }
155
156 }