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 info.magnolia.cms.util.QueryUtil;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.repository.RepositoryConstants;
39  import info.magnolia.ui.admincentral.field.builder.TwinColSelectFieldBuilder;
40  import info.magnolia.ui.model.field.definition.SelectFieldOptionDefinition;
41  import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
42  
43  import java.util.ArrayList;
44  import java.util.HashSet;
45  import java.util.List;
46  import java.util.Set;
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  
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  import com.vaadin.data.Item;
58  import com.vaadin.data.util.converter.Converter;
59  import com.vaadin.ui.AbstractSelect;
60  import com.vaadin.ui.TwinColSelect;
61  
62  /**
63   * GUI builder for the Group Management field.
64   */
65  public class GroupManagementField extends TwinColSelectFieldBuilder<GroupManagementFieldDefinition> {
66  
67      /**
68       * Internal bean to represent basic group data.
69       */
70      public static class Group {
71          public String name;
72          public String uuid;
73  
74          public Group(String name, String uuid) {
75              this.name = name;
76              this.uuid = uuid;
77          }
78      }
79  
80      private static final Logger log = LoggerFactory.getLogger(GroupManagementField.class);
81  
82      public GroupManagementField(GroupManagementFieldDefinition definition, Item relatedFieldItem) {
83          super(definition, relatedFieldItem);
84          this.definition.setOptions(getSelectFieldOptionDefinition());
85      }
86  
87      @Override
88      protected AbstractSelect buildField() {
89          super.buildField();
90          select.setMultiSelect(true);
91          select.setNullSelectionAllowed(true);
92          select.setImmediate(true);
93          return select;
94      }
95  
96      @Override
97      protected AbstractSelect createSelectionField() {
98          return new TwinColSelect() {
99              @Override
100             public void setConverter(Converter<Object, ?> converter) {
101                 System.out.println("Set the converter bldjad! " + converter);
102                 super.setConverter(converter);
103             }
104         };
105     }
106 
107     /**
108      * Returns the available groups with those already assigned marked selected,
109      * according to the current node.
110      */
111     @Override
112     public List<SelectFieldOptionDefinition> getSelectFieldOptionDefinition() {
113         List<SelectFieldOptionDefinition> options = new ArrayList<SelectFieldOptionDefinition>();
114         List<Group> allGroups = getAllGroups(); // name,uuid
115         Set<String> assignedGroups = getAssignedGroups();
116         String currentUUID = null;
117         try {
118             currentUUID = getRelatedNode(item).getIdentifier();
119         } catch (RepositoryException e) {
120             // nothing to do
121         }
122         for (Group group : allGroups) {
123             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
124             option.setValue(group.uuid);
125             option.setLabel(group.name);
126             if (assignedGroups.contains(group.uuid)) {
127                 option.setSelected(true);
128             }
129             if (!group.uuid.equals(currentUUID)) {
130                 // we don't want the group to be assigned to itself, do we?
131                 options.add(option);
132             }
133         }
134         return options;
135     }
136 
137     private List<Group> getAllGroups() {
138         List<Group> groups = new ArrayList<Group>();
139         try {
140             NodeIterator ni = QueryUtil.search(RepositoryConstants.USER_GROUPS, "SELECT * FROM [" + NodeTypes.Group.NAME + "] ORDER BY name()");
141 
142             while (ni.hasNext()) {
143                 Node n = ni.nextNode();
144                 String name = n.getName();
145                 String uuid = n.getIdentifier();
146                 groups.add(new Group(name, uuid));
147             }
148         } catch (RepositoryException e) {
149             log.error("Cannot read groups from the [" + RepositoryConstants.USER_GROUPS + "] workspace.", e);
150         }
151         return groups;
152     }
153 
154     private Set<String> getAssignedGroups() {
155         Set<String> groups = new HashSet<String>();
156         Node mainNode = getRelatedNode(item);
157         try {
158             if (mainNode.hasNode("groups")) {
159                 Node groupsNode = mainNode.getNode("groups");
160                 if (groupsNode == null) {
161                     // shouldn't happen, just in case
162                     return groups;
163                 }
164                 PropertyIterator pi = groupsNode.getProperties();
165                 while (pi.hasNext()) {
166                     Property p = pi.nextProperty();
167                     if (!p.getName().startsWith(NodeTypes.JCR_PREFIX)) {
168                         // do not add system properties
169                         groups.add(p.getString());
170                     }
171                 }
172             }
173         } catch (RepositoryException re) {
174             log.error("Cannot read assigned groups of the node [" + mainNode + "].", re);
175         }
176         return groups;
177     }
178 
179     @Override
180     public com.vaadin.data.Property<?> getOrCreateProperty() {
181         DefaultProperty<Set> prop = new DefaultProperty<Set>("groups", Set.class, getAssignedGroups());
182         item.addItemProperty("groups", prop);
183         return prop;
184     }
185 }