View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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: UUIDSaveHandler.java 41137 2011-01-06 18:19:25Z gjoseph $
54   */
55  public class UUIDSaveHandler extends SaveHandlerImpl implements DialogAwareSaveHandler {
56  
57      /**
58       * The dialog we are saving
59       */
60      private Dialog dialog;
61  
62      public Dialog getDialog() {
63          return dialog;
64      }
65  
66      public void setDialog(Dialog dialog) {
67          this.dialog = dialog;
68      }
69  
70      /**
71       * Process a singel value
72       */
73      protected void processString(Content node, String name, int type, int encoding, String[] values, String valueStr)
74          throws PathNotFoundException, RepositoryException, AccessDeniedException {
75          DialogControlImpl control = getControl(name);
76          if (control instanceof UUIDDialogControl) {
77              // convert it to an uuid
78              if (StringUtils.isNotEmpty(valueStr)) {
79                  valueStr = getUUID(name, valueStr);
80              }
81          }
82  
83          super.processString(node, name, type, encoding, values, valueStr);
84      }
85  
86      /**
87       * Process a multiple value
88       */
89      protected void processMultiple(Content node, String name, int type, String[] values) throws RepositoryException,
90          PathNotFoundException, AccessDeniedException {
91          DialogControlImpl control = getControl(name);
92  
93          if (control instanceof UUIDDialogControl) {
94              if (values != null && values.length != 0) {
95                  String[] uuidValues = new String[values.length];
96                  for (int i = 0; i < values.length; i++) {
97                      uuidValues[i] = getUUID(name, values[i]);
98                  }
99                  values = uuidValues;
100             }
101         }
102         super.processMultiple(node, name, type, values);
103     }
104 
105     /**
106      * Get the contorl which is processed now
107      * @param name
108      * @return the control or null if not found
109      */
110     protected DialogControlImpl getControl(String name) {
111         Object control = this.getDialog().getSub(name);
112         if (control instanceof DialogControlImpl) {
113             return (DialogControlImpl) control;
114         }
115         return null;
116     }
117 
118     /**
119      * Convert the path to an uuid. It uses the config value 'repository' of the control if found.
120      * @param name
121      * @param path
122      * @return thr uuid or the path if the path was not found
123      */
124     private String getUUID(String name, String path) {
125         String repository = ((UUIDDialogControl)getControl(name)).getRepository();
126         Content node = ContentUtil.getContent(repository, path);
127         if (node == null) {
128             return path;
129         }
130         return node.getUUID();
131     }
132 
133 }