001
014
015 package com.liferay.portlet.dynamicdatamapping.service.impl;
016
017 import com.liferay.portal.LocaleException;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.language.LanguageUtil;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.search.Indexer;
025 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
026 import com.liferay.portal.kernel.systemevent.SystemEvent;
027 import com.liferay.portal.kernel.util.ArrayUtil;
028 import com.liferay.portal.kernel.util.CharPool;
029 import com.liferay.portal.kernel.util.GetterUtil;
030 import com.liferay.portal.kernel.util.GroupThreadLocal;
031 import com.liferay.portal.kernel.util.HtmlUtil;
032 import com.liferay.portal.kernel.util.LocaleUtil;
033 import com.liferay.portal.kernel.util.OrderByComparator;
034 import com.liferay.portal.kernel.util.StringPool;
035 import com.liferay.portal.kernel.util.Validator;
036 import com.liferay.portal.kernel.xml.Document;
037 import com.liferay.portal.kernel.xml.DocumentException;
038 import com.liferay.portal.kernel.xml.Element;
039 import com.liferay.portal.kernel.xml.Node;
040 import com.liferay.portal.kernel.xml.SAXReaderUtil;
041 import com.liferay.portal.kernel.xml.XPath;
042 import com.liferay.portal.model.Group;
043 import com.liferay.portal.model.ResourceConstants;
044 import com.liferay.portal.model.SystemEventConstants;
045 import com.liferay.portal.model.User;
046 import com.liferay.portal.security.auth.CompanyThreadLocal;
047 import com.liferay.portal.service.ServiceContext;
048 import com.liferay.portal.util.PortalUtil;
049 import com.liferay.portal.util.PropsValues;
050 import com.liferay.portlet.dynamicdatamapping.NoSuchStructureException;
051 import com.liferay.portlet.dynamicdatamapping.RequiredStructureException;
052 import com.liferay.portlet.dynamicdatamapping.StructureDuplicateElementException;
053 import com.liferay.portlet.dynamicdatamapping.StructureDuplicateStructureKeyException;
054 import com.liferay.portlet.dynamicdatamapping.StructureNameException;
055 import com.liferay.portlet.dynamicdatamapping.StructureXsdException;
056 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
057 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
058 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
059 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
060 import com.liferay.portlet.dynamicdatamapping.service.base.DDMStructureLocalServiceBaseImpl;
061 import com.liferay.portlet.dynamicdatamapping.util.DDMTemplateHelperUtil;
062 import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
063
064 import java.util.ArrayList;
065 import java.util.Date;
066 import java.util.HashSet;
067 import java.util.List;
068 import java.util.Locale;
069 import java.util.Map;
070 import java.util.Set;
071
072
097 public class DDMStructureLocalServiceImpl
098 extends DDMStructureLocalServiceBaseImpl {
099
100
129 @Override
130 public DDMStructure addStructure(
131 long userId, long groupId, long parentStructureId, long classNameId,
132 String structureKey, Map<Locale, String> nameMap,
133 Map<Locale, String> descriptionMap, String xsd, String storageType,
134 int type, ServiceContext serviceContext)
135 throws PortalException, SystemException {
136
137
138
139 User user = userPersistence.findByPrimaryKey(userId);
140
141 if (Validator.isNull(structureKey)) {
142 structureKey = String.valueOf(counterLocalService.increment());
143 }
144 else {
145 structureKey = structureKey.trim().toUpperCase();
146 }
147
148 try {
149 xsd = DDMXMLUtil.validateXML(xsd);
150 xsd = DDMXMLUtil.formatXML(xsd);
151 }
152 catch (Exception e) {
153 throw new StructureXsdException();
154 }
155
156 Date now = new Date();
157
158 validate(groupId, classNameId, structureKey, nameMap, xsd);
159
160 long structureId = counterLocalService.increment();
161
162 DDMStructure structure = ddmStructurePersistence.create(structureId);
163
164 structure.setUuid(serviceContext.getUuid());
165 structure.setGroupId(groupId);
166 structure.setCompanyId(user.getCompanyId());
167 structure.setUserId(user.getUserId());
168 structure.setUserName(user.getFullName());
169 structure.setCreateDate(serviceContext.getCreateDate(now));
170 structure.setModifiedDate(serviceContext.getModifiedDate(now));
171 structure.setParentStructureId(parentStructureId);
172 structure.setClassNameId(classNameId);
173 structure.setStructureKey(structureKey);
174 structure.setNameMap(nameMap);
175 structure.setDescriptionMap(descriptionMap);
176 structure.setXsd(xsd);
177 structure.setStorageType(storageType);
178 structure.setType(type);
179
180 ddmStructurePersistence.update(structure);
181
182
183
184 if (serviceContext.isAddGroupPermissions() ||
185 serviceContext.isAddGuestPermissions()) {
186
187 addStructureResources(
188 structure, serviceContext.isAddGroupPermissions(),
189 serviceContext.isAddGuestPermissions());
190 }
191 else {
192 addStructureResources(
193 structure, serviceContext.getGroupPermissions(),
194 serviceContext.getGuestPermissions());
195 }
196
197 return structure;
198 }
199
200
221 @Override
222 public DDMStructure addStructure(
223 long userId, long groupId, long classNameId,
224 Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
225 String xsd, ServiceContext serviceContext)
226 throws PortalException, SystemException {
227
228 return addStructure(
229 userId, groupId, DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
230 classNameId, null, nameMap, descriptionMap, xsd,
231 PropsValues.DYNAMIC_DATA_LISTS_STORAGE_TYPE,
232 DDMStructureConstants.TYPE_DEFAULT, serviceContext);
233 }
234
235
264 @Override
265 public DDMStructure addStructure(
266 long userId, long groupId, String parentStructureKey,
267 long classNameId, String structureKey, Map<Locale, String> nameMap,
268 Map<Locale, String> descriptionMap, String xsd, String storageType,
269 int type, ServiceContext serviceContext)
270 throws PortalException, SystemException {
271
272 DDMStructure parentStructure = fetchStructure(
273 groupId, classNameId, parentStructureKey);
274
275 long parentStructureId =
276 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID;
277
278 if (parentStructure != null) {
279 parentStructureId = parentStructure.getStructureId();
280 }
281
282 return addStructure(
283 userId, groupId, parentStructureId, classNameId, structureKey,
284 nameMap, descriptionMap, xsd, storageType, type, serviceContext);
285 }
286
287
296 @Override
297 public void addStructureResources(
298 DDMStructure structure, boolean addGroupPermissions,
299 boolean addGuestPermissions)
300 throws PortalException, SystemException {
301
302 resourceLocalService.addResources(
303 structure.getCompanyId(), structure.getGroupId(),
304 structure.getUserId(), DDMStructure.class.getName(),
305 structure.getStructureId(), false, addGroupPermissions,
306 addGuestPermissions);
307 }
308
309
318 @Override
319 public void addStructureResources(
320 DDMStructure structure, String[] groupPermissions,
321 String[] guestPermissions)
322 throws PortalException, SystemException {
323
324 resourceLocalService.addModelResources(
325 structure.getCompanyId(), structure.getGroupId(),
326 structure.getUserId(), DDMStructure.class.getName(),
327 structure.getStructureId(), groupPermissions, guestPermissions);
328 }
329
330
347 @Override
348 public DDMStructure copyStructure(
349 long userId, long structureId, Map<Locale, String> nameMap,
350 Map<Locale, String> descriptionMap, ServiceContext serviceContext)
351 throws PortalException, SystemException {
352
353 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
354 structureId);
355
356 return addStructure(
357 userId, structure.getGroupId(), structure.getParentStructureId(),
358 structure.getClassNameId(), null, nameMap, descriptionMap,
359 structure.getXsd(), structure.getStorageType(), structure.getType(),
360 serviceContext);
361 }
362
363 @Override
364 public DDMStructure copyStructure(
365 long userId, long structureId, ServiceContext serviceContext)
366 throws PortalException, SystemException {
367
368 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
369 structureId);
370
371 return addStructure(
372 userId, structure.getGroupId(), structure.getParentStructureId(),
373 structure.getClassNameId(), null, structure.getNameMap(),
374 structure.getDescriptionMap(), structure.getXsd(),
375 structure.getStorageType(), structure.getType(), serviceContext);
376 }
377
378
390 @Override
391 @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
392 public void deleteStructure(DDMStructure structure)
393 throws PortalException, SystemException {
394
395 if (!GroupThreadLocal.isDeleteInProcess()) {
396 if (ddmStructureLinkPersistence.countByStructureId(
397 structure.getStructureId()) > 0) {
398
399 throw new RequiredStructureException(
400 RequiredStructureException.REFERENCED_STRUCTURE_LINK);
401 }
402
403 if (ddmStructurePersistence.countByParentStructureId(
404 structure.getStructureId()) > 0) {
405
406 throw new RequiredStructureException(
407 RequiredStructureException.REFERENCED_STRUCTURE);
408 }
409
410 long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
411
412 if (ddmTemplatePersistence.countByG_C_C(
413 structure.getGroupId(), classNameId,
414 structure.getPrimaryKey()) > 0) {
415
416 throw new RequiredStructureException(
417 RequiredStructureException.REFERENCED_TEMPLATE);
418 }
419 }
420
421
422
423 ddmStructurePersistence.remove(structure);
424
425
426
427 resourceLocalService.deleteResource(
428 structure.getCompanyId(), DDMStructure.class.getName(),
429 ResourceConstants.SCOPE_INDIVIDUAL, structure.getStructureId());
430 }
431
432
444 @Override
445 public void deleteStructure(long structureId)
446 throws PortalException, SystemException {
447
448 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
449 structureId);
450
451 ddmStructureLocalService.deleteStructure(structure);
452 }
453
454
469 @Override
470 public void deleteStructure(
471 long groupId, long classNameId, String structureKey)
472 throws PortalException, SystemException {
473
474 structureKey = getStructureKey(structureKey);
475
476 DDMStructure structure = ddmStructurePersistence.findByG_C_S(
477 groupId, classNameId, structureKey);
478
479 ddmStructureLocalService.deleteStructure(structure);
480 }
481
482
495 @Override
496 public void deleteStructures(long groupId)
497 throws PortalException, SystemException {
498
499 List<DDMStructure> structures = ddmStructurePersistence.findByGroupId(
500 groupId);
501
502 for (DDMStructure structure : structures) {
503 ddmStructureLocalService.deleteStructure(structure);
504 }
505 }
506
507
515 @Override
516 public DDMStructure fetchStructure(long structureId)
517 throws SystemException {
518
519 return ddmStructurePersistence.fetchByPrimaryKey(structureId);
520 }
521
522
534 @Override
535 public DDMStructure fetchStructure(
536 long groupId, long classNameId, String structureKey)
537 throws SystemException {
538
539 structureKey = getStructureKey(structureKey);
540
541 return ddmStructurePersistence.fetchByG_C_S(
542 groupId, classNameId, structureKey);
543 }
544
545
566 @Override
567 public DDMStructure fetchStructure(
568 long groupId, long classNameId, String structureKey,
569 boolean includeGlobalStructures)
570 throws PortalException, SystemException {
571
572 structureKey = getStructureKey(structureKey);
573
574 DDMStructure structure = ddmStructurePersistence.fetchByG_C_S(
575 groupId, classNameId, structureKey);
576
577 if ((structure != null) || !includeGlobalStructures) {
578 return structure;
579 }
580
581 Group group = groupPersistence.findByPrimaryKey(groupId);
582
583 Group companyGroup = groupLocalService.getCompanyGroup(
584 group.getCompanyId());
585
586 return ddmStructurePersistence.fetchByG_C_S(
587 companyGroup.getGroupId(), classNameId, structureKey);
588 }
589
590
594 @Override
595 public List<DDMStructure> getClassStructures(long classNameId)
596 throws SystemException {
597
598 return ddmStructurePersistence.findByClassNameId(classNameId);
599 }
600
601
605 @Override
606 public List<DDMStructure> getClassStructures(
607 long classNameId, int start, int end)
608 throws SystemException {
609
610 return ddmStructurePersistence.findByClassNameId(
611 classNameId, start, end);
612 }
613
614
623 @Override
624 public List<DDMStructure> getClassStructures(
625 long companyId, long classNameId)
626 throws SystemException {
627
628 return ddmStructurePersistence.findByC_C(companyId, classNameId);
629 }
630
631
653 @Override
654 public List<DDMStructure> getClassStructures(
655 long companyId, long classNameId, int start, int end)
656 throws SystemException {
657
658 return ddmStructurePersistence.findByC_C(
659 companyId, classNameId, start, end);
660 }
661
662
674 @Override
675 public List<DDMStructure> getClassStructures(
676 long companyId, long classNameId,
677 OrderByComparator orderByComparator)
678 throws SystemException {
679
680 return ddmStructurePersistence.findByC_C(
681 companyId, classNameId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
682 orderByComparator);
683 }
684
685
689 @Override
690 public List<DDMStructure> getClassStructures(
691 long classNameId, OrderByComparator orderByComparator)
692 throws SystemException {
693
694 return ddmStructurePersistence.findByClassNameId(
695 classNameId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
696 orderByComparator);
697 }
698
699
707 @Override
708 public List<DDMStructure> getDLFileEntryTypeStructures(
709 long dlFileEntryTypeId)
710 throws SystemException {
711
712 return dlFileEntryTypePersistence.getDDMStructures(dlFileEntryTypeId);
713 }
714
715
723 @Override
724 public DDMStructure getStructure(long structureId)
725 throws PortalException, SystemException {
726
727 return ddmStructurePersistence.findByPrimaryKey(structureId);
728 }
729
730
742 @Override
743 public DDMStructure getStructure(
744 long groupId, long classNameId, String structureKey)
745 throws PortalException, SystemException {
746
747 structureKey = getStructureKey(structureKey);
748
749 return ddmStructurePersistence.findByG_C_S(
750 groupId, classNameId, structureKey);
751 }
752
753
773 @Override
774 public DDMStructure getStructure(
775 long groupId, long classNameId, String structureKey,
776 boolean includeGlobalStructures)
777 throws PortalException, SystemException {
778
779 structureKey = getStructureKey(structureKey);
780
781 DDMStructure structure = ddmStructurePersistence.fetchByG_C_S(
782 groupId, classNameId, structureKey);
783
784 if (structure != null) {
785 return structure;
786 }
787
788 if (!includeGlobalStructures) {
789 throw new NoSuchStructureException(
790 "No DDMStructure exists with the structure key " +
791 structureKey);
792 }
793
794 Group group = groupPersistence.findByPrimaryKey(groupId);
795
796 Group companyGroup = groupLocalService.getCompanyGroup(
797 group.getCompanyId());
798
799 return ddmStructurePersistence.findByG_C_S(
800 companyGroup.getGroupId(), classNameId, structureKey);
801 }
802
803
812 @Override
813 public List<DDMStructure> getStructure(
814 long groupId, String name, String description)
815 throws SystemException {
816
817 return ddmStructurePersistence.findByG_N_D(groupId, name, description);
818 }
819
820
823 @Override
824 public List<DDMStructure> getStructureEntries() throws SystemException {
825 return getStructures();
826 }
827
828
831 @Override
832 public List<DDMStructure> getStructureEntries(long groupId)
833 throws SystemException {
834
835 return getStructures(groupId);
836 }
837
838
842 @Override
843 public List<DDMStructure> getStructureEntries(
844 long groupId, int start, int end)
845 throws SystemException {
846
847 return getStructures(groupId, start, end);
848 }
849
850
856 @Override
857 public List<DDMStructure> getStructures() throws SystemException {
858 return ddmStructurePersistence.findAll();
859 }
860
861
868 @Override
869 public List<DDMStructure> getStructures(long groupId)
870 throws SystemException {
871
872 return ddmStructurePersistence.findByGroupId(groupId);
873 }
874
875
896 @Override
897 public List<DDMStructure> getStructures(long groupId, int start, int end)
898 throws SystemException {
899
900 return ddmStructurePersistence.findByGroupId(groupId, start, end);
901 }
902
903
912 @Override
913 public List<DDMStructure> getStructures(long groupId, long classNameId)
914 throws SystemException {
915
916 return ddmStructurePersistence.findByG_C(groupId, classNameId);
917 }
918
919
943 @Override
944 public List<DDMStructure> getStructures(
945 long groupId, long classNameId, int start, int end)
946 throws SystemException {
947
948 return ddmStructurePersistence.findByG_C(
949 groupId, classNameId, start, end);
950 }
951
952
977 @Override
978 public List<DDMStructure> getStructures(
979 long groupId, long classNameId, int start, int end,
980 OrderByComparator orderByComparator)
981 throws SystemException {
982
983 return ddmStructurePersistence.findByG_C(
984 groupId, classNameId, start, end, orderByComparator);
985 }
986
987 @Override
988 public List<DDMStructure> getStructures(
989 long groupId, String name, String description)
990 throws SystemException {
991
992 return ddmStructurePersistence.findByG_N_D(groupId, name, description);
993 }
994
995
1002 @Override
1003 public List<DDMStructure> getStructures(long[] groupIds)
1004 throws SystemException {
1005
1006 return ddmStructurePersistence.findByGroupId(groupIds);
1007 }
1008
1009
1020 @Override
1021 public List<DDMStructure> getStructures(long[] groupIds, long classNameId)
1022 throws SystemException {
1023
1024 return ddmStructurePersistence.findByG_C(groupIds, classNameId);
1025 }
1026
1027 @Override
1028 public List<DDMStructure> getStructures(
1029 long[] groupIds, long classNameId, int start, int end)
1030 throws SystemException {
1031
1032 return ddmStructurePersistence.findByG_C(
1033 groupIds, classNameId, start, end);
1034 }
1035
1036
1043 @Override
1044 public int getStructuresCount(long groupId) throws SystemException {
1045 return ddmStructurePersistence.countByGroupId(groupId);
1046 }
1047
1048
1057 @Override
1058 public int getStructuresCount(long groupId, long classNameId)
1059 throws SystemException {
1060
1061 return ddmStructurePersistence.countByG_C(groupId, classNameId);
1062 }
1063
1064 @Override
1065 public int getStructuresCount(long[] groupIds, long classNameId)
1066 throws SystemException {
1067
1068 return ddmStructurePersistence.countByG_C(groupIds, classNameId);
1069 }
1070
1071
1100 @Override
1101 public List<DDMStructure> search(
1102 long companyId, long[] groupIds, long[] classNameIds,
1103 String keywords, int start, int end,
1104 OrderByComparator orderByComparator)
1105 throws SystemException {
1106
1107 return ddmStructureFinder.findByKeywords(
1108 companyId, groupIds, classNameIds, keywords, start, end,
1109 orderByComparator);
1110 }
1111
1112
1147 @Override
1148 public List<DDMStructure> search(
1149 long companyId, long[] groupIds, long[] classNameIds, String name,
1150 String description, String storageType, int type,
1151 boolean andOperator, int start, int end,
1152 OrderByComparator orderByComparator)
1153 throws SystemException {
1154
1155 return ddmStructureFinder.findByC_G_C_N_D_S_T(
1156 companyId, groupIds, classNameIds, name, description, storageType,
1157 type, andOperator, start, end, orderByComparator);
1158 }
1159
1160
1173 @Override
1174 public int searchCount(
1175 long companyId, long[] groupIds, long[] classNameIds,
1176 String keywords)
1177 throws SystemException {
1178
1179 return ddmStructureFinder.countByKeywords(
1180 companyId, groupIds, classNameIds, keywords);
1181 }
1182
1183
1203 @Override
1204 public int searchCount(
1205 long companyId, long[] groupIds, long[] classNameIds, String name,
1206 String description, String storageType, int type,
1207 boolean andOperator)
1208 throws SystemException {
1209
1210 return ddmStructureFinder.countByC_G_C_N_D_S_T(
1211 companyId, groupIds, classNameIds, name, description, storageType,
1212 type, andOperator);
1213 }
1214
1215
1236 @Override
1237 public DDMStructure updateStructure(
1238 long groupId, long parentStructureId, long classNameId,
1239 String structureKey, Map<Locale, String> nameMap,
1240 Map<Locale, String> descriptionMap, String xsd,
1241 ServiceContext serviceContext)
1242 throws PortalException, SystemException {
1243
1244 structureKey = getStructureKey(structureKey);
1245
1246 DDMStructure structure = ddmStructurePersistence.findByG_C_S(
1247 groupId, classNameId, structureKey);
1248
1249 return doUpdateStructure(
1250 parentStructureId, nameMap, descriptionMap, xsd, serviceContext,
1251 structure);
1252 }
1253
1254
1271 @Override
1272 public DDMStructure updateStructure(
1273 long structureId, long parentStructureId,
1274 Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
1275 String xsd, ServiceContext serviceContext)
1276 throws PortalException, SystemException {
1277
1278 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
1279 structureId);
1280
1281 return doUpdateStructure(
1282 parentStructureId, nameMap, descriptionMap, xsd, serviceContext,
1283 structure);
1284 }
1285
1286
1299 @Override
1300 public DDMStructure updateXSD(
1301 long structureId, String xsd, ServiceContext serviceContext)
1302 throws PortalException, SystemException {
1303
1304 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
1305 structureId);
1306
1307 return doUpdateStructure(
1308 structure.getParentStructureId(), structure.getNameMap(),
1309 structure.getDescriptionMap(), xsd, serviceContext, structure);
1310 }
1311
1312
1326 @Override
1327 public void updateXSDFieldMetadata(
1328 long structureId, String fieldName, String metadataEntryName,
1329 String metadataEntryValue, ServiceContext serviceContext)
1330 throws PortalException, SystemException {
1331
1332 DDMStructure ddmStructure = fetchDDMStructure(structureId);
1333
1334 if (ddmStructure == null) {
1335 return;
1336 }
1337
1338 String xsd = ddmStructure.getXsd();
1339
1340 try {
1341 Document document = SAXReaderUtil.read(xsd);
1342
1343 Element rootElement = document.getRootElement();
1344
1345 List<Element> dynamicElementElements = rootElement.elements(
1346 "dynamic-element");
1347
1348 for (Element dynamicElementElement : dynamicElementElements) {
1349 String dynamicElementElementFieldName = GetterUtil.getString(
1350 dynamicElementElement.attributeValue("name"));
1351
1352 if (!dynamicElementElementFieldName.equals(fieldName)) {
1353 continue;
1354 }
1355
1356 List<Element> metadataElements = dynamicElementElement.elements(
1357 "meta-data");
1358
1359 for (Element metadataElement : metadataElements) {
1360 List<Element> metadataEntryElements =
1361 metadataElement.elements();
1362
1363 for (Element metadataEntryElement : metadataEntryElements) {
1364 String metadataEntryElementName = GetterUtil.getString(
1365 metadataEntryElement.attributeValue("name"));
1366
1367 if (metadataEntryElementName.equals(
1368 metadataEntryName)) {
1369
1370 metadataEntryElement.setText(metadataEntryValue);
1371 }
1372 }
1373 }
1374 }
1375
1376 updateXSD(structureId, document.asXML(), serviceContext);
1377 }
1378 catch (DocumentException de) {
1379 throw new SystemException(de);
1380 }
1381 }
1382
1383 protected void appendNewStructureRequiredFields(
1384 DDMStructure structure, Document templateDocument) {
1385
1386 String xsd = structure.getXsd();
1387
1388 Document structureDocument = null;
1389
1390 try {
1391 structureDocument = SAXReaderUtil.read(xsd);
1392 }
1393 catch (DocumentException de) {
1394 if (_log.isWarnEnabled()) {
1395 _log.warn(de, de);
1396 }
1397
1398 return;
1399 }
1400
1401 Element templateElement = templateDocument.getRootElement();
1402
1403 XPath structureXPath = SAXReaderUtil.createXPath(
1404 "
1405 "\"true\"]");
1406
1407 List<Node> nodes = structureXPath.selectNodes(structureDocument);
1408
1409 for (Node node : nodes) {
1410 Element element = (Element)node;
1411
1412 String name = element.attributeValue("name");
1413
1414 name = HtmlUtil.escapeXPathAttribute(name);
1415
1416 XPath templateXPath = SAXReaderUtil.createXPath(
1417 "
1418
1419 if (!templateXPath.booleanValueOf(templateDocument)) {
1420 templateElement.add(element.createCopy());
1421 }
1422 }
1423 }
1424
1425 protected DDMStructure doUpdateStructure(
1426 long parentStructureId, Map<Locale, String> nameMap,
1427 Map<Locale, String> descriptionMap, String xsd,
1428 ServiceContext serviceContext, DDMStructure structure)
1429 throws PortalException, SystemException {
1430
1431 try {
1432 xsd = DDMXMLUtil.validateXML(xsd);
1433 xsd = DDMXMLUtil.formatXML(xsd);
1434 }
1435 catch (Exception e) {
1436 throw new StructureXsdException();
1437 }
1438
1439 validate(nameMap, xsd);
1440
1441 structure.setModifiedDate(serviceContext.getModifiedDate(null));
1442 structure.setParentStructureId(parentStructureId);
1443 structure.setNameMap(nameMap);
1444 structure.setDescriptionMap(descriptionMap);
1445 structure.setXsd(xsd);
1446
1447 ddmStructurePersistence.update(structure);
1448
1449 syncStructureTemplatesFields(structure);
1450
1451 Indexer indexer = IndexerRegistryUtil.getIndexer(
1452 structure.getClassName());
1453
1454 if (indexer != null) {
1455 List<Long> ddmStructureIds = getChildrenStructureIds(
1456 structure.getGroupId(), structure.getStructureId());
1457
1458 indexer.reindexDDMStructures(ddmStructureIds);
1459 }
1460
1461 return structure;
1462 }
1463
1464 protected void getChildrenStructureIds(
1465 List<Long> structureIds, long groupId, long structureId)
1466 throws PortalException, SystemException {
1467
1468 List<DDMStructure> structures = ddmStructurePersistence.findByG_P(
1469 groupId, structureId);
1470
1471 for (DDMStructure structure : structures) {
1472 structureIds.add(structure.getStructureId());
1473
1474 getChildrenStructureIds(
1475 structureIds, structure.getGroupId(),
1476 structure.getParentStructureId());
1477 }
1478 }
1479
1480 protected List<Long> getChildrenStructureIds(long groupId, long structureId)
1481 throws PortalException, SystemException {
1482
1483 List<Long> structureIds = new ArrayList<Long>();
1484
1485 getChildrenStructureIds(structureIds, groupId, structureId);
1486
1487 structureIds.add(0, structureId);
1488
1489 return structureIds;
1490 }
1491
1492 protected String getStructureKey(String structureKey) {
1493 if (structureKey != null) {
1494 structureKey = structureKey.trim();
1495
1496 return structureKey.toUpperCase();
1497 }
1498
1499 return StringPool.BLANK;
1500 }
1501
1502 protected void syncStructureTemplatesFields(DDMStructure structure)
1503 throws PortalException, SystemException {
1504
1505 long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
1506
1507 List<DDMTemplate> templates = ddmTemplateLocalService.getTemplates(
1508 structure.getGroupId(), classNameId, structure.getStructureId(),
1509 DDMTemplateConstants.TEMPLATE_TYPE_FORM);
1510
1511 for (DDMTemplate template : templates) {
1512 String script = template.getScript();
1513
1514 Document templateDocument = null;
1515
1516 try {
1517 templateDocument = SAXReaderUtil.read(script);
1518 }
1519 catch (DocumentException de) {
1520 if (_log.isWarnEnabled()) {
1521 _log.warn(de, de);
1522 }
1523
1524 continue;
1525 }
1526
1527 Element templateRootElement = templateDocument.getRootElement();
1528
1529 syncStructureTemplatesFields(template, templateRootElement);
1530
1531 appendNewStructureRequiredFields(structure, templateDocument);
1532
1533 try {
1534 script = DDMXMLUtil.formatXML(templateDocument.asXML());
1535 }
1536 catch (Exception e) {
1537 throw new StructureXsdException();
1538 }
1539
1540 template.setScript(script);
1541
1542 ddmTemplatePersistence.update(template);
1543 }
1544 }
1545
1546 protected void syncStructureTemplatesFields(
1547 DDMTemplate template, Element templateElement)
1548 throws PortalException, SystemException {
1549
1550 DDMStructure structure = DDMTemplateHelperUtil.fetchStructure(template);
1551
1552 List<Element> dynamicElementElements = templateElement.elements(
1553 "dynamic-element");
1554
1555 for (Element dynamicElementElement : dynamicElementElements) {
1556 String dataType = dynamicElementElement.attributeValue("dataType");
1557 String fieldName = dynamicElementElement.attributeValue("name");
1558
1559 if (Validator.isNull(dataType)) {
1560 continue;
1561 }
1562
1563 if (!structure.hasField(fieldName)) {
1564 templateElement.remove(dynamicElementElement);
1565
1566 continue;
1567 }
1568
1569 String mode = template.getMode();
1570
1571 if (mode.equals(DDMTemplateConstants.TEMPLATE_MODE_CREATE)) {
1572 boolean fieldRequired = structure.getFieldRequired(fieldName);
1573
1574 List<Element> metadataElements = dynamicElementElement.elements(
1575 "meta-data");
1576
1577 for (Element metadataElement : metadataElements) {
1578 for (Element metadataEntryElement :
1579 metadataElement.elements()) {
1580
1581 String attributeName =
1582 metadataEntryElement.attributeValue("name");
1583
1584 if (fieldRequired && attributeName.equals("required")) {
1585 metadataEntryElement.setText("true");
1586 }
1587 }
1588 }
1589 }
1590
1591 syncStructureTemplatesFields(template, dynamicElementElement);
1592 }
1593 }
1594
1595 protected void validate(List<Element> elements, Set<String> names)
1596 throws PortalException {
1597
1598 for (Element element : elements) {
1599 String elementName = element.getName();
1600
1601 if (elementName.equals("meta-data")) {
1602 continue;
1603 }
1604
1605 String name = element.attributeValue("name", StringPool.BLANK);
1606 String type = element.attributeValue("type", StringPool.BLANK);
1607
1608 if (Validator.isNull(name) ||
1609 name.startsWith(DDMStructureConstants.XSD_NAME_RESERVED)) {
1610
1611 throw new StructureXsdException();
1612 }
1613
1614 char[] charArray = name.toCharArray();
1615
1616 for (int i = 0; i < charArray.length; i++) {
1617 if (!Character.isLetterOrDigit(charArray[i]) &&
1618 (charArray[i] != CharPool.DASH) &&
1619 (charArray[i] != CharPool.UNDERLINE)) {
1620
1621 throw new StructureXsdException();
1622 }
1623 }
1624
1625 String path = name;
1626
1627 Element parentElement = element.getParent();
1628
1629 while (!parentElement.isRootElement()) {
1630 path =
1631 parentElement.attributeValue("name", StringPool.BLANK) +
1632 StringPool.SLASH + path;
1633
1634 parentElement = parentElement.getParent();
1635 }
1636
1637 path = path.toLowerCase();
1638
1639 if (names.contains(path)) {
1640 throw new StructureDuplicateElementException();
1641 }
1642 else {
1643 names.add(path);
1644 }
1645
1646 if (Validator.isNull(type)) {
1647 throw new StructureXsdException();
1648 }
1649
1650 validate(element.elements(), names);
1651 }
1652 }
1653
1654 protected void validate(
1655 long groupId, long classNameId, String structureKey,
1656 Map<Locale, String> nameMap, String xsd)
1657 throws PortalException, SystemException {
1658
1659 structureKey = getStructureKey(structureKey);
1660
1661 DDMStructure structure = ddmStructurePersistence.fetchByG_C_S(
1662 groupId, classNameId, structureKey);
1663
1664 if (structure != null) {
1665 StructureDuplicateStructureKeyException sdske =
1666 new StructureDuplicateStructureKeyException();
1667
1668 sdske.setStructureKey(structure.getStructureKey());
1669
1670 throw sdske;
1671 }
1672
1673 validate(nameMap, xsd);
1674 }
1675
1676 protected void validate(Map<Locale, String> nameMap, String xsd)
1677 throws PortalException {
1678
1679 if (Validator.isNull(xsd)) {
1680 throw new StructureXsdException();
1681 }
1682 else {
1683 try {
1684 List<Element> elements = new ArrayList<Element>();
1685
1686 Document document = SAXReaderUtil.read(xsd);
1687
1688 Element rootElement = document.getRootElement();
1689
1690 List<Element> rootElementElements = rootElement.elements();
1691
1692 if (rootElementElements.isEmpty()) {
1693 throw new StructureXsdException();
1694 }
1695
1696 Locale contentDefaultLocale = LocaleUtil.fromLanguageId(
1697 rootElement.attributeValue("default-locale"));
1698
1699 validateLanguages(nameMap, contentDefaultLocale);
1700
1701 elements.addAll(rootElement.elements());
1702
1703 Set<String> elNames = new HashSet<String>();
1704
1705 validate(elements, elNames);
1706 }
1707 catch (LocaleException le) {
1708 throw le;
1709 }
1710 catch (StructureDuplicateElementException sdee) {
1711 throw sdee;
1712 }
1713 catch (StructureNameException sne) {
1714 throw sne;
1715 }
1716 catch (StructureXsdException sxe) {
1717 throw sxe;
1718 }
1719 catch (Exception e) {
1720 throw new StructureXsdException();
1721 }
1722 }
1723 }
1724
1725 protected void validateLanguages(
1726 Map<Locale, String> nameMap, Locale contentDefaultLocale)
1727 throws PortalException {
1728
1729 String name = nameMap.get(contentDefaultLocale);
1730
1731 if (Validator.isNull(name)) {
1732 throw new StructureNameException();
1733 }
1734
1735 Locale[] availableLocales = LanguageUtil.getAvailableLocales();
1736
1737 if (!ArrayUtil.contains(availableLocales, contentDefaultLocale)) {
1738 Long companyId = CompanyThreadLocal.getCompanyId();
1739
1740 LocaleException le = new LocaleException(
1741 LocaleException.TYPE_CONTENT,
1742 "The locale " + contentDefaultLocale +
1743 " is not available in company " + companyId);
1744
1745 le.setSourceAvailableLocales(new Locale[] {contentDefaultLocale});
1746 le.setTargetAvailableLocales(availableLocales);
1747
1748 throw le;
1749 }
1750 }
1751
1752 private static Log _log = LogFactoryUtil.getLog(
1753 DDMStructureLocalServiceImpl.class);
1754
1755 }