1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.imixs.workflow.jee.ejb;
29
30 import java.util.ArrayList;
31 import java.util.Calendar;
32 import java.util.Collection;
33 import java.util.Date;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.StringTokenizer;
38 import java.util.Vector;
39 import java.util.logging.Logger;
40
41 import javax.annotation.Resource;
42 import javax.annotation.security.DeclareRoles;
43 import javax.annotation.security.RolesAllowed;
44 import javax.ejb.LocalBean;
45 import javax.ejb.SessionContext;
46 import javax.ejb.Stateless;
47 import javax.persistence.EntityManager;
48 import javax.persistence.FlushModeType;
49 import javax.persistence.PersistenceContext;
50 import javax.persistence.Query;
51
52 import org.imixs.workflow.ItemCollection;
53 import org.imixs.workflow.exceptions.AccessDeniedException;
54 import org.imixs.workflow.exceptions.InvalidAccessException;
55
56 import org.imixs.workflow.jee.jpa.CalendarItem;
57 import org.imixs.workflow.jee.jpa.DoubleItem;
58 import org.imixs.workflow.jee.jpa.Entity;
59 import org.imixs.workflow.jee.jpa.EntityIndex;
60 import org.imixs.workflow.jee.jpa.IntegerItem;
61 import org.imixs.workflow.jee.jpa.ReadAccess;
62 import org.imixs.workflow.jee.jpa.TextItem;
63 import org.imixs.workflow.jee.jpa.WriteAccess;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 @DeclareRoles({ "org.imixs.ACCESSLEVEL.NOACCESS",
154 "org.imixs.ACCESSLEVEL.READERACCESS",
155 "org.imixs.ACCESSLEVEL.AUTHORACCESS",
156 "org.imixs.ACCESSLEVEL.EDITORACCESS",
157 "org.imixs.ACCESSLEVEL.MANAGERACCESS" })
158 @RolesAllowed({ "org.imixs.ACCESSLEVEL.NOACCESS",
159 "org.imixs.ACCESSLEVEL.READERACCESS",
160 "org.imixs.ACCESSLEVEL.AUTHORACCESS",
161 "org.imixs.ACCESSLEVEL.EDITORACCESS",
162 "org.imixs.ACCESSLEVEL.MANAGERACCESS" })
163 @Stateless
164 @LocalBean
165 public class EntityService implements EntityServiceRemote {
166
167 public static final int TYP_TEXT = 0;
168
169 public static final int TYP_INT = 1;
170
171 public static final int TYP_DOUBLE = 2;
172
173 public static final int TYP_CALENDAR = 3;
174
175 public static final String ACCESSLEVEL_NOACCESS = "org.imixs.ACCESSLEVEL.NOACCESS";
176
177 public static final String ACCESSLEVEL_READERACCESS = "org.imixs.ACCESSLEVEL.READERACCESS";
178
179 public static final String ACCESSLEVEL_AUTHORACCESS = "org.imixs.ACCESSLEVEL.AUTHORACCESS";
180
181 public static final String ACCESSLEVEL_EDITORACCESS = "org.imixs.ACCESSLEVEL.EDITORACCESS";
182
183 public static final String ACCESSLEVEL_MANAGERACCESS = "org.imixs.ACCESSLEVEL.MANAGERACCESS";
184
185 public static final String UNIQUEID = "$uniqueid";
186
187 public static final String USER_GROUP_LIST = "org.imixs.USER.GROUPLIST";
188
189 private static Logger logger = Logger.getLogger("org.imixs.workflow");
190
191 @Resource
192 SessionContext ctx;
193
194 @Resource(name = "READ_ACCESS_FIELDS")
195 private String readAccessFields = "";
196 @Resource(name = "WRITE_ACCESS_FIELDS")
197 private String writeAccessFields = "";
198 @Resource(name = "ACCESS_ROLES")
199 private String accessRoles = "";
200 @Resource(name = "DISABLE_OPTIMISTIC_LOCKING")
201 private Boolean disbaleOptimisticLocking = false;
202
203 @PersistenceContext(unitName = "org.imixs.workflow.jee.jpa")
204 private EntityManager manager;
205
206
207
208
209
210
211
212 public String getAccessRoles() {
213 return accessRoles;
214 }
215
216
217
218
219
220
221
222 public String getReadAccessFields() {
223 return readAccessFields;
224 }
225
226
227
228
229
230
231
232 public String getWriteAccessFields() {
233 return writeAccessFields;
234 }
235
236
237
238
239
240
241
242 public List<String> getUserNameList() {
243
244 List<String> userNameList = new Vector<String>();
245
246
247 userNameList.add(ctx.getCallerPrincipal().getName().toString());
248
249 String roleList = "org.imixs.ACCESSLEVEL.READERACCESS,org.imixs.ACCESSLEVEL.AUTHORACCESS,org.imixs.ACCESSLEVEL.EDITORACCESS,"
250 + accessRoles;
251
252 StringTokenizer roleListTokens = new StringTokenizer(roleList, ",");
253 while (roleListTokens.hasMoreTokens()) {
254 try {
255 String testRole = roleListTokens.nextToken().trim();
256 if (!"".equals(testRole) && ctx.isCallerInRole(testRole))
257 userNameList.add(testRole);
258 } catch (Exception e) {
259
260
261
262 }
263 }
264
265
266
267 String[] applicationGroups = getUserGroupList();
268 if (applicationGroups != null)
269 for (String auserRole : applicationGroups)
270 userNameList.add(auserRole);
271
272 return userNameList;
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 public ItemCollection save(ItemCollection itemcol)
294 throws AccessDeniedException {
295
296 Entity activeEntity = null;
297 ItemCollection slimItemCollection = null;
298
299
300
301
302
303
304
305 Collection<EntityIndex> entityIndexCache = getIndices();
306
307
308 manager.setFlushMode(FlushModeType.COMMIT);
309
310
311 String sID = itemcol.getItemValueString(UNIQUEID);
312 if (!"".equals(sID)) {
313
314 activeEntity = manager.find(Entity.class, sID);
315 if (activeEntity == null) {
316 logger.fine("[EntityService] Entity '" + sID + "' not found!");
317 } else {
318
319
320
321
322
323
324
325
326 }
327
328 }
329
330
331 if (activeEntity == null) {
332
333
334
335 if (!(ctx.isCallerInRole(ACCESSLEVEL_MANAGERACCESS)
336 || ctx.isCallerInRole(ACCESSLEVEL_EDITORACCESS) || ctx
337 .isCallerInRole(ACCESSLEVEL_AUTHORACCESS))) {
338 throw new AccessDeniedException(
339 "[EntityService] You are not allowed to perform this operation");
340 }
341
342 activeEntity = new Entity(sID);
343
344 Date datCreated = itemcol.getItemValueDate("$Created");
345 if (datCreated != null) {
346 Calendar cal = Calendar.getInstance();
347 cal.setTime(datCreated);
348
349 activeEntity.setCreated(cal);
350 }
351
352
353 manager.persist(activeEntity);
354
355 } else {
356
357
358 if (!isCallerAuthor(activeEntity) || !isCallerReader(activeEntity)) {
359 throw new AccessDeniedException(
360 "[EntityService] You are not allowed to perform this operation");
361 }
362 }
363
364
365
366
367
368 itemcol.removeItem("$isauthor");
369
370 String aType = itemcol.getItemValueString("type");
371 if ("".equals(aType))
372 aType = "Entity";
373 activeEntity.setType(aType);
374
375
376 Calendar cal = Calendar.getInstance();
377
378 itemcol.replaceItemValue("$uniqueid", activeEntity.getId());
379 itemcol.replaceItemValue("$modified", cal.getTime());
380 itemcol.replaceItemValue("$created", activeEntity.getCreated()
381 .getTime());
382
383 slimItemCollection = new ItemCollection(itemcol.getAllItems());
384
385
386 updateReadAccessList(slimItemCollection, activeEntity);
387 updateWriteAccessList(slimItemCollection, activeEntity);
388
389 explodeEntity(slimItemCollection, activeEntity, entityIndexCache);
390
391
392 activeEntity.setData(slimItemCollection.getAllItems());
393
394
395
396 itemcol.replaceItemValue("$isauthor", isCallerAuthor(activeEntity));
397
398 return itemcol;
399 }
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422 public ItemCollection load(String id) {
423 Entity activeEntity = null;
424 activeEntity = manager.find(Entity.class, id);
425
426
427 if (activeEntity != null && isCallerReader(activeEntity)) {
428
429
430
431
432
433
434 return implodeEntity(activeEntity);
435 } else
436 return null;
437 }
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455 public void remove(ItemCollection itemcol) throws AccessDeniedException {
456 Entity activeEntity = null;
457 String sID = itemcol.getItemValueString("$uniqueid");
458 activeEntity = manager.find(Entity.class, sID);
459
460 if (activeEntity != null) {
461 if (!isCallerReader(activeEntity) || !isCallerAuthor(activeEntity))
462 throw new AccessDeniedException(
463 "[EntityService] You are not allowed to perform this operation");
464
465
466 for (TextItem aItem : activeEntity.getTextItems())
467 manager.remove(aItem);
468 activeEntity.getTextItems().clear();
469
470
471 for (CalendarItem aItem : activeEntity.getCalendarItems())
472 manager.remove(aItem);
473 activeEntity.getCalendarItems().clear();
474
475
476 for (IntegerItem aItem : activeEntity.getIntegerItems())
477 manager.remove(aItem);
478 activeEntity.getIntegerItems().clear();
479
480
481 for (DoubleItem aItem : activeEntity.getDoubleItems())
482 manager.remove(aItem);
483 activeEntity.getDoubleItems().clear();
484
485
486 for (WriteAccess aItem : activeEntity.getWriteAccessList())
487 manager.remove(aItem);
488 activeEntity.getWriteAccessList().clear();
489
490
491 for (ReadAccess aItem : activeEntity.getReadAccessList())
492 manager.remove(aItem);
493 activeEntity.getReadAccessList().clear();
494
495
496 manager.remove(activeEntity);
497
498 } else
499 throw new AccessDeniedException("[EntityService] invalid $uniqueid");
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516 public void addIndex(String stitel, int ityp) throws AccessDeniedException {
517
518 stitel = stitel.toLowerCase();
519
520
521 EntityIndex activeEntityIndex = manager.find(EntityIndex.class, stitel);
522 if (activeEntityIndex != null)
523 return;
524
525
526
527
528 if (ctx.isCallerInRole("org.imixs.ACCESSLEVEL.MANAGERACCESS") == false)
529 throw new AccessDeniedException(
530 "[EntityService] You are not allowed to add index fields");
531
532
533 logger.info("[EntityServiceBean] add new Index: " + stitel + ":" + ityp);
534 activeEntityIndex = new EntityIndex(stitel, ityp);
535 manager.persist(activeEntityIndex);
536
537
538
539 Collection<Entity> entityList = null;
540 Query q = manager.createQuery("SELECT entity FROM Entity entity");
541 entityList = q.getResultList();
542 logger.info("[EntityServiceBean] found " + entityList.size()
543 + " existing entities. Starting update...");
544 updateAllEntityIndexFields(entityList, stitel);
545 logger.info("[EntityServiceBean] index update completed");
546
547 }
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567 public void removeIndex(String stitel) throws AccessDeniedException {
568 int indexType = 0;
569
570 stitel = stitel.toLowerCase();
571
572
573 EntityIndex activeEntityIndex = manager.find(EntityIndex.class, stitel);
574 if (activeEntityIndex == null)
575 return;
576
577
578 if (ctx.isCallerInRole("org.imixs.ACCESSLEVEL.MANAGERACCESS") == false)
579 throw new AccessDeniedException(
580 "[EntityService] You are not allowed to add index fields");
581
582 indexType = activeEntityIndex.getTyp();
583
584 logger.info("[EntityService] remove Index: " + stitel + ":" + indexType);
585
586
587 manager.remove(activeEntityIndex);
588
589
590
591 Collection<Entity> entityList = null;
592
593 String query = "SELECT wi FROM Entity wi ";
594 switch (indexType) {
595 case EntityIndex.TYP_CALENDAR:
596 query += " JOIN wi.calendarItems AS i1 WHERE i1.itemName='"
597 + stitel + "'";
598 break;
599 case EntityIndex.TYP_DOUBLE:
600 query += " JOIN wi.doubleItems AS i1 WHERE i1.itemName='" + stitel
601 + "'";
602 break;
603 case EntityIndex.TYP_INT:
604 query += " JOIN wi.integerItems AS i1 WHERE i1.itemName='" + stitel
605 + "'";
606 break;
607
608 default:
609 query += " JOIN wi.textItems AS i1 WHERE i1.itemName='" + stitel
610 + "'";
611 break;
612 }
613
614 logger.info("[EntityServiceBean] remove Index - update query=" + query);
615 Query q = manager.createQuery(query);
616
617 entityList = q.getResultList();
618 logger.info("[EntityServiceBean] found " + entityList.size()
619 + " affected entities. Starting update...");
620 updateAllEntityIndexFields(entityList, null);
621 logger.info("[EntityServiceBean] index update completed");
622
623 }
624
625
626
627
628
629
630
631 public Collection<EntityIndex> getIndices() {
632
633 logger.finer("getIndiecies....");
634
635 Query q = manager
636 .createQuery("SELECT entityindex FROM EntityIndex entityindex");
637 return q.getResultList();
638 }
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655 public Collection<ItemCollection> findAllEntities(String query,
656 int startpos, int count) throws InvalidAccessException {
657 Vector<ItemCollection> vectorResult = new Vector<ItemCollection>();
658
659
660 query = optimizeQuery(query);
661 try {
662 Query q = manager.createQuery(query);
663 if (startpos >= 0)
664 q.setFirstResult(startpos);
665 if (count > 0)
666 q.setMaxResults(count);
667
668 Collection<Entity> entityList = q.getResultList();
669
670 if (entityList == null)
671 return vectorResult;
672 for (Entity aEntity : entityList) {
673
674
675
676
677
678
679
680
681 vectorResult.add(implodeEntity(aEntity));
682 }
683 } catch (RuntimeException nre) {
684 throw new InvalidAccessException(
685 "[EntityService] Error findAllEntities: '" + query + "' ",
686 nre);
687 }
688 return vectorResult;
689
690 }
691
692
693
694
695
696
697
698
699
700
701 public ItemCollection findParentEntity(ItemCollection child)
702 throws InvalidAccessException {
703 String parentUniqueID = child.getItemValueString("$uniqueidref");
704 return this.load(parentUniqueID);
705 }
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723 public Collection<ItemCollection> findChildEntities(ItemCollection child,
724 int start, int count) throws InvalidAccessException {
725
726 String parentUniqueID = child.getItemValueString("$uniqueid");
727
728 String sQuery = "SELECT wi FROM Entity AS wi ";
729 sQuery += " JOIN wi.textItems as t2 ";
730 sQuery += " WHERE t2.itemName = '$uniqueidref' and t2.itemValue = '"
731 + parentUniqueID + "' ";
732
733 return this.findAllEntities(sQuery, start, count);
734 }
735
736
737
738
739
740
741
742 private boolean isCallerReader(Entity aEntity) {
743
744 List<ReadAccess> readAccessList = aEntity.getReadAccessList();
745
746
747
748
749
750
751
752 if (ctx.isCallerInRole(ACCESSLEVEL_NOACCESS))
753 return false;
754
755
756
757
758
759
760
761 if (ctx.isCallerInRole(ACCESSLEVEL_MANAGERACCESS))
762 return true;
763
764
765
766
767
768
769
770 if (readAccessList == null || readAccessList.size() == 0) {
771
772 return true;
773 }
774
775 boolean notemptyfield = false;
776
777
778 List<String> auserNameList = getUserNameList();
779
780
781 for (ReadAccess aReadAccess : readAccessList) {
782 if (aReadAccess != null && !"".equals(aReadAccess.getValue())) {
783 notemptyfield = true;
784 if (auserNameList.indexOf(aReadAccess.getValue()) > -1)
785 return true;
786
787 }
788 }
789 if (!notemptyfield)
790 return true;
791
792 return false;
793 }
794
795
796
797
798
799
800 private boolean isCallerAuthor(Entity aEntity) {
801 List<WriteAccess> writeAccessList = aEntity.getWriteAccessList();
802
803
804
805
806 if (ctx.isCallerInRole(ACCESSLEVEL_NOACCESS))
807 return false;
808
809
810
811
812
813 if (ctx.isCallerInRole(ACCESSLEVEL_MANAGERACCESS)
814 || ctx.isCallerInRole(ACCESSLEVEL_EDITORACCESS))
815 return true;
816
817
818
819
820
821
822
823 if (ctx.isCallerInRole(ACCESSLEVEL_AUTHORACCESS)) {
824 if (writeAccessList == null || writeAccessList.size() == 0) {
825
826 return false;
827 }
828
829
830 List<String> auserNameList = getUserNameList();
831
832
833 for (WriteAccess aWriteAccess : writeAccessList) {
834 if (aWriteAccess != null && !"".equals(aWriteAccess.getValue())) {
835 if (auserNameList.indexOf(aWriteAccess.getValue()) > -1)
836 return true;
837 }
838 }
839 }
840 return false;
841 }
842
843
844
845
846
847
848
849
850
851
852
853 private String[] getUserGroupList() {
854
855 String[] applicationUserGroupList = (String[]) ctx.getContextData()
856 .get(USER_GROUP_LIST);
857
858 if (applicationUserGroupList != null)
859
860 for (int i = 0; i < applicationUserGroupList.length; i++) {
861 applicationUserGroupList[i] = applicationUserGroupList[i]
862 .trim();
863 }
864
865 return applicationUserGroupList;
866 }
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881 private void updateWriteAccessList(ItemCollection itemCol, Entity aEntity) {
882 List<String> vAccessFieldList = new ArrayList<String>();
883
884
885
886 for (WriteAccess aItem : aEntity.getWriteAccessList())
887 manager.remove(aItem);
888 aEntity.getWriteAccessList().clear();
889
890
891 vAccessFieldList.add("$writeAccess");
892
893
894 if (writeAccessFields != null && !"".equals(writeAccessFields)) {
895 String[] stringArrayList = writeAccessFields.split(",");
896 for (String aentry : stringArrayList) {
897 vAccessFieldList.add(aentry);
898 }
899 }
900
901
902 for (String aentry : vAccessFieldList) {
903
904 List vEntryList = itemCol.getItemValue(aentry);
905 for (Object avalue : vEntryList) {
906 if (avalue != null && !"".equals(avalue.toString())) {
907
908 WriteAccess newItem = new WriteAccess(avalue.toString());
909 manager.persist(newItem);
910 aEntity.getWriteAccessList().add(newItem);
911 }
912 }
913 }
914 }
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929 private void updateReadAccessList(ItemCollection itemCol, Entity aEntity) {
930 List<String> vAccessFieldList = new ArrayList<String>();
931
932
933 for (ReadAccess aItem : aEntity.getReadAccessList())
934 manager.remove(aItem);
935 aEntity.getReadAccessList().clear();
936
937
938 vAccessFieldList.add("$readAccess");
939
940
941 if (readAccessFields != null && !"".equals(readAccessFields)) {
942 String[] stringArrayList = readAccessFields.split(",");
943 for (String aentry : stringArrayList) {
944 vAccessFieldList.add(aentry);
945 }
946 }
947
948
949 for (String aentry : vAccessFieldList) {
950
951 List vEntryList = itemCol.getItemValue(aentry);
952 for (Object avalue : vEntryList) {
953 if (avalue != null && !"".equals(avalue.toString())) {
954
955 ReadAccess newItem = new ReadAccess(avalue.toString());
956 manager.persist(newItem);
957 aEntity.getReadAccessList().add(newItem);
958 }
959 }
960 }
961 }
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994 private void explodeEntity(ItemCollection itemCol, Entity aEntity,
995 Collection<EntityIndex> entityIndexCache) {
996
997
998 if (disbaleOptimisticLocking) {
999 itemCol.removeItem("$Version");
1000 } else if (itemCol.hasItem("$Version")) {
1001
1002 aEntity.setVersion(itemCol.getItemValueInteger("$Version"));
1003 }
1004
1005
1006 for (TextItem aItem : aEntity.getTextItems())
1007 manager.remove(aItem);
1008 aEntity.getTextItems().clear();
1009
1010
1011 for (CalendarItem aItem : aEntity.getCalendarItems())
1012 manager.remove(aItem);
1013 aEntity.getCalendarItems().clear();
1014
1015
1016 for (IntegerItem aItem : aEntity.getIntegerItems())
1017 manager.remove(aItem);
1018 aEntity.getIntegerItems().clear();
1019
1020
1021 for (DoubleItem aItem : aEntity.getDoubleItems())
1022 manager.remove(aItem);
1023 aEntity.getDoubleItems().clear();
1024
1025
1026 for (EntityIndex index : entityIndexCache) {
1027 if (index.getTyp() == EntityIndex.TYP_TEXT) {
1028
1029 List valueList = itemCol.getItemValue(index.getName());
1030 for (Object asingleValue : valueList) {
1031 TextItem newItem = new TextItem(index.getName(),
1032 asingleValue.toString());
1033 manager.persist(newItem);
1034 aEntity.getTextItems().add(newItem);
1035 }
1036
1037 itemCol.removeItem(index.getName());
1038 continue;
1039 }
1040 if (index.getTyp() == EntityIndex.TYP_INT) {
1041
1042 List valueList = itemCol.getItemValue(index.getName());
1043
1044 boolean bClassCastException = false;
1045 Vector vInvalidValues = new Vector();
1046 for (Object asingleValue : valueList) {
1047 try {
1048 IntegerItem newItem = new IntegerItem(index.getName(),
1049 (Integer) asingleValue);
1050 manager.persist(newItem);
1051 aEntity.getIntegerItems().add(newItem);
1052 } catch (ClassCastException cce) {
1053 bClassCastException = true;
1054 logger.warning("explodeEntity - " + index.getName()
1055 + " TYP_INT: " + cce.getMessage());
1056 vInvalidValues.add(asingleValue);
1057 }
1058 }
1059
1060 if (!bClassCastException)
1061 itemCol.removeItem(index.getName());
1062 else
1063 itemCol.replaceItemValue(index.getName(), vInvalidValues);
1064 continue;
1065 }
1066
1067 if (index.getTyp() == EntityIndex.TYP_DOUBLE) {
1068
1069 List valueList = itemCol.getItemValue(index.getName());
1070
1071 boolean bClassCastException = false;
1072 List vInvalidValues = new Vector();
1073 for (Object asingleValue : valueList) {
1074 try {
1075 DoubleItem newItem = new DoubleItem(index.getName(),
1076 (Double) asingleValue);
1077 manager.persist(newItem);
1078 aEntity.getDoubleItems().add(newItem);
1079 } catch (ClassCastException cce) {
1080 bClassCastException = true;
1081 logger.warning("explodeEntity - " + index.getName()
1082 + " TYP_DOUBLE: " + cce.getMessage());
1083 vInvalidValues.add(asingleValue);
1084 }
1085 }
1086
1087 if (!bClassCastException)
1088 itemCol.removeItem(index.getName());
1089 else
1090 itemCol.replaceItemValue(index.getName(), vInvalidValues);
1091 continue;
1092 }
1093
1094 if (index.getTyp() == EntityIndex.TYP_CALENDAR) {
1095
1096 List valueList = itemCol.getItemValue(index.getName());
1097
1098 boolean bClassCastException = false;
1099 Vector vInvalidValues = new Vector();
1100 for (Object asingleValue : valueList) {
1101 try {
1102 if (asingleValue instanceof java.util.Date) {
1103 Calendar cal = Calendar.getInstance();
1104 cal.setTime((java.util.Date) asingleValue);
1105 asingleValue = cal;
1106 }
1107
1108 CalendarItem newItem = new CalendarItem(
1109 index.getName(), (Calendar) asingleValue);
1110 manager.persist(newItem);
1111 aEntity.getCalendarItems().add(newItem);
1112 } catch (ClassCastException cce) {
1113 bClassCastException = true;
1114 logger.warning("explodeEntity - " + index.getName()
1115 + " TYP_CALENDAR: " + cce.getMessage());
1116 vInvalidValues.add(asingleValue);
1117 }
1118 }
1119
1120 if (!bClassCastException)
1121 itemCol.removeItem(index.getName());
1122 else
1123 itemCol.replaceItemValue(index.getName(), vInvalidValues);
1124 continue;
1125 }
1126
1127 logger.warning(" explodeEntity - " + index.getName()
1128 + " Indextype:" + index.getTyp() + " unknown!");
1129
1130 }
1131
1132 }
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167 @SuppressWarnings("unchecked")
1168 private ItemCollection implodeEntity(Entity aEntity) {
1169
1170 ItemCollection itemCollection = new ItemCollection();
1171
1172
1173 try {
1174 itemCollection.replaceItemValue("$isauthor",
1175 isCallerAuthor(aEntity));
1176 } catch (Exception e1) {
1177 }
1178
1179
1180
1181
1182
1183
1184
1185 for (TextItem textItem : aEntity.getTextItems()) {
1186 try {
1187 List vValue = itemCollection.getItemValue(textItem.itemName);
1188 vValue.add(textItem.itemValue);
1189 itemCollection.replaceItemValue(textItem.itemName, vValue);
1190 } catch (Exception e) {
1191 logger.warning("[EntityService] could not implode ItemValue: "
1192 + textItem.itemName);
1193 }
1194 }
1195
1196
1197 for (IntegerItem ii : aEntity.getIntegerItems()) {
1198 try {
1199 List vValue = itemCollection.getItemValue(ii.itemName);
1200 vValue.add(ii.itemValue);
1201 itemCollection.replaceItemValue(ii.itemName, vValue);
1202 } catch (Exception e) {
1203 logger.warning("[EntityService] could not implode ItemValue: "
1204 + ii.itemName);
1205 }
1206 }
1207
1208
1209 for (DoubleItem di : aEntity.getDoubleItems()) {
1210 try {
1211 List vValue = itemCollection.getItemValue(di.itemName);
1212 vValue.add(di.itemValue);
1213 itemCollection.replaceItemValue(di.itemName, vValue);
1214 } catch (Exception e) {
1215 logger.warning("[EntityService] could not implode ItemValue: "
1216 + di.itemName);
1217 }
1218 }
1219
1220
1221 for (CalendarItem ci : aEntity.getCalendarItems()) {
1222 try {
1223 List vValue = itemCollection.getItemValue(ci.itemName);
1224
1225 vValue.add(ci.itemValue.getTime());
1226 itemCollection.replaceItemValue(ci.itemName, vValue);
1227 } catch (Exception e) {
1228 logger.warning("[EntityService] could not implode ItemValue: "
1229 + ci.itemName);
1230 }
1231 }
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 Map activeMap = aEntity.getData();
1247 if (activeMap != null) {
1248
1249 Iterator iter = activeMap.entrySet().iterator();
1250 while (iter.hasNext()) {
1251 Map.Entry mapEntry = (Map.Entry) iter.next();
1252
1253 String activePropertyName = mapEntry.getKey().toString();
1254 if (!itemCollection.hasItem(activePropertyName)) {
1255
1256
1257
1258 Vector activePropertyValue = (Vector) mapEntry.getValue();
1259
1260 Vector detachePropertyValue = new Vector();
1261
1262 detachePropertyValue.addAll(activePropertyValue);
1263
1264 try {
1265 itemCollection.replaceItemValue(activePropertyName,
1266 detachePropertyValue);
1267 } catch (Exception e) {
1268
1269 logger.warning("[EntityService] Warning implodeEntity - "
1270 + e.getMessage());
1271 }
1272 }
1273
1274
1275 }
1276 }
1277
1278
1279
1280 if (!disbaleOptimisticLocking) {
1281 itemCollection.replaceItemValue("$Version", aEntity.getVersion());
1282 }
1283
1284 return itemCollection;
1285 }
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322 private String optimizeQuery(String aQuery) throws InvalidAccessException {
1323
1324 StringBuffer nameListBuf = new StringBuffer();
1325 String nameList = "";
1326 aQuery = aQuery.trim();
1327 StringTokenizer st = new StringTokenizer(aQuery);
1328
1329 if (st.countTokens() < 5)
1330 throw new InvalidAccessException(
1331 "[EntityService] Invalid query format: " + aQuery);
1332
1333
1334 st.nextToken();
1335 String sDistinct = st.nextToken();
1336 if (!"distinct".equals(sDistinct.toLowerCase().trim())) {
1337
1338 int iDisPos = aQuery.toLowerCase().indexOf("select") + 6;
1339 aQuery = aQuery.substring(0, iDisPos) + " DISTINCT"
1340 + aQuery.substring(iDisPos);
1341 }
1342
1343
1344 if (ctx.isCallerInRole(ACCESSLEVEL_MANAGERACCESS))
1345 return aQuery;
1346
1347
1348 List<String> auserNameList = getUserNameList();
1349 for (String auserName : auserNameList) {
1350 nameListBuf.append(",'" + auserName + "'");
1351 }
1352
1353 nameListBuf.deleteCharAt(0);
1354
1355 nameList = nameListBuf.toString();
1356
1357 logger.fine("Optimized NameList = " + nameList);
1358
1359
1360
1361 int iPos = 0;
1362
1363 iPos = aQuery.toLowerCase().indexOf("join");
1364 if (iPos == -1)
1365
1366 iPos = aQuery.toLowerCase().indexOf("where");
1367 if (iPos == -1)
1368
1369 iPos = aQuery.toLowerCase().indexOf("order by");
1370
1371 String firstPart;
1372 if (iPos == -1)
1373 firstPart = aQuery;
1374 else
1375 firstPart = aQuery.substring(0, iPos - 1);
1376
1377 firstPart = firstPart.trim();
1378
1379 iPos = firstPart.length();
1380
1381 String identifier = null;
1382 identifier = firstPart.substring(firstPart.lastIndexOf(' ')).trim();
1383
1384
1385 if (identifier.endsWith(","))
1386 identifier = identifier.substring(0, identifier.length() - 1);
1387
1388
1389 String aNewQuery = aQuery.substring(0, iPos);
1390 aNewQuery += " LEFT JOIN " + identifier + ".readAccessList access807 ";
1391 aNewQuery += aQuery.substring(iPos);
1392
1393 aQuery = aNewQuery.trim();
1394
1395 int iWherePos = aQuery.toLowerCase().indexOf("where");
1396 if (iWherePos > -1) {
1397
1398 aNewQuery = aQuery.substring(0, iWherePos + 5)
1399 + " (access807.value IS NULL OR access807.value IN ("
1400 + nameList + ")) AND " + aQuery.substring(iWherePos + 6);
1401 aQuery = aNewQuery;
1402
1403 } else {
1404
1405 int iOrderPos = aQuery.toLowerCase().indexOf("order by");
1406 if (iOrderPos > -1)
1407 aNewQuery = aQuery.substring(0, iOrderPos - 1)
1408 + " WHERE (access807.value IS NULL OR access807.value IN ("
1409 + nameList + ")) " + aQuery.substring(iOrderPos);
1410 else
1411 aNewQuery = aQuery
1412 + " WHERE (access807.value IS NULL OR access807.value IN("
1413 + nameList + ")) ";
1414
1415 aQuery = aNewQuery;
1416 }
1417
1418 logger.fine("Optimized Query=" + aQuery);
1419 return aQuery;
1420 }
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433 private void updateAllEntityIndexFields(Collection<Entity> entityList,
1434 String newIndexField) {
1435 long count = 0;
1436
1437
1438 Collection<EntityIndex> entityIndexCache = getIndices();
1439
1440 Iterator<Entity> iter = entityList.iterator();
1441 while (iter.hasNext()) {
1442 Entity activeEntity = iter.next();
1443 ItemCollection slimItemcol = new ItemCollection(
1444 activeEntity.getData());
1445
1446 if (newIndexField == null || slimItemcol.hasItem(newIndexField)) {
1447
1448 try {
1449
1450
1451
1452
1453
1454
1455
1456
1457 ItemCollection itemcol = implodeEntity(activeEntity);
1458
1459 ItemCollection slimItemCollection = new ItemCollection(
1460 itemcol.getAllItems());
1461
1462
1463 updateReadAccessList(slimItemCollection, activeEntity);
1464 updateWriteAccessList(slimItemCollection, activeEntity);
1465
1466 explodeEntity(slimItemCollection, activeEntity,
1467 entityIndexCache);
1468
1469
1470 activeEntity.setData(slimItemCollection.getAllItems());
1471
1472 } catch (Exception merex) {
1473 logger.info("[EntityServiceBean] Error updateAllEntityIndexFields for Entity : "
1474 + activeEntity.getId());
1475 merex.printStackTrace();
1476
1477 }
1478 count++;
1479 }
1480 }
1481 logger.info("[EntityServiceBean] " + count + " effective updates");
1482 }
1483
1484 }