View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.security.app.dialog.field;
35  
36  import info.magnolia.cms.util.QueryUtil;
37  import info.magnolia.jcr.iterator.FilteringPropertyIterator;
38  import info.magnolia.jcr.predicate.JCRMgnlPropertyHidingPredicate;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.objectfactory.ComponentProvider;
41  import info.magnolia.repository.RepositoryConstants;
42  import info.magnolia.ui.form.field.definition.SelectFieldOptionDefinition;
43  import info.magnolia.ui.form.field.factory.TwinColSelectFieldFactory;
44  import info.magnolia.ui.form.field.transformer.Transformer;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
46  
47  import java.util.ArrayList;
48  import java.util.HashSet;
49  import java.util.List;
50  import java.util.Set;
51  
52  import javax.inject.Inject;
53  import javax.jcr.Node;
54  import javax.jcr.NodeIterator;
55  import javax.jcr.Property;
56  import javax.jcr.PropertyIterator;
57  import javax.jcr.RepositoryException;
58  
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  import com.vaadin.data.Item;
63  import com.vaadin.ui.AbstractSelect;
64  import com.vaadin.ui.TwinColSelect;
65  
66  /**
67   * GUI builder for the Group Management field.
68   */
69  public class GroupManagementFieldFactory extends TwinColSelectFieldFactory<GroupManagementFieldDefinition> {
70  
71      /**
72       * Internal bean to represent basic group data.
73       */
74      public static class Group {
75          public String name;
76          public String uuid;
77  
78          public Group(String name, String uuid) {
79              this.name = name;
80              this.uuid = uuid;
81          }
82      }
83  
84      private static final Logger log = LoggerFactory.getLogger(GroupManagementFieldFactory.class);
85      private ComponentProvider componentProvider;
86  
87      @Inject
88      public GroupManagementFieldFactory(GroupManagementFieldDefinition definition, Item relatedFieldItem, ComponentProvider componentProvider) {
89          super(definition, relatedFieldItem, componentProvider);
90          this.definition.setOptions(getSelectFieldOptionDefinition());
91          this.componentProvider = componentProvider;
92      }
93  
94      @Override
95      protected AbstractSelect createFieldComponent() {
96          super.createFieldComponent();
97          select.setMultiSelect(true);
98          select.setNullSelectionAllowed(true);
99          select.setImmediate(true);
100         return select;
101     }
102 
103     @Override
104     protected AbstractSelect createSelectionField() {
105         return new TwinColSelect() {
106 
107             @Override
108             public String getConnectorId() {
109                 return super.getConnectorId();
110             }
111 
112             /* (non-Javadoc)
113              * @see com.vaadin.ui.AbstractComponent#isVisible()
114              */
115             @Override
116             public boolean isVisible() {
117                 return super.isVisible();
118             }
119         };
120     }
121 
122     /**
123      * Returns the available groups with those already assigned marked selected,
124      * according to the current node.
125      */
126     @Override
127     public List<SelectFieldOptionDefinition> getSelectFieldOptionDefinition() {
128         List<SelectFieldOptionDefinition> options = new ArrayList<SelectFieldOptionDefinition>();
129         List<Group> allGroups = getAllGroups(); // name,uuid
130         Set<String> assignedGroups = getAssignedGroups();
131         String currentUUID = null;
132         try {
133             currentUUID = ((JcrNodeAdapter) item).getJcrItem().getIdentifier();
134         } catch (RepositoryException e) {
135             // nothing to do
136         }
137         for (Group group : allGroups) {
138             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
139             option.setValue(group.uuid);
140             option.setLabel(group.name);
141             if (assignedGroups.contains(group.uuid)) {
142                 option.setSelected(true);
143             }
144             if (!group.uuid.equals(currentUUID)) {
145                 // we don't want the group to be assigned to itself, do we?
146                 options.add(option);
147             }
148         }
149         return options;
150     }
151 
152     private List<Group> getAllGroups() {
153         List<Group> groups = new ArrayList<Group>();
154         try {
155             NodeIterator ni = QueryUtil.search(RepositoryConstants.USER_GROUPS, "SELECT * FROM [" + NodeTypes.Group.NAME + "] ORDER BY name()");
156 
157             while (ni.hasNext()) {
158                 Node n = ni.nextNode();
159                 String name = n.getName();
160                 String uuid = n.getIdentifier();
161                 groups.add(new Group(name, uuid));
162             }
163         } catch (RepositoryException e) {
164             log.error("Cannot read groups from the [" + RepositoryConstants.USER_GROUPS + "] workspace.", e);
165         }
166         return groups;
167     }
168 
169     private Set<String> getAssignedGroups() {
170         Set<String> groups = new HashSet<String>();
171         try {
172             Node mainNode = ((JcrNodeAdapter) item).getJcrItem();
173             if (mainNode.hasNode("groups")) {
174                 Node groupsNode = mainNode.getNode("groups");
175                 if (groupsNode == null) {
176                     // shouldn't happen, just in case
177                     return groups;
178                 }
179                 for (PropertyIterator iter = new FilteringPropertyIterator(groupsNode.getProperties(), new JCRMgnlPropertyHidingPredicate()); iter.hasNext();) {
180                     Property p = iter.nextProperty();
181                     groups.add(p.getString());
182 
183                 }
184             }
185         } catch (RepositoryException re) {
186             log.error("Cannot read assigned groups.", re);
187         }
188         return groups;
189     }
190 
191     /**
192      * Create a new Instance of {@link Transformer}.
193      */
194     @Override
195     protected Transformer<?> initializeTransformer(Class<? extends Transformer<?>> transformerClass) {
196         return this.componentProvider.newInstance(transformerClass, item, definition, HashSet.class, getAssignedGroups(), "groups");
197     }
198 
199 }