KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > UUIDSaveHandler


1 /**
2  *
3  */

4 package info.magnolia.module.admininterface;
5
6 import javax.jcr.PathNotFoundException;
7 import javax.jcr.RepositoryException;
8
9 import org.apache.commons.lang.StringUtils;
10
11 import info.magnolia.cms.beans.config.ContentRepository;
12 import info.magnolia.cms.core.Content;
13 import info.magnolia.cms.gui.dialog.Dialog;
14 import info.magnolia.cms.gui.dialog.DialogControlImpl;
15 import info.magnolia.cms.gui.dialog.UUIDDialogControl;
16 import info.magnolia.cms.security.AccessDeniedException;
17 import info.magnolia.cms.util.ContentUtil;
18
19
20 /**
21  * This save handler checks each control if it is implementing the UUIDDialogControl interface. If so it stores the uuid
22  * instead of the path.
23  * @author Philipp Bracher
24  * @version $Id: UUIDSaveHandler.java 6341 2006-09-12 09:18:27Z philipp $
25  */

26 public class UUIDSaveHandler extends SaveHandlerImpl implements DialogAwareSaveHandler {
27
28     /**
29      * The dialog we are saving
30      */

31     private Dialog dialog;
32
33     public Dialog getDialog() {
34         return dialog;
35     }
36
37     public void setDialog(Dialog dialog) {
38         this.dialog = dialog;
39     }
40
41     /**
42      * Process a singel value
43      */

44     protected void processString(Content node, String JavaDoc name, int type, int encoding, String JavaDoc[] values, String JavaDoc valueStr)
45         throws PathNotFoundException, RepositoryException, AccessDeniedException {
46         DialogControlImpl control = getControl(name);
47         if (control instanceof UUIDDialogControl) {
48             // convert it to an uuid
49
if (StringUtils.isNotEmpty(valueStr)) {
50                 valueStr = getUUID(name, valueStr);
51             }
52         }
53
54         super.processString(node, name, type, encoding, values, valueStr);
55     }
56
57     /**
58      * Process a multiple value
59      */

60     protected void processMultiple(Content node, String JavaDoc name, int type, String JavaDoc[] values) throws RepositoryException,
61         PathNotFoundException, AccessDeniedException {
62         DialogControlImpl control = getControl(name);
63
64         if (control instanceof UUIDDialogControl) {
65             if (values != null && values.length != 0) {
66                 String JavaDoc[] uuidValues = new String JavaDoc[values.length];
67                 for (int i = 0; i < values.length; i++) {
68                     uuidValues[i] = getUUID(name, values[i]);
69                 }
70                 values = uuidValues;
71             }
72         }
73         super.processMultiple(node, name, type, values);
74     }
75
76     /**
77      * Get the contorl which is processed now
78      * @param name
79      * @return the control or null if not found
80      */

81     protected DialogControlImpl getControl(String JavaDoc name) {
82         Object JavaDoc control = this.getDialog().getSub(name);
83         if (control instanceof DialogControlImpl) {
84             return (DialogControlImpl) control;
85         }
86         return null;
87     }
88
89     /**
90      * Convert the path to an uuid. It uses the config value 'repository' of the control if found.
91      * @param name
92      * @param path
93      * @return thr uuid or the path if the path was not found
94      */

95     private String JavaDoc getUUID(String JavaDoc name, String JavaDoc path) {
96         String JavaDoc repository = ((UUIDDialogControl)getControl(name)).getRepository();
97         Content node = ContentUtil.getContent(repository, path);
98         if (node == null) {
99             return path;
100         }
101         return node.getUUID();
102     }
103
104 }
105
Popular Tags