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.GetterUtil;
029 import com.liferay.portal.kernel.util.GroupThreadLocal;
030 import com.liferay.portal.kernel.util.HtmlUtil;
031 import com.liferay.portal.kernel.util.LocaleUtil;
032 import com.liferay.portal.kernel.util.OrderByComparator;
033 import com.liferay.portal.kernel.util.StringPool;
034 import com.liferay.portal.kernel.util.StringUtil;
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 = StringUtil.toUpperCase(structureKey.trim());
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(
159 groupId, parentStructureId, classNameId, structureKey, nameMap,
160 xsd);
161
162 long structureId = counterLocalService.increment();
163
164 DDMStructure structure = ddmStructurePersistence.create(structureId);
165
166 structure.setUuid(serviceContext.getUuid());
167 structure.setGroupId(groupId);
168 structure.setCompanyId(user.getCompanyId());
169 structure.setUserId(user.getUserId());
170 structure.setUserName(user.getFullName());
171 structure.setCreateDate(serviceContext.getCreateDate(now));
172 structure.setModifiedDate(serviceContext.getModifiedDate(now));
173 structure.setParentStructureId(parentStructureId);
174 structure.setClassNameId(classNameId);
175 structure.setStructureKey(structureKey);
176 structure.setNameMap(nameMap);
177 structure.setDescriptionMap(descriptionMap);
178 structure.setXsd(xsd);
179 structure.setStorageType(storageType);
180 structure.setType(type);
181
182 ddmStructurePersistence.update(structure);
183
184
185
186 if (serviceContext.isAddGroupPermissions() ||
187 serviceContext.isAddGuestPermissions()) {
188
189 addStructureResources(
190 structure, serviceContext.isAddGroupPermissions(),
191 serviceContext.isAddGuestPermissions());
192 }
193 else {
194 addStructureResources(
195 structure, serviceContext.getGroupPermissions(),
196 serviceContext.getGuestPermissions());
197 }
198
199 return structure;
200 }
201
202
223 @Override
224 public DDMStructure addStructure(
225 long userId, long groupId, long classNameId,
226 Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
227 String xsd, ServiceContext serviceContext)
228 throws PortalException, SystemException {
229
230 return addStructure(
231 userId, groupId, DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
232 classNameId, null, nameMap, descriptionMap, xsd,
233 PropsValues.DYNAMIC_DATA_LISTS_STORAGE_TYPE,
234 DDMStructureConstants.TYPE_DEFAULT, serviceContext);
235 }
236
237
266 @Override
267 public DDMStructure addStructure(
268 long userId, long groupId, String parentStructureKey,
269 long classNameId, String structureKey, Map<Locale, String> nameMap,
270 Map<Locale, String> descriptionMap, String xsd, String storageType,
271 int type, ServiceContext serviceContext)
272 throws PortalException, SystemException {
273
274 DDMStructure parentStructure = fetchStructure(
275 groupId, classNameId, parentStructureKey);
276
277 long parentStructureId =
278 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID;
279
280 if (parentStructure != null) {
281 parentStructureId = parentStructure.getStructureId();
282 }
283
284 return addStructure(
285 userId, groupId, parentStructureId, classNameId, structureKey,
286 nameMap, descriptionMap, xsd, storageType, type, serviceContext);
287 }
288
289
298 @Override
299 public void addStructureResources(
300 DDMStructure structure, boolean addGroupPermissions,
301 boolean addGuestPermissions)
302 throws PortalException, SystemException {
303
304 resourceLocalService.addResources(
305 structure.getCompanyId(), structure.getGroupId(),
306 structure.getUserId(), DDMStructure.class.getName(),
307 structure.getStructureId(), false, addGroupPermissions,
308 addGuestPermissions);
309 }
310
311
320 @Override
321 public void addStructureResources(
322 DDMStructure structure, String[] groupPermissions,
323 String[] guestPermissions)
324 throws PortalException, SystemException {
325
326 resourceLocalService.addModelResources(
327 structure.getCompanyId(), structure.getGroupId(),
328 structure.getUserId(), DDMStructure.class.getName(),
329 structure.getStructureId(), groupPermissions, guestPermissions);
330 }
331
332
349 @Override
350 public DDMStructure copyStructure(
351 long userId, long structureId, Map<Locale, String> nameMap,
352 Map<Locale, String> descriptionMap, ServiceContext serviceContext)
353 throws PortalException, SystemException {
354
355 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
356 structureId);
357
358 return addStructure(
359 userId, structure.getGroupId(), structure.getParentStructureId(),
360 structure.getClassNameId(), null, nameMap, descriptionMap,
361 structure.getXsd(), structure.getStorageType(), structure.getType(),
362 serviceContext);
363 }
364
365 @Override
366 public DDMStructure copyStructure(
367 long userId, long structureId, ServiceContext serviceContext)
368 throws PortalException, SystemException {
369
370 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
371 structureId);
372
373 return addStructure(
374 userId, structure.getGroupId(), structure.getParentStructureId(),
375 structure.getClassNameId(), null, structure.getNameMap(),
376 structure.getDescriptionMap(), structure.getXsd(),
377 structure.getStorageType(), structure.getType(), serviceContext);
378 }
379
380
392 @Override
393 @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
394 public void deleteStructure(DDMStructure structure)
395 throws PortalException, SystemException {
396
397 if (!GroupThreadLocal.isDeleteInProcess()) {
398 if (ddmStructureLinkPersistence.countByStructureId(
399 structure.getStructureId()) > 0) {
400
401 throw new RequiredStructureException(
402 RequiredStructureException.REFERENCED_STRUCTURE_LINK);
403 }
404
405 if (ddmStructurePersistence.countByParentStructureId(
406 structure.getStructureId()) > 0) {
407
408 throw new RequiredStructureException(
409 RequiredStructureException.REFERENCED_STRUCTURE);
410 }
411
412 long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
413
414 if (ddmTemplatePersistence.countByG_C_C(
415 structure.getGroupId(), classNameId,
416 structure.getPrimaryKey()) > 0) {
417
418 throw new RequiredStructureException(
419 RequiredStructureException.REFERENCED_TEMPLATE);
420 }
421 }
422
423
424
425 ddmStructurePersistence.remove(structure);
426
427
428
429 resourceLocalService.deleteResource(
430 structure.getCompanyId(), DDMStructure.class.getName(),
431 ResourceConstants.SCOPE_INDIVIDUAL, structure.getStructureId());
432 }
433
434
446 @Override
447 public void deleteStructure(long structureId)
448 throws PortalException, SystemException {
449
450 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
451 structureId);
452
453 ddmStructureLocalService.deleteStructure(structure);
454 }
455
456
471 @Override
472 public void deleteStructure(
473 long groupId, long classNameId, String structureKey)
474 throws PortalException, SystemException {
475
476 structureKey = getStructureKey(structureKey);
477
478 DDMStructure structure = ddmStructurePersistence.findByG_C_S(
479 groupId, classNameId, structureKey);
480
481 ddmStructureLocalService.deleteStructure(structure);
482 }
483
484
497 @Override
498 public void deleteStructures(long groupId)
499 throws PortalException, SystemException {
500
501 List<DDMStructure> structures = ddmStructurePersistence.findByGroupId(
502 groupId);
503
504 deleteStructures(structures);
505 }
506
507 @Override
508 public void deleteStructures(long groupId, long classNameId)
509 throws PortalException, SystemException {
510
511 List<DDMStructure> structures = ddmStructurePersistence.findByG_C(
512 groupId, classNameId);
513
514 deleteStructures(structures);
515 }
516
517
525 @Override
526 public DDMStructure fetchStructure(long structureId)
527 throws SystemException {
528
529 return ddmStructurePersistence.fetchByPrimaryKey(structureId);
530 }
531
532
544 @Override
545 public DDMStructure fetchStructure(
546 long groupId, long classNameId, String structureKey)
547 throws SystemException {
548
549 structureKey = getStructureKey(structureKey);
550
551 return ddmStructurePersistence.fetchByG_C_S(
552 groupId, classNameId, structureKey);
553 }
554
555
576 @Override
577 public DDMStructure fetchStructure(
578 long groupId, long classNameId, String structureKey,
579 boolean includeGlobalStructures)
580 throws PortalException, SystemException {
581
582 structureKey = getStructureKey(structureKey);
583
584 DDMStructure structure = ddmStructurePersistence.fetchByG_C_S(
585 groupId, classNameId, structureKey);
586
587 if ((structure != null) || !includeGlobalStructures) {
588 return structure;
589 }
590
591 Group group = groupPersistence.findByPrimaryKey(groupId);
592
593 Group companyGroup = groupLocalService.getCompanyGroup(
594 group.getCompanyId());
595
596 return ddmStructurePersistence.fetchByG_C_S(
597 companyGroup.getGroupId(), classNameId, structureKey);
598 }
599
600
604 @Override
605 public List<DDMStructure> getClassStructures(long classNameId)
606 throws SystemException {
607
608 return ddmStructurePersistence.findByClassNameId(classNameId);
609 }
610
611
615 @Override
616 public List<DDMStructure> getClassStructures(
617 long classNameId, int start, int end)
618 throws SystemException {
619
620 return ddmStructurePersistence.findByClassNameId(
621 classNameId, start, end);
622 }
623
624
633 @Override
634 public List<DDMStructure> getClassStructures(
635 long companyId, long classNameId)
636 throws SystemException {
637
638 return ddmStructurePersistence.findByC_C(companyId, classNameId);
639 }
640
641
663 @Override
664 public List<DDMStructure> getClassStructures(
665 long companyId, long classNameId, int start, int end)
666 throws SystemException {
667
668 return ddmStructurePersistence.findByC_C(
669 companyId, classNameId, start, end);
670 }
671
672
684 @Override
685 public List<DDMStructure> getClassStructures(
686 long companyId, long classNameId,
687 OrderByComparator orderByComparator)
688 throws SystemException {
689
690 return ddmStructurePersistence.findByC_C(
691 companyId, classNameId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
692 orderByComparator);
693 }
694
695
699 @Override
700 public List<DDMStructure> getClassStructures(
701 long classNameId, OrderByComparator orderByComparator)
702 throws SystemException {
703
704 return ddmStructurePersistence.findByClassNameId(
705 classNameId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
706 orderByComparator);
707 }
708
709
717 @Override
718 public List<DDMStructure> getDLFileEntryTypeStructures(
719 long dlFileEntryTypeId)
720 throws SystemException {
721
722 return dlFileEntryTypePersistence.getDDMStructures(dlFileEntryTypeId);
723 }
724
725
733 @Override
734 public DDMStructure getStructure(long structureId)
735 throws PortalException, SystemException {
736
737 return ddmStructurePersistence.findByPrimaryKey(structureId);
738 }
739
740
752 @Override
753 public DDMStructure getStructure(
754 long groupId, long classNameId, String structureKey)
755 throws PortalException, SystemException {
756
757 structureKey = getStructureKey(structureKey);
758
759 return ddmStructurePersistence.findByG_C_S(
760 groupId, classNameId, structureKey);
761 }
762
763
783 @Override
784 public DDMStructure getStructure(
785 long groupId, long classNameId, String structureKey,
786 boolean includeGlobalStructures)
787 throws PortalException, SystemException {
788
789 structureKey = getStructureKey(structureKey);
790
791 DDMStructure structure = ddmStructurePersistence.fetchByG_C_S(
792 groupId, classNameId, structureKey);
793
794 if (structure != null) {
795 return structure;
796 }
797
798 if (!includeGlobalStructures) {
799 throw new NoSuchStructureException(
800 "No DDMStructure exists with the structure key " +
801 structureKey);
802 }
803
804 Group group = groupPersistence.findByPrimaryKey(groupId);
805
806 Group companyGroup = groupLocalService.getCompanyGroup(
807 group.getCompanyId());
808
809 return ddmStructurePersistence.findByG_C_S(
810 companyGroup.getGroupId(), classNameId, structureKey);
811 }
812
813
822 @Override
823 public List<DDMStructure> getStructure(
824 long groupId, String name, String description)
825 throws SystemException {
826
827 return ddmStructurePersistence.findByG_N_D(groupId, name, description);
828 }
829
830
833 @Override
834 public List<DDMStructure> getStructureEntries() throws SystemException {
835 return getStructures();
836 }
837
838
841 @Override
842 public List<DDMStructure> getStructureEntries(long groupId)
843 throws SystemException {
844
845 return getStructures(groupId);
846 }
847
848
852 @Override
853 public List<DDMStructure> getStructureEntries(
854 long groupId, int start, int end)
855 throws SystemException {
856
857 return getStructures(groupId, start, end);
858 }
859
860
866 @Override
867 public List<DDMStructure> getStructures() throws SystemException {
868 return ddmStructurePersistence.findAll();
869 }
870
871
878 @Override
879 public List<DDMStructure> getStructures(long groupId)
880 throws SystemException {
881
882 return ddmStructurePersistence.findByGroupId(groupId);
883 }
884
885
905 @Override
906 public List<DDMStructure> getStructures(long groupId, int start, int end)
907 throws SystemException {
908
909 return ddmStructurePersistence.findByGroupId(groupId, start, end);
910 }
911
912
921 @Override
922 public List<DDMStructure> getStructures(long groupId, long classNameId)
923 throws SystemException {
924
925 return ddmStructurePersistence.findByG_C(groupId, classNameId);
926 }
927
928
951 @Override
952 public List<DDMStructure> getStructures(
953 long groupId, long classNameId, int start, int end)
954 throws SystemException {
955
956 return ddmStructurePersistence.findByG_C(
957 groupId, classNameId, start, end);
958 }
959
960
985 @Override
986 public List<DDMStructure> getStructures(
987 long groupId, long classNameId, int start, int end,
988 OrderByComparator orderByComparator)
989 throws SystemException {
990
991 return ddmStructurePersistence.findByG_C(
992 groupId, classNameId, start, end, orderByComparator);
993 }
994
995 @Override
996 public List<DDMStructure> getStructures(
997 long groupId, String name, String description)
998 throws SystemException {
999
1000 return ddmStructurePersistence.findByG_N_D(groupId, name, description);
1001 }
1002
1003
1010 @Override
1011 public List<DDMStructure> getStructures(long[] groupIds)
1012 throws SystemException {
1013
1014 return ddmStructurePersistence.findByGroupId(groupIds);
1015 }
1016
1017
1027 @Override
1028 public List<DDMStructure> getStructures(long[] groupIds, long classNameId)
1029 throws SystemException {
1030
1031 return ddmStructurePersistence.findByG_C(groupIds, classNameId);
1032 }
1033
1034
1057 @Override
1058 public List<DDMStructure> getStructures(
1059 long[] groupIds, long classNameId, int start, int end)
1060 throws SystemException {
1061
1062 return ddmStructurePersistence.findByG_C(
1063 groupIds, classNameId, start, end);
1064 }
1065
1066
1073 @Override
1074 public int getStructuresCount(long groupId) throws SystemException {
1075 return ddmStructurePersistence.countByGroupId(groupId);
1076 }
1077
1078
1087 @Override
1088 public int getStructuresCount(long groupId, long classNameId)
1089 throws SystemException {
1090
1091 return ddmStructurePersistence.countByG_C(groupId, classNameId);
1092 }
1093
1094
1104 @Override
1105 public int getStructuresCount(long[] groupIds, long classNameId)
1106 throws SystemException {
1107
1108 return ddmStructurePersistence.countByG_C(groupIds, classNameId);
1109 }
1110
1111
1140 @Override
1141 public List<DDMStructure> search(
1142 long companyId, long[] groupIds, long[] classNameIds,
1143 String keywords, int start, int end,
1144 OrderByComparator orderByComparator)
1145 throws SystemException {
1146
1147 return ddmStructureFinder.findByKeywords(
1148 companyId, groupIds, classNameIds, keywords, start, end,
1149 orderByComparator);
1150 }
1151
1152
1187 @Override
1188 public List<DDMStructure> search(
1189 long companyId, long[] groupIds, long[] classNameIds, String name,
1190 String description, String storageType, int type,
1191 boolean andOperator, int start, int end,
1192 OrderByComparator orderByComparator)
1193 throws SystemException {
1194
1195 return ddmStructureFinder.findByC_G_C_N_D_S_T(
1196 companyId, groupIds, classNameIds, name, description, storageType,
1197 type, andOperator, start, end, orderByComparator);
1198 }
1199
1200
1213 @Override
1214 public int searchCount(
1215 long companyId, long[] groupIds, long[] classNameIds,
1216 String keywords)
1217 throws SystemException {
1218
1219 return ddmStructureFinder.countByKeywords(
1220 companyId, groupIds, classNameIds, keywords);
1221 }
1222
1223
1243 @Override
1244 public int searchCount(
1245 long companyId, long[] groupIds, long[] classNameIds, String name,
1246 String description, String storageType, int type,
1247 boolean andOperator)
1248 throws SystemException {
1249
1250 return ddmStructureFinder.countByC_G_C_N_D_S_T(
1251 companyId, groupIds, classNameIds, name, description, storageType,
1252 type, andOperator);
1253 }
1254
1255
1276 @Override
1277 public DDMStructure updateStructure(
1278 long groupId, long parentStructureId, long classNameId,
1279 String structureKey, Map<Locale, String> nameMap,
1280 Map<Locale, String> descriptionMap, String xsd,
1281 ServiceContext serviceContext)
1282 throws PortalException, SystemException {
1283
1284 structureKey = getStructureKey(structureKey);
1285
1286 DDMStructure structure = ddmStructurePersistence.findByG_C_S(
1287 groupId, classNameId, structureKey);
1288
1289 return doUpdateStructure(
1290 parentStructureId, nameMap, descriptionMap, xsd, serviceContext,
1291 structure);
1292 }
1293
1294
1311 @Override
1312 public DDMStructure updateStructure(
1313 long structureId, long parentStructureId,
1314 Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
1315 String xsd, ServiceContext serviceContext)
1316 throws PortalException, SystemException {
1317
1318 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
1319 structureId);
1320
1321 return doUpdateStructure(
1322 parentStructureId, nameMap, descriptionMap, xsd, serviceContext,
1323 structure);
1324 }
1325
1326
1339 @Override
1340 public DDMStructure updateXSD(
1341 long structureId, String xsd, ServiceContext serviceContext)
1342 throws PortalException, SystemException {
1343
1344 DDMStructure structure = ddmStructurePersistence.findByPrimaryKey(
1345 structureId);
1346
1347 return doUpdateStructure(
1348 structure.getParentStructureId(), structure.getNameMap(),
1349 structure.getDescriptionMap(), xsd, serviceContext, structure);
1350 }
1351
1352
1369 @Override
1370 public void updateXSDFieldMetadata(
1371 long structureId, String fieldName, String metadataEntryName,
1372 String metadataEntryValue, ServiceContext serviceContext)
1373 throws PortalException, SystemException {
1374
1375 DDMStructure ddmStructure = fetchDDMStructure(structureId);
1376
1377 if (ddmStructure == null) {
1378 return;
1379 }
1380
1381 String xsd = ddmStructure.getXsd();
1382
1383 try {
1384 Document document = SAXReaderUtil.read(xsd);
1385
1386 Element rootElement = document.getRootElement();
1387
1388 List<Element> dynamicElementElements = rootElement.elements(
1389 "dynamic-element");
1390
1391 for (Element dynamicElementElement : dynamicElementElements) {
1392 String dynamicElementElementFieldName = GetterUtil.getString(
1393 dynamicElementElement.attributeValue("name"));
1394
1395 if (!dynamicElementElementFieldName.equals(fieldName)) {
1396 continue;
1397 }
1398
1399 List<Element> metadataElements = dynamicElementElement.elements(
1400 "meta-data");
1401
1402 for (Element metadataElement : metadataElements) {
1403 List<Element> metadataEntryElements =
1404 metadataElement.elements();
1405
1406 for (Element metadataEntryElement : metadataEntryElements) {
1407 String metadataEntryElementName = GetterUtil.getString(
1408 metadataEntryElement.attributeValue("name"));
1409
1410 if (metadataEntryElementName.equals(
1411 metadataEntryName)) {
1412
1413 metadataEntryElement.setText(metadataEntryValue);
1414 }
1415 }
1416 }
1417 }
1418
1419 updateXSD(structureId, document.asXML(), serviceContext);
1420 }
1421 catch (DocumentException de) {
1422 throw new SystemException(de);
1423 }
1424 }
1425
1426 protected void appendNewStructureRequiredFields(
1427 DDMStructure structure, Document structureDocument,
1428 Document templateDocument) {
1429
1430 Element templateRootElement = templateDocument.getRootElement();
1431
1432 XPath structureXPath = SAXReaderUtil.createXPath(
1433 "
1434
1435 List<Node> structureNodes = structureXPath.selectNodes(
1436 structureDocument);
1437
1438 for (Node structureNode : structureNodes) {
1439 Element structureElement = (Element)structureNode;
1440
1441 String name = structureElement.attributeValue("name");
1442
1443 name = HtmlUtil.escapeXPathAttribute(name);
1444
1445 XPath templateXPath = SAXReaderUtil.createXPath(
1446 "
1447
1448 List<Node> matchingTemplateNodes = templateXPath.selectNodes(
1449 templateDocument);
1450
1451 if (matchingTemplateNodes.isEmpty()) {
1452 templateRootElement.add(structureElement.createCopy());
1453 }
1454 }
1455 }
1456
1457 protected Set<Long> deleteStructures(List<DDMStructure> structures)
1458 throws PortalException, SystemException {
1459
1460 Set<Long> deletedStructureIds = new HashSet<Long>();
1461
1462 for (DDMStructure structure : structures) {
1463 if (deletedStructureIds.contains(structure.getStructureId())) {
1464 continue;
1465 }
1466
1467 if (!GroupThreadLocal.isDeleteInProcess()) {
1468 List<DDMStructure> childDDMStructures =
1469 ddmStructurePersistence.findByParentStructureId(
1470 structure.getStructureId());
1471
1472 deletedStructureIds.addAll(
1473 deleteStructures(childDDMStructures));
1474 }
1475
1476 ddmStructureLocalService.deleteStructure(structure);
1477
1478 deletedStructureIds.add(structure.getStructureId());
1479 }
1480
1481 return deletedStructureIds;
1482 }
1483
1484 protected DDMStructure doUpdateStructure(
1485 long parentStructureId, Map<Locale, String> nameMap,
1486 Map<Locale, String> descriptionMap, String xsd,
1487 ServiceContext serviceContext, DDMStructure structure)
1488 throws PortalException, SystemException {
1489
1490 try {
1491 xsd = DDMXMLUtil.validateXML(xsd);
1492 xsd = DDMXMLUtil.formatXML(xsd);
1493 }
1494 catch (Exception e) {
1495 throw new StructureXsdException();
1496 }
1497
1498 String parentXsd = StringPool.BLANK;
1499
1500 DDMStructure parentStructure =
1501 ddmStructurePersistence.fetchByPrimaryKey(parentStructureId);
1502
1503 if (parentStructure != null) {
1504 parentXsd = parentStructure.getCompleteXsd();
1505 }
1506
1507 validate(nameMap, parentXsd, xsd);
1508
1509 structure.setModifiedDate(serviceContext.getModifiedDate(null));
1510 structure.setParentStructureId(parentStructureId);
1511 structure.setNameMap(nameMap);
1512 structure.setDescriptionMap(descriptionMap);
1513 structure.setXsd(xsd);
1514
1515 ddmStructurePersistence.update(structure);
1516
1517 syncStructureTemplatesFields(structure);
1518
1519 if (!serviceContext.isIndexingEnabled()) {
1520 return structure;
1521 }
1522
1523 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1524 structure.getClassName());
1525
1526 List<Long> ddmStructureIds = getChildrenStructureIds(
1527 structure.getGroupId(), structure.getStructureId());
1528
1529 indexer.reindexDDMStructures(ddmStructureIds);
1530
1531 return structure;
1532 }
1533
1534 protected void getChildrenStructureIds(
1535 List<Long> structureIds, long groupId, long parentStructureId)
1536 throws PortalException, SystemException {
1537
1538 List<DDMStructure> structures = ddmStructurePersistence.findByG_P(
1539 groupId, parentStructureId);
1540
1541 for (DDMStructure structure : structures) {
1542 structureIds.add(structure.getStructureId());
1543
1544 getChildrenStructureIds(
1545 structureIds, structure.getGroupId(),
1546 structure.getStructureId());
1547 }
1548 }
1549
1550 protected List<Long> getChildrenStructureIds(long groupId, long structureId)
1551 throws PortalException, SystemException {
1552
1553 List<Long> structureIds = new ArrayList<Long>();
1554
1555 getChildrenStructureIds(structureIds, groupId, structureId);
1556
1557 structureIds.add(0, structureId);
1558
1559 return structureIds;
1560 }
1561
1562 protected Set<String> getElementNames(Document document)
1563 throws PortalException {
1564
1565 Set<String> elementNames = new HashSet<String>();
1566
1567 XPath xPathSelector = SAXReaderUtil.createXPath("
1568
1569 List<Node> nodes = xPathSelector.selectNodes(document);
1570
1571 for (Node node : nodes) {
1572 Element element = (Element)node;
1573
1574 String name = StringUtil.toLowerCase(
1575 element.attributeValue("name"));
1576
1577 elementNames.add(name);
1578 }
1579
1580 return elementNames;
1581 }
1582
1583 protected String getStructureKey(String structureKey) {
1584 if (structureKey != null) {
1585 structureKey = structureKey.trim();
1586
1587 return StringUtil.toUpperCase(structureKey);
1588 }
1589
1590 return StringPool.BLANK;
1591 }
1592
1593 protected void syncStructureTemplatesFields(DDMStructure structure)
1594 throws PortalException, SystemException {
1595
1596 long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
1597
1598 List<DDMTemplate> templates = ddmTemplateLocalService.getTemplates(
1599 structure.getGroupId(), classNameId, structure.getStructureId(),
1600 DDMTemplateConstants.TEMPLATE_TYPE_FORM);
1601
1602 Document structureDocument = null;
1603
1604 try {
1605 structureDocument = SAXReaderUtil.read(structure.getXsd());
1606 }
1607 catch (DocumentException de) {
1608 if (_log.isWarnEnabled()) {
1609 _log.warn(de, de);
1610 }
1611
1612 return;
1613 }
1614
1615 for (DDMTemplate template : templates) {
1616 String script = template.getScript();
1617
1618 Document templateDocument = null;
1619
1620 try {
1621 templateDocument = SAXReaderUtil.read(script);
1622 }
1623 catch (DocumentException de) {
1624 if (_log.isWarnEnabled()) {
1625 _log.warn(de, de);
1626 }
1627
1628 continue;
1629 }
1630
1631 Element templateRootElement = templateDocument.getRootElement();
1632
1633 syncStructureTemplatesFields(
1634 structureDocument, template, templateRootElement);
1635
1636 appendNewStructureRequiredFields(
1637 structure, structureDocument, templateDocument);
1638
1639 try {
1640 script = DDMXMLUtil.formatXML(templateDocument.asXML());
1641 }
1642 catch (Exception e) {
1643 throw new StructureXsdException();
1644 }
1645
1646 template.setScript(script);
1647
1648 ddmTemplatePersistence.update(template);
1649 }
1650 }
1651
1652 protected void syncStructureTemplatesFields(
1653 Document structureDocument, DDMTemplate template,
1654 Element templateElement)
1655 throws PortalException, SystemException {
1656
1657 DDMStructure structure = DDMTemplateHelperUtil.fetchStructure(template);
1658
1659 List<Element> dynamicElementElements = templateElement.elements(
1660 "dynamic-element");
1661
1662 for (Element dynamicElementElement : dynamicElementElements) {
1663 String dataType = dynamicElementElement.attributeValue("dataType");
1664 String fieldName = dynamicElementElement.attributeValue("name");
1665
1666 if (Validator.isNull(dataType)) {
1667 continue;
1668 }
1669
1670 if (!structure.hasField(fieldName)) {
1671 templateElement.remove(dynamicElementElement);
1672
1673 continue;
1674 }
1675
1676 XPath structureXPath = SAXReaderUtil.createXPath(
1677 "
1678
1679 Element structureElement =
1680 (Element)structureXPath.selectSingleNode(
1681 structureDocument.getRootElement());
1682
1683 syncFieldOptions(structureElement, dynamicElementElement);
1684
1685 String mode = template.getMode();
1686
1687 if (mode.equals(DDMTemplateConstants.TEMPLATE_MODE_CREATE)) {
1688 boolean fieldRequired = structure.getFieldRequired(fieldName);
1689
1690 List<Element> metadataElements = dynamicElementElement.elements(
1691 "meta-data");
1692
1693 for (Element metadataElement : metadataElements) {
1694 for (Element metadataEntryElement :
1695 metadataElement.elements()) {
1696
1697 String attributeName =
1698 metadataEntryElement.attributeValue("name");
1699
1700 if (fieldRequired && attributeName.equals("required")) {
1701 metadataEntryElement.setText("true");
1702 }
1703 }
1704 }
1705 }
1706
1707 syncStructureTemplatesFields(
1708 structureDocument, template, dynamicElementElement);
1709 }
1710 }
1711
1712 protected void syncFieldOptions(
1713 Element structureElement, Element templateElement) {
1714
1715 if (structureElement == null) {
1716 return;
1717 }
1718
1719 String type = templateElement.attributeValue("type");
1720
1721 if (!(type.equals("select") || type.equals("radio"))) {
1722 return;
1723 }
1724
1725 List<Element> metadataElements = templateElement.elements("meta-data");
1726
1727 templateElement.clearContent();
1728
1729 List<Element> dynamicElementElements = structureElement.elements(
1730 "dynamic-element");
1731
1732 for (Element dynamicElement :dynamicElementElements) {
1733 templateElement.add(dynamicElement.createCopy());
1734 }
1735
1736 for (Element metadateElement : metadataElements) {
1737 templateElement.add(metadateElement.createCopy());
1738 }
1739 }
1740
1741 protected void validate(Document document) throws PortalException {
1742 XPath xPathSelector = SAXReaderUtil.createXPath("
1743
1744 List<Node> nodes = xPathSelector.selectNodes(document);
1745
1746 Set<String> elementNames = new HashSet<String>();
1747
1748 for (Node node : nodes) {
1749 Element element = (Element)node;
1750
1751 String name = StringUtil.toLowerCase(
1752 element.attributeValue("name"));
1753
1754 if (name.startsWith(DDMStructureConstants.XSD_NAME_RESERVED)) {
1755 throw new StructureXsdException();
1756 }
1757
1758 if (elementNames.contains(name)) {
1759 throw new StructureDuplicateElementException();
1760 }
1761
1762 elementNames.add(name);
1763 }
1764 }
1765
1766 protected void validate(Document parentDocument, Document childDocument)
1767 throws PortalException {
1768
1769 Set<String> parentElementNames = getElementNames(parentDocument);
1770
1771 for (String childElementName : getElementNames(childDocument)) {
1772 if (parentElementNames.contains(childElementName)) {
1773 throw new StructureDuplicateElementException();
1774 }
1775 }
1776 }
1777
1778 protected void validate(
1779 long groupId, long parentStructureId, long classNameId,
1780 String structureKey, Map<Locale, String> nameMap, String xsd)
1781 throws PortalException, SystemException {
1782
1783 structureKey = getStructureKey(structureKey);
1784
1785 DDMStructure structure = ddmStructurePersistence.fetchByG_C_S(
1786 groupId, classNameId, structureKey);
1787
1788 if (structure != null) {
1789 StructureDuplicateStructureKeyException sdske =
1790 new StructureDuplicateStructureKeyException();
1791
1792 sdske.setStructureKey(structure.getStructureKey());
1793
1794 throw sdske;
1795 }
1796
1797 String parentXsd = StringPool.BLANK;
1798
1799 DDMStructure parentStructure =
1800 ddmStructurePersistence.fetchByPrimaryKey(parentStructureId);
1801
1802 if (parentStructure != null) {
1803 parentXsd = parentStructure.getCompleteXsd();
1804 }
1805
1806 validate(nameMap, parentXsd, xsd);
1807 }
1808
1809 protected void validate(
1810 Map<Locale, String> nameMap, Locale contentDefaultLocale)
1811 throws PortalException {
1812
1813 String name = nameMap.get(contentDefaultLocale);
1814
1815 if (Validator.isNull(name)) {
1816 throw new StructureNameException();
1817 }
1818
1819 Locale[] availableLocales = LanguageUtil.getAvailableLocales();
1820
1821 if (!ArrayUtil.contains(availableLocales, contentDefaultLocale)) {
1822 Long companyId = CompanyThreadLocal.getCompanyId();
1823
1824 LocaleException le = new LocaleException(
1825 LocaleException.TYPE_CONTENT,
1826 "The locale " + contentDefaultLocale +
1827 " is not available in company " + companyId);
1828
1829 le.setSourceAvailableLocales(new Locale[] {contentDefaultLocale});
1830 le.setTargetAvailableLocales(availableLocales);
1831
1832 throw le;
1833 }
1834 }
1835
1836 protected void validate(
1837 Map<Locale, String> nameMap, String parentXsd, String childXsd)
1838 throws PortalException {
1839
1840 try {
1841 Document document = SAXReaderUtil.read(childXsd);
1842
1843 Element rootElement = document.getRootElement();
1844
1845 Locale contentDefaultLocale = LocaleUtil.fromLanguageId(
1846 rootElement.attributeValue("default-locale"));
1847
1848 validate(nameMap, contentDefaultLocale);
1849
1850 validate(document);
1851
1852 if (Validator.isNotNull(parentXsd)) {
1853 Document parentDocument = SAXReaderUtil.read(parentXsd);
1854
1855 validate(parentDocument, document);
1856 }
1857 }
1858 catch (LocaleException le) {
1859 throw le;
1860 }
1861 catch (StructureDuplicateElementException sdee) {
1862 throw sdee;
1863 }
1864 catch (StructureNameException sne) {
1865 throw sne;
1866 }
1867 catch (StructureXsdException sxe) {
1868 throw sxe;
1869 }
1870 catch (Exception e) {
1871 throw new StructureXsdException();
1872 }
1873 }
1874
1875 private static Log _log = LogFactoryUtil.getLog(
1876 DDMStructureLocalServiceImpl.class);
1877
1878 }