View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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.module.admininterface;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.gui.dialog.Dialog;
38  import info.magnolia.cms.gui.dialog.DialogControlImpl;
39  import info.magnolia.cms.gui.dialog.UUIDDialogControl;
40  import info.magnolia.cms.security.AccessDeniedException;
41  import info.magnolia.cms.util.ContentUtil;
42  
43  import javax.jcr.PathNotFoundException;
44  import javax.jcr.RepositoryException;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  
49  /**
50   * This save handler checks each control if it is implementing the UUIDDialogControl interface. If so it stores the uuid
51   * instead of the path.
52   * @author Philipp Bracher
53   * @version $Id$
54   */
55  public class UUIDSaveHandler extends SaveHandlerImpl implements DialogAwareSaveHandler {
56  
57      /**
58       * The dialog we are saving
59       */
60      private Dialog dialog;
61  
62      @Override
63      public Dialog getDialog() {
64          return dialog;
65      }
66  
67      @Override
68      public void setDialog(Dialog dialog) {
69          this.dialog = dialog;
70      }
71  
72      /**
73       * Process a singel value
74       */
75      @Override
76      protected void processString(Content node, String name, int type, int encoding, String[] values, String valueStr)
77          throws PathNotFoundException, RepositoryException, AccessDeniedException {
78          DialogControlImpl control = getControl(name);
79          if (control instanceof UUIDDialogControl) {
80              // convert it to an uuid
81              if (StringUtils.isNotEmpty(valueStr)) {
82                  valueStr = getUUID(name, valueStr);
83              }
84          }
85  
86          super.processString(node, name, type, encoding, values, valueStr);
87      }
88  
89      /**
90       * Process a multiple value
91       */
92      @Override
93      protected void processMultiple(Content node, String name, int type, String[] values) throws RepositoryException,
94          PathNotFoundException, AccessDeniedException {
95          DialogControlImpl control = getControl(name);
96  
97          if (control instanceof UUIDDialogControl) {
98              if (values != null && values.length != 0) {
99                  String[] uuidValues = new String[values.length];
100                 for (int i = 0; i < values.length; i++) {
101                     uuidValues[i] = getUUID(name, values[i]);
102                 }
103                 values = uuidValues;
104             }
105         }
106         super.processMultiple(node, name, type, values);
107     }
108 
109     /**
110      * Get the contorl which is processed now
111      * @param name
112      * @return the control or null if not found
113      */
114     protected DialogControlImpl getControl(String name) {
115         Object control = this.getDialog().getSub(name);
116         if (control instanceof DialogControlImpl) {
117             return (DialogControlImpl) control;
118         }
119         return null;
120     }
121 
122     /**
123      * Convert the path to an uuid. It uses the config value 'repository' of the control if found.
124      * @param name
125      * @param path
126      * @return thr uuid or the path if the path was not found
127      */
128     private String getUUID(String name, String path) {
129         String repository = ((UUIDDialogControl)getControl(name)).getRepository();
130         Content node = ContentUtil.getContent(repository, path);
131         if (node == null) {
132             return path;
133         }
134         return node.getUUID();
135     }
136 
137 }