001
014
015 package com.liferay.portlet.documentlibrary.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.ArrayUtil;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.kernel.xml.Element;
027 import com.liferay.portal.model.Group;
028 import com.liferay.portal.service.GroupLocalServiceUtil;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
031 import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
032 import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryTypeUtil;
033 import com.liferay.portlet.documentlibrary.util.DLUtil;
034 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
035 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
036
037 import java.util.Collection;
038 import java.util.List;
039 import java.util.Map;
040
041
045 public class DLFileEntryTypeStagedModelDataHandler
046 extends BaseStagedModelDataHandler<DLFileEntryType> {
047
048 public static final String[] CLASS_NAMES =
049 {DLFileEntryType.class.getName()};
050
051 @Override
052 public String[] getClassNames() {
053 return CLASS_NAMES;
054 }
055
056 @Override
057 protected void doExportStagedModel(
058 PortletDataContext portletDataContext,
059 DLFileEntryType fileEntryType)
060 throws Exception {
061
062 Element fileEntryTypeElement =
063 portletDataContext.getExportDataStagedModelElement(fileEntryType);
064
065 List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
066
067 for (DDMStructure ddmStructure : ddmStructures) {
068 StagedModelDataHandlerUtil.exportStagedModel(
069 portletDataContext, ddmStructure);
070
071 portletDataContext.addReferenceElement(
072 fileEntryTypeElement, ddmStructure);
073 }
074
075 portletDataContext.addClassedModel(
076 fileEntryTypeElement,
077 ExportImportPathUtil.getModelPath(fileEntryType), fileEntryType,
078 DLPortletDataHandler.NAMESPACE);
079 }
080
081 @Override
082 protected void doImportStagedModel(
083 PortletDataContext portletDataContext,
084 DLFileEntryType fileEntryType)
085 throws Exception {
086
087 long userId = portletDataContext.getUserId(fileEntryType.getUserUuid());
088
089 String name = getFileEntryTypeName(
090 fileEntryType.getUuid(), portletDataContext.getScopeGroupId(),
091 fileEntryType.getName(), 2);
092
093 List<Element> ddmStructureElements =
094 portletDataContext.getReferencedDataElements(
095 fileEntryType, DDMStructure.class);
096
097 for (Element ddmStructureElement : ddmStructureElements) {
098 StagedModelDataHandlerUtil.importStagedModel(
099 portletDataContext, ddmStructureElement);
100 }
101
102 Map<Long, Long> ddmStructureIds =
103 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
104 DDMStructure.class);
105
106 Collection<Long> ddmStructureIdsCollection = ddmStructureIds.values();
107
108 long[] ddmStructureIdsArray = ArrayUtil.toArray(
109 ddmStructureIdsCollection.toArray(
110 new Long[ddmStructureIds.size()]));
111
112 ServiceContext serviceContext = portletDataContext.createServiceContext(
113 fileEntryType, DLPortletDataHandler.NAMESPACE);
114
115 DLFileEntryType importedDLFileEntryType = null;
116
117 if (portletDataContext.isDataStrategyMirror()) {
118 DLFileEntryType existingDLFileEntryType =
119 DLFileEntryTypeUtil.fetchByUUID_G(
120 fileEntryType.getUuid(),
121 portletDataContext.getScopeGroupId());
122
123 if (existingDLFileEntryType == null) {
124 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
125 portletDataContext.getCompanyId());
126
127 existingDLFileEntryType = DLFileEntryTypeUtil.fetchByUUID_G(
128 fileEntryType.getUuid(), companyGroup.getGroupId());
129 }
130
131 if (existingDLFileEntryType == null) {
132 serviceContext.setUuid(fileEntryType.getUuid());
133
134 importedDLFileEntryType =
135 DLFileEntryTypeLocalServiceUtil.addFileEntryType(
136 userId, portletDataContext.getScopeGroupId(), name,
137 fileEntryType.getDescription(), ddmStructureIdsArray,
138 serviceContext);
139 }
140 else {
141 if (!isFileEntryTypeGlobal(
142 portletDataContext.getCompanyId(),
143 existingDLFileEntryType)) {
144
145 DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
146 userId, existingDLFileEntryType.getFileEntryTypeId(),
147 name, fileEntryType.getDescription(),
148 ddmStructureIdsArray, serviceContext);
149 }
150
151 importedDLFileEntryType = existingDLFileEntryType;
152 }
153 }
154 else {
155 importedDLFileEntryType =
156 DLFileEntryTypeLocalServiceUtil.addFileEntryType(
157 userId, portletDataContext.getScopeGroupId(), name,
158 fileEntryType.getDescription(), ddmStructureIdsArray,
159 serviceContext);
160 }
161
162 if (!isFileEntryTypeGlobal(
163 portletDataContext.getCompanyId(), importedDLFileEntryType)) {
164
165 portletDataContext.importClassedModel(
166 fileEntryType, importedDLFileEntryType,
167 DLPortletDataHandler.NAMESPACE);
168
169 String importedDLFileEntryDDMStructureKey =
170 DLUtil.getDDMStructureKey(importedDLFileEntryType);
171
172 List<DDMStructure> ddmStructures =
173 importedDLFileEntryType.getDDMStructures();
174
175 for (DDMStructure ddmStructure : ddmStructures) {
176 String ddmStructureKey = ddmStructure.getStructureKey();
177
178 if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(
179 ddmStructureKey)) {
180
181 continue;
182 }
183
184 if (ddmStructureKey.equals(
185 importedDLFileEntryDDMStructureKey)) {
186
187 continue;
188 }
189
190 ddmStructure.setStructureKey(
191 importedDLFileEntryDDMStructureKey);
192
193 DDMStructureLocalServiceUtil.updateDDMStructure(ddmStructure);
194 }
195 }
196 }
197
198
204 protected String getFileEntryTypeName(
205 String uuid, long groupId, String name, int count)
206 throws Exception {
207
208 DLFileEntryType dlFileEntryType = DLFileEntryTypeUtil.fetchByG_N(
209 groupId, name);
210
211 if (dlFileEntryType == null) {
212 return name;
213 }
214
215 if (Validator.isNotNull(uuid) &&
216 uuid.equals(dlFileEntryType.getUuid())) {
217
218 return name;
219 }
220
221 name = StringUtil.appendParentheticalSuffix(name, count);
222
223 return getFileEntryTypeName(uuid, groupId, name, ++count);
224 }
225
226 protected boolean isFileEntryTypeGlobal(
227 long companyId, DLFileEntryType dlFileEntryType)
228 throws PortalException, SystemException {
229
230 Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
231
232 if (dlFileEntryType.getGroupId() == group.getGroupId()) {
233 return true;
234 }
235
236 return false;
237 }
238
239 }