View Javadoc

1   /**
2    * This file Copyright (c) 2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.app.security.dialog.field;
35  
36  import com.vaadin.data.Item;
37  import com.vaadin.ui.AbstractSelect;
38  import com.vaadin.ui.TwinColSelect;
39  import info.magnolia.cms.util.QueryUtil;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.repository.RepositoryConstants;
42  import info.magnolia.ui.admincentral.field.builder.TwinColSelectFieldBuilder;
43  import info.magnolia.ui.model.field.definition.SelectFieldOptionDefinition;
44  import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import javax.jcr.Node;
49  import javax.jcr.NodeIterator;
50  import javax.jcr.Property;
51  import javax.jcr.PropertyIterator;
52  import javax.jcr.RepositoryException;
53  import java.util.ArrayList;
54  import java.util.List;
55  
56  /**
57   * GUI builder for the Group Management field.
58   */
59  public class GroupManagementField extends TwinColSelectFieldBuilder<GroupManagementFieldDefinition> {
60  
61      /**
62       * Internal bean to represent basic group data.
63       */
64      public static class Group {
65          public String name;
66          public String uuid;
67          public Group(String name, String uuid) {
68              this.name = name;
69              this.uuid = uuid;
70          }
71      }
72  
73      private static final Logger log = LoggerFactory.getLogger(GroupManagementField.class);
74  
75      public GroupManagementField(GroupManagementFieldDefinition definition, Item relatedFieldItem) {
76          super(definition, relatedFieldItem);
77          this.definition.setOptions(getSelectFieldOptionDefinition());
78      }
79  
80  
81      @Override
82      protected AbstractSelect buildField() {
83          super.buildField();
84          select.setMultiSelect(true);
85          select.setNullSelectionAllowed(true);
86          select.setImmediate(true);
87          return select;
88      }
89  
90      @Override
91      protected AbstractSelect createSelectionField() {
92          return new TwinColSelect();
93      }
94  
95      /**
96       * Returns the available groups with those already assigned marked selected, according to the current node.
97       */
98      @Override
99      public List<SelectFieldOptionDefinition> getSelectFieldOptionDefinition(){
100         List<SelectFieldOptionDefinition> options = new ArrayList<SelectFieldOptionDefinition>();
101         List<Group> allGroups = getAllGroups(); // name,uuid
102         List<String> assignedGroups = getAssignedGroups();
103         String currentUUID = null;
104         try {
105             currentUUID = getRelatedNode(item).getIdentifier();
106         } catch (RepositoryException e) {
107             // nothing to do
108         }
109         for (Group group : allGroups) {
110             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
111             option.setValue(group.uuid);
112             option.setLabel(group.name);
113             if (assignedGroups.contains(group.uuid)) {
114                 option.setSelected(true);
115             }
116             if (!group.uuid.equals(currentUUID)) {
117                 // we don't want the group to be assigned to itself, do we?
118                 options.add(option);
119             }
120         }
121         return options;
122     }
123 
124     private List<Group> getAllGroups() {
125         List<Group> groups = new ArrayList<Group>();
126         try {
127             NodeIterator ni = QueryUtil.search(RepositoryConstants.USER_GROUPS, "SELECT * FROM ["+NodeTypes.Group.NAME+"] ORDER BY name()");
128             while (ni.hasNext()) {
129                 Node n = ni.nextNode();
130                 String name = n.getName();
131                 String uuid = n.getIdentifier();
132                 groups.add(new Group(name, uuid));
133             }
134         } catch (RepositoryException e) {
135             log.error("Cannot read groups from the ["+RepositoryConstants.USER_GROUPS+"] workspace.", e);
136         }
137         return groups;
138     }
139 
140     private List<String> getAssignedGroups() {
141         List<String> groups = new ArrayList<String>();
142         Node mainNode = getRelatedNode(item);
143         try {
144             if (mainNode.hasNode("groups")) {
145                 Node groupsNode = mainNode.getNode("groups");
146                 if (groupsNode == null) {
147                     // shouldn't happen, just in case
148                     return groups;
149                 }
150                 PropertyIterator pi = groupsNode.getProperties();
151                 while (pi.hasNext()) {
152                     Property p = pi.nextProperty();
153                     if (!p.getName().startsWith(NodeTypes.JCR_PREFIX)) {
154                         // do not add system properties
155                         groups.add(p.getString());
156                     }
157                 }
158             }
159         } catch (RepositoryException re) {
160             log.error("Cannot read assigned groups of the node ["+mainNode+"].", re);
161         }
162         return groups;
163     }
164 
165     @Override
166     public com.vaadin.data.Property getOrCreateProperty() {
167         DefaultProperty prop = new DefaultProperty("groups", getAssignedGroups());
168         item.addItemProperty("groups", prop);
169         return prop;
170     }
171 
172 }