1
18 package org.torweg.pulse.component.core.accesscontrol.attributes.admin;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.TreeMap;
25
26 import net.sf.json.JSONArray;
27 import net.sf.json.JSONObject;
28
29 import org.hibernate.Criteria;
30 import org.hibernate.Session;
31 import org.hibernate.Transaction;
32 import org.hibernate.criterion.Criterion;
33 import org.hibernate.criterion.Order;
34 import org.hibernate.criterion.Restrictions;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.torweg.pulse.accesscontrol.Role;
38 import org.torweg.pulse.accesscontrol.attributes.AbstractAttribute;
39 import org.torweg.pulse.accesscontrol.attributes.AbstractTypedCheck;
40 import org.torweg.pulse.accesscontrol.attributes.AttributeFactory;
41 import org.torweg.pulse.accesscontrol.attributes.AttributeRegistry;
42 import org.torweg.pulse.accesscontrol.attributes.BundleAttribute;
43 import org.torweg.pulse.annotations.Action;
44 import org.torweg.pulse.annotations.Groups;
45 import org.torweg.pulse.annotations.Permission;
46 import org.torweg.pulse.annotations.RequireToken;
47 import org.torweg.pulse.bundle.Bundle;
48 import org.torweg.pulse.bundle.Result;
49 import org.torweg.pulse.component.core.accesscontrol.admin.AbstractAccessControlEditor;
50 import org.torweg.pulse.configuration.ConfigBean;
51 import org.torweg.pulse.configuration.DeprecatedConfigurable;
52 import org.torweg.pulse.invocation.lifecycle.Lifecycle;
53 import org.torweg.pulse.service.PulseException;
54 import org.torweg.pulse.service.event.JSONOutputEvent;
55 import org.torweg.pulse.service.event.XSLTOutputEvent;
56 import org.torweg.pulse.service.request.ServiceRequest;
57 import org.torweg.pulse.util.adminui.JSONCommunicationUtils;
58 import org.torweg.pulse.util.adminui.RegistryEditorResult;
59 import org.torweg.pulse.util.entity.Node;
60
61
67 public class AttributeRegistryEditor extends AbstractAccessControlEditor
68 implements DeprecatedConfigurable {
69
70
73 private static final Logger LOGGER = LoggerFactory
74 .getLogger(AttributeRegistryEditor.class);
75
76
79 private AttributeRegistryEditorConfig config;
80
81
89 @RequireToken
90 @Action(value = "attributeRegistryTreeInit", generate = true)
91 @Permission("useAttributeRegistry")
92 @Groups(values = { "AttributeRegistryAdministrator" })
93 public final Result initEditor(final Bundle bundle,
94 final ServiceRequest request) {
95 AttributeRegistryEditorResult result = new AttributeRegistryEditorResult();
96 XSLTOutputEvent event = new XSLTOutputEvent(this.config.getAjaxXSL());
97 event.setStopEvent(true);
98 request.getEventManager().addEvent(event);
99 return result;
100
101 }
102
103
110 @RequireToken
111 @Action(value = "browseAttributeRegistryEdit", generate = true)
112 @Permission("browseAttributeRegistryEdit")
113 @Groups(values = { "AttributeRegistryAdministrator" })
114 public final Result browseAttributeRegistryEdit(final ServiceRequest request) {
115
116 return browseAttributeRegistry(request, true);
117
118 }
119
120
127 @RequireToken
128 @Action(value = "browseAttributeRegistrySelect", generate = true)
129 @Permission("browseAttributeRegistrySelect")
130 @Groups(values = { "AttributeRegistryAdministrator" })
131 public final Result browseAttributeRegistrySelect(
132 final ServiceRequest request) {
133
134 return browseAttributeRegistry(request, false);
135
136 }
137
138
149 private Result browseAttributeRegistry(final ServiceRequest request,
150 final boolean withEditURLs) {
151 String nodeid = request.getCommand().getParameter("node")
153 .getFirstValue();
154
155 Session s = Lifecycle.getHibernateDataSource().createNewSession();
157 Transaction tx = s.beginTransaction();
158 JSONArray attributeNodes = new JSONArray();
159
160 try {
161
162 if (nodeid.length() > 5 && nodeid.substring(0, 6).equals("source")) {
163
164 attributeNodes = processAttributeRegistryRoot(request, s,
166 withEditURLs);
167
168 } else {
169
170 Node attributeNode = (Node) s.get(Node.class,
172 Long.parseLong(nodeid));
173
174 List<? extends Node> nodeList = (List<? extends Node>) attributeNode
175 .getChildren();
176
177
180 List<JSONObject> jsonNodeList = buildNodeList(request,
181 nodeList, withEditURLs);
182
183 attributeNodes.addAll(jsonNodeList);
184
185 }
186
187 tx.commit();
188
189 } catch (Exception e) {
190 tx.rollback();
191 throw new PulseException(
192 "Core.AttributeRegistry.browseAttributeRegistryEdit.failed: "
193 + e.getLocalizedMessage(), e);
194 } finally {
195 s.close();
196 }
197
198 request.getEventManager().addEvent(new JSONOutputEvent(attributeNodes));
200
201 return null;
202 }
203
204
227 private JSONArray processAttributeRegistryRoot(
228 final ServiceRequest request, final Session s,
229 final boolean withEditURLs) {
230
231 String bundleString = null;
234 if (request.getCommand().getParameter("bundle") != null) {
235 bundleString = request.getCommand().getParameter("bundle")
236 .getFirstValue();
237 }
238
239 AttributeRegistry attributeRegistry = (AttributeRegistry) s.get(
241 AttributeRegistry.class, 1L);
242
243 List<BundleAttribute> nodeList = new ArrayList<BundleAttribute>(
244 attributeRegistry.getRootNodes());
245
246 Map<String, JSONObject> nodeMap = new TreeMap<String, JSONObject>();
247
248 for (BundleAttribute bundleAttribute : nodeList) {
250
251 JSONObject nodeJSONObject = bundleAttribute.toJSON();
252 nodeJSONObject.put("draggable", false);
253 nodeJSONObject.put("allowDrop", true);
254
255 if (withEditURLs) {
259 nodeJSONObject.put(
260 "createURL",
261 request.getCommand()
262 .createCopy(false)
263 .setAction("createAttributeRegistryEdit")
264 .addHttpParameter("id",
265 bundleAttribute.getId().toString())
266 .toCommandURL(request));
267 }
268
269 if (bundleString == null) {
271 nodeMap.put(bundleAttribute.getBundle().getName(),
272 nodeJSONObject);
273 } else {
274 if (bundleAttribute.getBundle().getName()
276 .equalsIgnoreCase(bundleString)) {
277 nodeMap.put(bundleAttribute.getBundle().getName(),
278 nodeJSONObject);
279 }
280 }
281 }
282
283 JSONArray rootNodes = new JSONArray();
284 rootNodes.addAll(nodeMap.values());
285 return rootNodes;
286
287 }
288
289
302
303 private List<JSONObject> buildNodeList(final ServiceRequest request,
308 final List<? extends Node> nodeList, final boolean withEditURLs) {
309
310 List<JSONObject> jsonNodeList = new ArrayList<JSONObject>();
312
313 for (Node n : nodeList) {
314 if (n != null) {
315
316 LOGGER.debug(" listing node: {}", n.getId());
317
318 JSONObject nodeJSONObject = n.toJSON();
319 nodeJSONObject.put("draggable", true);
320 nodeJSONObject.put("allowDrop", true);
321
322
325 if (withEditURLs) {
326
327 nodeJSONObject.put(
330 "createURL",
331 request.getCommand()
332 .createCopy(false)
333 .setAction("createAttributeRegistryEdit")
334 .addHttpParameter("id",
335 n.getId().toString())
336 .toCommandURL(request));
337
338 if (!((AbstractAttribute<?>) n).isSystemAttribute()) {
340 nodeJSONObject.put(
341 "editURL",
342 request.getCommand()
343 .createCopy(false)
344 .setAction("editAttributeRegistryEdit")
345 .addHttpParameter("id",
346 n.getId().toString())
347 .toCommandURL(request));
348 }
349
350 nodeJSONObject.put(
353 "deleteURL",
354 request.getCommand()
355 .createCopy(false)
356 .setAction("deleteAttributeRegistryEdit")
357 .addHttpParameter("id",
358 n.getId().toString())
359 .toCommandURL(request));
360
361 nodeJSONObject.put(
364 "moveURL",
365 request.getCommand()
366 .createCopy(false)
367 .setAction("moveAttributeRegistryEdit")
368 .addHttpParameter("id",
369 n.getId().toString())
370 .toCommandURL(request));
371
372 }
373
374
379 jsonNodeList.add(nodeJSONObject);
380
381 } else {
382 LOGGER.debug(" NULL VALUE");
383 }
384 }
385
386 return jsonNodeList;
388 }
389
390
403 @RequireToken
404 @Action(value = "createAttributeRegistryEdit", generate = true)
405 @Permission("editAttributeRegistry")
406 @Groups(values = { "AttributeRegistryAdministrator" })
407 public final Result create(final Bundle bundle, final ServiceRequest request) {
408
409 Session s = Lifecycle.getHibernateDataSource().createNewSession();
411 Transaction tx = s.beginTransaction();
412 RegistryEditorResult result = new RegistryEditorResult();
413
414 try {
415
416 for (Class<AbstractAttribute<?>> clazz : Lifecycle
417 .getAttributeFactory().getAttributes()) {
418
419 result.setCreateURL(
420 clazz.getCanonicalName(),
421 request.getCommand()
422 .createCopy()
423 .setAction(
424 "createAttributeAttributeRegistryEdit")
425 .addHttpParameter(
426 "clazz",
427 clazz.getCanonicalName().replace('.',
428 '_')).toCommandURL(request));
429
430 }
431
432 tx.commit();
433
434 } catch (Exception e) {
435 tx.rollback();
436 throw new PulseException("AttributeRegistryEditor.create.failed: "
437 + e.getLocalizedMessage(), e);
438 } finally {
439 s.close();
440 }
441
442 XSLTOutputEvent event = new XSLTOutputEvent(
444 this.config.getAjaxCreateXSL());
445 event.setStopEvent(true);
446 request.getEventManager().addEvent(event);
447 return result;
448 }
449
450
461 @RequireToken
462 @Action(value = "createAttributeAttributeRegistryEdit", generate = true)
463 @Permission("editAttributeRegistry")
464 @Groups(values = { "AttributeRegistryAdministrator" })
465 public final void createAttribute(final Bundle bundle,
466 final ServiceRequest request) {
467
468 String clazz = request.getCommand().getParameter("clazz")
470 .getFirstValue();
471 Long id = Long.parseLong(request.getCommand().getParameter("id")
472 .getFirstValue());
473 String name = request.getCommand().getParameter("name").getFirstValue();
474
475 LOGGER.debug(
476 "\n---> request.clazz: {}\n---> request.id: {}\n---> request.name: {}",
477 new Object[] { clazz, id, name });
478
479 Session s = Lifecycle.getHibernateDataSource().createNewSession();
481 Transaction tx = s.beginTransaction();
482 AbstractAttribute<?> newAttribute = null;
483
484 try {
485
486 Node parentAttribute = (Node) s.get(Node.class, id);
489
490 AttributeFactory factory = Lifecycle.getAttributeFactory();
492 for (Class<AbstractAttribute<?>> knownClazz : factory
493 .getAttributes()) {
494 if (knownClazz.getCanonicalName().replace('.', '_')
495 .equals(clazz)) {
496 newAttribute = factory.getAttribute(knownClazz, name);
497 }
498 }
499
500 if (newAttribute != null) {
502 parentAttribute.addChild(newAttribute);
503 }
504
505 s.saveOrUpdate(parentAttribute);
507
508 tx.commit();
509
510 } catch (Exception e) {
511 tx.rollback();
512 throw new PulseException(
513 "AttributeRegistryEditor.createAttribute.failed: "
514 + e.getLocalizedMessage(), e);
515 } finally {
516 s.close();
517 }
518
519 if (newAttribute != null) {
521 JSONCommunicationUtils.jsonSuccessMessage(request, "id",
522 newAttribute.getId().toString());
523 } else {
524 JSONCommunicationUtils.jsonErrorMessage(request);
525 }
526 }
527
528
541 @RequireToken
542 @Action(value = "editAttributeRegistryEdit", generate = true)
543 @Permission("editAttributeRegistry")
544 @Groups(values = { "AttributeRegistryAdministrator" })
545 public final AttributeRegistryEditorResult edit(final Bundle bundle,
546 final ServiceRequest request) {
547
548
550 Long id = Long.parseLong(request.getCommand().getParameter("id")
552 .getFirstValue());
553
554 Session s = Lifecycle.getHibernateDataSource().createNewSession();
556 Transaction tx = s.beginTransaction();
557 AttributeRegistryEditorResult result = new AttributeRegistryEditorResult();
558
559 try {
560
561 AbstractAttribute<?> attribute = (AbstractAttribute<?>) s.get(
563 AbstractAttribute.class, id);
564
565 result.setAttribute(attribute);
567
568 tx.commit();
569
570 } catch (Exception e) {
571 tx.rollback();
572 throw new PulseException("AttributeRegistryEditor.edit.failed: "
573 + e.getLocalizedMessage(), e);
574 } finally {
575 s.close();
576 }
577
578 XSLTOutputEvent event = new XSLTOutputEvent(
580 this.config.getAjaxEditorsXSL());
581 event.setStopEvent(true);
582 request.getEventManager().addEvent(event);
583 return result;
584 }
585
586
597 @RequireToken
598 @Action(value = "deleteAttributeRegistryEdit", generate = true)
599 @Permission("editAttributeRegistry")
600 @Groups(values = { "AttributeRegistryAdministrator" })
601 public final void delete(final Bundle bundle, final ServiceRequest request) {
602
603
605 Long id = Long.parseLong(request.getCommand().getParameter("id")
607 .getFirstValue());
608
609 Session s = Lifecycle.getHibernateDataSource().createNewSession();
611 Transaction tx = s.beginTransaction();
612
613 try {
614
615 AbstractAttribute<?> deleteAttribute = (AbstractAttribute<?>) s
617 .get(AbstractAttribute.class, id);
618
619 Node parentAttribute = deleteAttribute.getParent();
621
622 parentAttribute.removeChild(deleteAttribute);
624
625 s.saveOrUpdate(parentAttribute);
627 s.delete((AbstractAttribute<?>) s.get(AbstractAttribute.class, id));
628
629 tx.commit();
630
631 } catch (Exception e) {
632 tx.rollback();
633 throw new PulseException("AttributeRegistryEditor.delete.failed: "
634 + e.getLocalizedMessage(), e);
635 } finally {
636 s.close();
637 }
638
639 JSONCommunicationUtils.jsonSuccessMessage(request);
641 }
642
643
652 @RequireToken
653 @Action(value = "loadAdminEditRolesAttributeRegistryEditor", generate = true)
654 @Permission("editAttributeRegistry")
655 @Groups(values = { "AttributeRegistryAdministrator" })
656 public final void loadAdminEditRoles(final ServiceRequest request) {
657 loadRoles(request, 1);
658 }
659
660
669 @RequireToken
670 @Action(value = "loadAdminViewRolesAttributeRegistryEditor", generate = true)
671 @Permission("editAttributeRegistry")
672 @Groups(values = { "AttributeRegistryAdministrator" })
673 public final void loadAdminViewRoles(final ServiceRequest request) {
674 loadRoles(request, 2);
675 }
676
677
686 @RequireToken
687 @Action(value = "loadSelfEditRolesAttributeRegistryEditor", generate = true)
688 @Permission("editAttributeRegistry")
689 @Groups(values = { "AttributeRegistryAdministrator" })
690 public final void loadSelfEditRoles(final ServiceRequest request) {
691 loadRoles(request, 3);
692 }
693
694
703 @RequireToken
704 @Action(value = "loadSelfViewRolesAttributeRegistryEditor", generate = true)
705 @Permission("editAttributeRegistry")
706 @Groups(values = { "AttributeRegistryAdministrator" })
707 public final void loadSelfViewRoles(final ServiceRequest request) {
708 loadRoles(request, 4);
709 }
710
711
721 @RequireToken
722 @Action(value = "loadTriggeredRolesAttributeRegistryEditor", generate = true)
723 @Permission("editAttributeRegistry")
724 @Groups(values = { "AttributeRegistryAdministrator" })
725 public final void loadTriggeredRoles(final ServiceRequest request) {
726 loadRoles(request, 5);
727 }
728
729
737 @SuppressWarnings("unchecked")
738 private void loadRoles(final ServiceRequest request, final int mode) {
739
740 Long attributeId = Long.parseLong(request.getCommand()
742 .getParameter("id").getFirstValue());
743
744 boolean unasoc = false;
746 if (request.getCommand().getParameter("unasoc") != null) {
747 unasoc = Boolean.parseBoolean(request.getCommand()
748 .getParameter("unasoc").getFirstValue());
749 }
750
751 Session s = Lifecycle.getHibernateDataSource().createNewSession();
753 Transaction tx = s.beginTransaction();
754 JSONArray data = new JSONArray();
755
756 try {
757
758 AbstractAttribute<?> attribute = (AbstractAttribute<?>) s.get(
760 AbstractAttribute.class, attributeId);
761
762 Criteria criteria = buildLoadRolesCriteria(request, unasoc, s,
763 attribute, mode);
764
765 if (criteria != null) {
766 long total = processCriteriaForPaging(request, criteria);
767
768 List<Role> roles = (List<Role>) criteria.addOrder(
770 Order.asc("name").ignoreCase()).list();
771
772 boolean totalSet = false;
774 for (Role role : roles) {
775 JSONObject roleObj = role.toJSON();
776 if (!totalSet) {
777 roleObj.put("total", total);
778 totalSet = true;
779 }
780 roleObj.put("initialAsoc", !unasoc);
781 data.add(roleObj);
782 }
783
784 }
785
786 tx.commit();
787
788 } catch (Exception e) {
789 tx.rollback();
790 throw new PulseException(
791 "AttributeRegistryEditor.loadRoles.failed: "
792 + e.getLocalizedMessage(), e);
793 } finally {
794 s.close();
795 }
796
797 request.getEventManager().addEvent(new JSONOutputEvent(data));
799 }
800
801
827 private Criteria buildLoadRolesCriteria(final ServiceRequest request,
828 final boolean unasoc, final Session s,
829 final AbstractAttribute<?> attribute, final int mode) {
830
831 Criterion nameCriterion = buildCriterionFromRequest(request, "filter",
832 "name");
833
834 List<Long> ids = new ArrayList<Long>();
836 if (mode == 1) {
837 for (Role r : (Set<Role>) attribute.getAdminEditRoles()) {
838 ids.add(r.getId());
839 }
840 } else if (mode == 2) {
841 for (Role r : (Set<Role>) attribute.getAdminViewRoles()) {
842 ids.add(r.getId());
843 }
844 } else if (mode == 3) {
845 for (Role r : (Set<Role>) attribute.getSelfEditRoles()) {
846 ids.add(r.getId());
847 }
848 } else if (mode == 4) {
849 for (Role r : (Set<Role>) attribute.getSelfViewRoles()) {
850 ids.add(r.getId());
851 }
852 } else if (mode == 5) {
853 for (Role r : (Set<Role>) attribute.getTriggeredRoles()) {
854 ids.add(r.getId());
855 }
856 }
857
858 Criteria criteria = null;
859 if (unasoc) {
860
861 if (ids.isEmpty()) {
863 criteria = s.createCriteria(Role.class);
864 } else {
865 criteria = s.createCriteria(Role.class).add(
866 Restrictions.not(Restrictions.in("id", ids)));
867 }
868
869 } else {
870
871 if (!ids.isEmpty()) {
873 criteria = s.createCriteria(Role.class).add(
874 Restrictions.in("id", ids));
875 }
876
877 }
878
879 if (criteria != null && nameCriterion != null) {
881 criteria.add(nameCriterion);
882 }
883 return criteria;
884 }
885
886
894 @RequireToken
895 @Action(value = "saveAttributeRegistryEditor", generate = true)
896 @Permission("editAttributeRegistry")
897 @Groups(values = { "AttributeRegistryAdministrator" })
898 public final void save(final Bundle bundle, final ServiceRequest request) {
899
900 Long id = Long.parseLong(request.getCommand().getParameter("id")
902 .getFirstValue());
903
904 Session s = Lifecycle.getHibernateDataSource().createNewSession();
906 Transaction tx = s.beginTransaction();
907 JSONObject error = null;
909
910 try {
911
912 AbstractAttribute<?> attribute = (AbstractAttribute<?>) s.get(
914 AbstractAttribute.class, id);
915
916 attribute.updateSettingsFromCommand(request.getCommand(), s);
918
919 if (attribute.getCheck() != null) {
921 try {
922 attribute.getCheck().setCheckFromCommand(
923 request.getCommand());
924 } catch (Exception e) {
925 LOGGER.debug(
926 "suppressed Exception and returned JSON-error-message instead for:"
927 + e.getLocalizedMessage(), e);
928 error = new JSONObject();
929 error.put("e", "invalidCheck");
930 }
931 }
932
933 if (error == null) {
934 if (request.getCommand().getParameter("isRequired") != null) {
936 Boolean isRequired = Boolean.valueOf(request.getCommand()
937 .getParameter("isRequired").getFirstValue());
938 if (isRequired != attribute.isRequired()) {
939 attribute.setRequired(isRequired);
940 }
941 }
942
943 processRoles(request, s, attribute, 1);
945
946 processRoles(request, s, attribute, 2);
948
949 processRoles(request, s, attribute, 3);
951
952 processRoles(request, s, attribute, 4);
954
955 processRoles(request, s, attribute, 5);
957
958 s.saveOrUpdate(attribute);
960 }
961
962 tx.commit();
963
964 } catch (Exception e) {
965 tx.rollback();
966 throw new PulseException(
967 "PermissionEditor.doSavePermissionEditor.failed: "
968 + e.getLocalizedMessage(), e);
969 } finally {
970 s.close();
971 }
972
973 if (error == null) {
975 JSONCommunicationUtils.jsonSuccessMessage(request);
976 } else {
977 JSONCommunicationUtils.jsonErrorMessage(request, error);
978 }
979 }
980
981
1000 private void processRoles(final ServiceRequest request, final Session s,
1001 final AbstractAttribute<?> attribute, final int mode) {
1002
1003 if (mode == 1) {
1004
1006 List<Object> roles = retrieveAssorterObjects("addAdminEditRoles",
1008 request, "Role", s);
1009 for (Object o : roles) {
1010 attribute.addAdminEditRole((Role) o);
1011 }
1012
1013 roles = retrieveAssorterObjects("remAdminEditRoles", request,
1015 "Role", s);
1016 for (Object o : roles) {
1017 attribute.removeAdminEditRole((Role) o);
1018 }
1019 } else if (mode == 2) {
1020
1022 List<Object> roles = retrieveAssorterObjects("addAdminViewRoles",
1024 request, "Role", s);
1025 for (Object o : roles) {
1026 attribute.addAdminViewRole((Role) o);
1027 }
1028
1029 roles = retrieveAssorterObjects("remAdminViewRoles", request,
1031 "Role", s);
1032 for (Object o : roles) {
1033 attribute.removeAdminViewRole((Role) o);
1034 }
1035 } else if (mode == 3) {
1036
1038 List<Object> roles = retrieveAssorterObjects("addSelfEditRoles",
1040 request, "Role", s);
1041 for (Object o : roles) {
1042 attribute.addSelfEditRole((Role) o);
1043 }
1044
1045 roles = retrieveAssorterObjects("remSelfEditRoles", request,
1047 "Role", s);
1048 for (Object o : roles) {
1049 attribute.removeSelfEditRole((Role) o);
1050 }
1051 } else if (mode == 4) {
1052
1054 List<Object> roles = retrieveAssorterObjects("addSelfViewRoles",
1056 request, "Role", s);
1057 for (Object o : roles) {
1058 attribute.addSelfViewRole((Role) o);
1059 }
1060
1061 roles = retrieveAssorterObjects("remSelfViewRoles", request,
1063 "Role", s);
1064 for (Object o : roles) {
1065 attribute.removeSelfViewRole((Role) o);
1066 }
1067 } else if (mode == 5) {
1068
1070 List<Object> roles = retrieveAssorterObjects("addTriggeredRoles",
1072 request, "Role", s);
1073 for (Object o : roles) {
1074 attribute.addTriggeredRole((Role) o);
1075 }
1076
1077 roles = retrieveAssorterObjects("remTriggeredRoles", request,
1079 "Role", s);
1080 for (Object o : roles) {
1081 attribute.removeTriggeredRole((Role) o);
1082 }
1083 }
1084
1085 }
1086
1087
1104 @RequireToken
1105 @Action(value = "loadChecksAttributeRegistryEditor", generate = true)
1106 @Permission("editAttributeRegistry")
1107 @Groups(values = { "AttributeRegistryAdministrator" })
1108 public final Result loadChecks(final Bundle bundle,
1109 final ServiceRequest request) {
1110
1111 Long id = Long.parseLong(request.getCommand().getParameter("id")
1113 .getFirstValue());
1114
1115 Session s = Lifecycle.getHibernateDataSource().createNewSession();
1117 Transaction tx = s.beginTransaction();
1118 AttributeRegistryEditorResult result = new AttributeRegistryEditorResult();
1119
1120 try {
1121
1122 AbstractAttribute<?> attribute = (AbstractAttribute<?>) s.get(
1124 AbstractAttribute.class, id);
1125
1126 if (attribute.getCheck() == null) {
1128 result.setAttribute(attribute);
1130 result.setAvailableChecks(attribute.getTypedChecks());
1131 } else {
1132 result.setAttribute(attribute);
1134 result.setAttributeCheck(attribute.getCheck());
1135 }
1136
1137 tx.commit();
1138
1139 } catch (Exception e) {
1140 tx.rollback();
1141 throw new PulseException(
1142 "AttributeRegistryEditor.loadChecks.failed: "
1143 + e.getLocalizedMessage(), e);
1144 } finally {
1145 s.close();
1146 }
1147
1148 XSLTOutputEvent event = new XSLTOutputEvent(
1150 this.config.getAjaxChecksXSL());
1151 event.setStopEvent(true);
1152 request.getEventManager().addEvent(event);
1153 return result;
1154
1155 }
1156
1157
1168 @RequireToken
1169 @Action(value = "setCheckAttributeRegistryEditor", generate = true)
1170 @Permission("editAttributeRegistry")
1171 @Groups(values = { "AttributeRegistryAdministrator" })
1172 @SuppressWarnings("unchecked")
1173 public final void setCheck(final Bundle bundle, final ServiceRequest request) {
1174
1175
1177 Long id = Long.parseLong(request.getCommand().getParameter("id")
1179 .getFirstValue());
1180 String clazz = request.getCommand().getParameter("clazz")
1181 .getFirstValue();
1182
1183 Session s = Lifecycle.getHibernateDataSource().createNewSession();
1185 Transaction tx = s.beginTransaction();
1186 boolean removeOldCheck = false;
1187
1188 try {
1189
1190 AbstractAttribute<?> attribute = (AbstractAttribute<?>) s.get(
1192 AbstractAttribute.class, id);
1193
1194 AbstractTypedCheck<?> oldCheck = null;
1196 if (attribute.getCheck() != null) {
1197 oldCheck = attribute.getCheck();
1198 }
1199
1200 if (clazz.equals("")) {
1202 attribute.setCheck(null);
1204 removeOldCheck = true;
1205 } else {
1206 for (Class<AbstractTypedCheck<?>> c : (Set<Class<AbstractTypedCheck<?>>>) attribute
1208 .getTypedChecks()) {
1209 if (c.getCanonicalName().equals(clazz)) {
1210 AbstractTypedCheck<?> check = Lifecycle
1211 .getAttributeFactory().getTypedCheck(c);
1212 attribute.setCheck((AbstractTypedCheck) check);
1214 removeOldCheck = true;
1215 }
1216 }
1217 }
1218
1219 if (removeOldCheck && (oldCheck != null)) {
1221 s.delete(oldCheck);
1222 }
1223
1224 s.saveOrUpdate(attribute);
1226
1227 tx.commit();
1228
1229 } catch (Exception e) {
1230 tx.rollback();
1231 throw new PulseException(
1232 "AttributeRegistryEditor.setCheck.failed: "
1233 + e.getLocalizedMessage(), e);
1234 } finally {
1235 s.close();
1236 }
1237
1238 if (removeOldCheck) {
1240 JSONCommunicationUtils.jsonSuccessMessage(request);
1241 } else {
1242 JSONCommunicationUtils.jsonErrorMessage(request);
1243 }
1244
1245 }
1246
1247
1254 @RequireToken
1255 @Action(value = "moveAttributeRegistryEdit", generate = true)
1256 @Permission("editAttributeRegistry")
1257 @Groups(values = { "AttributeRegistryAdministrator" })
1258 public final void move(final ServiceRequest request) {
1259
1260 Session s = Lifecycle.getHibernateDataSource().createNewSession();
1262 Transaction tx = s.beginTransaction();
1263
1264 int index = -1;
1265 JSONObject error = null;
1267
1268 try {
1269
1270 AbstractAttribute<?> moveNode = (AbstractAttribute<?>) s.get(
1272 AbstractAttribute.class,
1273 Long.parseLong(request.getCommand()
1274 .getParameter("movenodeid").getFirstValue()));
1275
1276 AbstractAttribute<?> newParentNode = null;
1277
1278 if (error == null) {
1279
1280 newParentNode = determineNewParent(request, s);
1282
1283 if (newParentNode == null) {
1286 error = new JSONObject();
1287 error.put("e", "cannotMoveToUndefinedLevel");
1288 }
1289 }
1290
1291 if (error == null) {
1293
1294 if (!request.getCommand().getParameter("insertpoint")
1296 .getFirstValue().equals("append")) {
1297 index = newParentNode
1299 .getChildIndex((AbstractAttribute<?>) s.get(
1300 AbstractAttribute.class,
1301 Long.parseLong(request.getCommand()
1302 .getParameter("targetnodeid")
1303 .getFirstValue())));
1304 if (request.getCommand().getParameter("insertpoint")
1305 .getFirstValue().equals("below")) {
1306 index += 1;
1307 }
1308 }
1309
1310 AbstractAttribute<?> oldParentNode = (AbstractAttribute<?>) moveNode
1312 .getParent();
1313
1314 oldParentNode.removeChild(moveNode);
1316 if (index == -1) {
1317 newParentNode.addChild(moveNode);
1318 } else {
1319 try {
1320 newParentNode.addChild(index, moveNode);
1321 } catch (IndexOutOfBoundsException e) {
1322 newParentNode.addChild(moveNode);
1323 }
1324 }
1325
1326 s.saveOrUpdate(oldParentNode);
1328 s.saveOrUpdate(newParentNode);
1329
1330 tx.commit();
1331 }
1332
1333 } catch (Exception e) {
1334 if (tx != null) {
1335 tx.rollback();
1336 }
1337 throw new PulseException(
1338 "Core.AttributeRegistryEditor.moveAttributeNode.failed: "
1339 + e.getLocalizedMessage(), e);
1340 } finally {
1341 s.close();
1342 }
1343
1344 if (error == null) {
1346 JSONCommunicationUtils.jsonSuccessMessage(request);
1347 } else {
1348 JSONCommunicationUtils.jsonErrorMessage(request, error);
1349 }
1350 }
1351
1352
1363 private AbstractAttribute<?> determineNewParent(
1364 final ServiceRequest request, final Session s) {
1365 try {
1366 if (request.getCommand().getParameter("insertpoint")
1367 .getFirstValue().equals("append")) {
1368 return (AbstractAttribute<?>) s.get(
1371 AbstractAttribute.class,
1372 Long.parseLong(request.getCommand()
1373 .getParameter("targetnodeid").getFirstValue()));
1374 } else {
1375 return (AbstractAttribute<?>) ((AbstractAttribute<?>) s.get(
1378 AbstractAttribute.class,
1379 Long.parseLong(request.getCommand()
1380 .getParameter("targetnodeid").getFirstValue())))
1381 .getParent();
1382 }
1383 } catch (Exception e) {
1384 return null;
1385 }
1386 }
1387
1388
1394 public final void init(final ConfigBean conf) {
1395 this.config = (AttributeRegistryEditorConfig) conf;
1396 }
1397
1398 }
1399