KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > JahiaObjectTool


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// JahiaObjectUtils
14
// MJ 12.03.2001
15
//
16
//
17

18 package org.jahia.utils;
19
20
21 import java.util.HashMap JavaDoc;
22
23 import org.jahia.data.containers.JahiaContainer;
24 import org.jahia.data.containers.JahiaContainerList;
25 import org.jahia.data.fields.JahiaField;
26 import org.jahia.data.fields.LoadFlags;
27 import org.jahia.exceptions.JahiaException;
28 import org.jahia.params.ParamBean;
29 import org.jahia.registries.ServicesRegistry;
30 import org.jahia.services.pages.JahiaPage;
31 import org.jahia.services.pages.JahiaPageDefinition;
32 import org.jahia.services.version.EntryLoadRequest;
33
34
35 /**
36  * Utility class to return characteristic strings with an input of (int objectType, int objectID)
37  * intended for use by the audit log management system
38  *
39  * @author MJ
40  */

41
42
43 public class JahiaObjectTool
44 {
45
46     private static JahiaObjectTool theObject = null;
47
48     private static final String JavaDoc MSG_INTERNAL_ERROR = new String JavaDoc ("Object Tool internal error");
49
50     public static final int FIELD_TYPE = 1;
51     public static final int CONTAINER_TYPE = 2;
52     public static final int CONTAINERLIST_TYPE = 3;
53     public static final int PAGE_TYPE = 4;
54     public static final int ACL_TYPE = 5;
55     public static final int APPLICATION_TYPE = 6;
56     public static final int SERVER_TYPE = 7;
57     public static final int TEMPLATE_TYPE = 8;
58
59     /***
60         * returns the single instance of the object
61         *
62         */

63     public static synchronized JahiaObjectTool getInstance()
64     {
65         if (theObject == null) {
66             theObject = new JahiaObjectTool();
67         }
68         return theObject;
69     } // end getInstance
70

71
72
73     /***
74         * get the object name according to the object type and object ID (without engineMap)
75         * @author Mikha�l Janson
76         * @param objectType
77         * @param objectID
78         * @param jParams
79         * @return the object name, as <code>String</code>
80         */

81     public String JavaDoc getObjectName( int objectType, int objectID, ParamBean jParams )
82     throws JahiaException
83     {
84         ServicesRegistry sReg = ServicesRegistry.getInstance();
85         EntryLoadRequest loadRequest = new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 0, jParams.getEntryLoadRequest().getLocales());
86
87         switch(objectType) {
88             case CONTAINERLIST_TYPE:
89                 return sReg.getJahiaContainersService().loadContainerListInfo(objectID).getDefinition().getName();
90             case CONTAINER_TYPE:
91                 JahiaContainer jahiaContainer = sReg.getJahiaContainersService().loadContainerInfo(objectID,jParams.getEntryLoadRequest());
92                 if (jahiaContainer == null) {
93                     jahiaContainer = sReg.getJahiaContainersService().loadContainerInfo(objectID,EntryLoadRequest.STAGED);
94                 }
95                 if (jahiaContainer != null) {
96                     return jahiaContainer.getDefinition().getName();
97                 } else {
98                     return "container [" + objectID + "]";
99                 }
100             case PAGE_TYPE:
101                 JahiaPage page = sReg.getJahiaPageService().lookupPage(objectID,
102                     loadRequest, jParams.getOperationMode(), jParams.getUser(),
103                     true);
104                 if (page != null)
105                     return page.getTitle();
106                 else
107                     return "page [" + objectID + "]";
108             case FIELD_TYPE:
109                 JahiaField f =sReg.getJahiaFieldService().loadField(objectID,LoadFlags.NOTHING, jParams, jParams.getEntryLoadRequest());
110                 if ( f!= null ){
111                     return f.getDefinition().getName();
112                 } else {
113                     return ("field [" + objectID + "]");
114                 }
115             case APPLICATION_TYPE:
116                 return sReg.getJahiaApplicationsManagerService().getApplication(objectID).getName();
117             case TEMPLATE_TYPE:
118                 return sReg.getJahiaPageTemplateService().lookupPageTemplate(objectID).getName();
119             case SERVER_TYPE:
120                 return "Jahia Server";
121             default:
122                 throw new JahiaException (MSG_INTERNAL_ERROR,
123                                           "Incompatible Object Type passed to JahiaObjectTool.getObjectName(objectType, objectID)",
124                                           JahiaException.SERVICE_ERROR,
125                                           JahiaException.CRITICAL_SEVERITY);
126         }
127     } // end getObjectName( int objectType, int objectID )
128

129
130
131     /***
132         * get the object name according to the object type
133         * @author Mikha�l Janson
134         * @param objectType
135         * @param engineMap
136         * @return the object name, as an <code>String</code>
137         */

138     public String JavaDoc getObjectName( int objectType, HashMap JavaDoc engineMap )
139     throws JahiaException
140     {
141         switch(objectType) {
142             case CONTAINERLIST_TYPE: return ((JahiaContainerList) engineMap.get( "theContainerList" )).getDefinition().getName();
143             case CONTAINER_TYPE: return ((JahiaContainer) engineMap.get( "theContainer" )).getDefinition().getName();
144             case PAGE_TYPE: return ((JahiaPage) engineMap.get( "thePage" )).getTitle();
145             case FIELD_TYPE: return ((JahiaField) engineMap.get( "theField" )).getDefinition().getName();
146             case TEMPLATE_TYPE: return ((JahiaPageDefinition) engineMap.get( "theTemplate" )).getName();
147
148
149             default: throw new JahiaException (MSG_INTERNAL_ERROR,
150                                                                "Incompatible Object Type passed to JahiaObjectTool.getObjectName(objectType, engineMap)",
151                                                                JahiaException.SERVICE_ERROR,
152                                                                JahiaException.CRITICAL_SEVERITY);
153         }
154     } // end getObjectName (int objectType, HashMap engineMap)
155

156
157     /***
158         * get an object type name from the objectType
159         * @author Mikha�l Janson
160         * @param objectType the object type, as an <code>int</code>
161         * @return a <code>String</code> containing the name of the object type
162         *
163         */

164     public String JavaDoc getObjectTypeName( int objectType )
165     throws JahiaException
166     {
167         switch(objectType) {
168
169             case CONTAINERLIST_TYPE: return "containerlist";
170             case CONTAINER_TYPE: return "container";
171             case PAGE_TYPE: return "page";
172             case FIELD_TYPE: return "field";
173             case TEMPLATE_TYPE: return "template";
174
175             default: throw new JahiaException (MSG_INTERNAL_ERROR,
176                                                                "Incompatible Object Type passed to JahiaObjectTool.getObjectName(objectType)",
177                                                                JahiaException.SERVICE_ERROR,
178                                                                JahiaException.CRITICAL_SEVERITY);
179         }
180     }
181
182
183     /***
184         * get the object ID according to the object type
185         * @author Mikha�l Janson
186         * @param objectType
187         * @param engineMap
188         * @return the object ID, as an <code>int</code>
189         */

190     public int getObjectID( int objectType, HashMap JavaDoc engineMap )
191     throws JahiaException
192     {
193         switch(objectType) {
194
195             case CONTAINERLIST_TYPE: return ((JahiaContainerList) engineMap.get( "theContainerList" )).getID();
196             case CONTAINER_TYPE: return ((JahiaContainer) engineMap.get( "theContainer" )).getID();
197             case PAGE_TYPE: return ((JahiaPage) engineMap.get( "thePage" )).getID();
198             case FIELD_TYPE: return ((JahiaField) engineMap.get( "theField" )).getID();
199             case TEMPLATE_TYPE: return ((JahiaPageDefinition) engineMap.get( "theTemplate" )).getID();
200
201             default: throw new JahiaException (MSG_INTERNAL_ERROR,
202                                                                "Incompatible Object Type passed to JahiaObjectTool",
203                                                                JahiaException.SERVICE_ERROR,
204                                                                JahiaException.CRITICAL_SEVERITY);
205         }
206     }
207
208
209
210 } // end class JahiaObjectTool
211
Popular Tags