KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > content > PermissionRecorder


1 /*
2  * $Id: PermissionRecorder.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.content.content;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.ofbiz.base.util.UtilProperties;
34 import org.ofbiz.entity.GenericValue;
35
36 /**
37  * PermissionRecorder Class
38  *
39  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
40  * @version $Rev: 5462 $
41  * @since 2.2
42  *
43  * Services for granting operation permissions on Content entities in a data-driven manner.
44  */

45 public class PermissionRecorder {
46
47     public static final int PRE_PURPOSE = 0;
48     public static final int PRE_ROLE = 1;
49     public static final int WITH_ROLES = 2;
50
51     protected boolean isOn = false;
52     protected GenericValue userLogin;
53     protected List JavaDoc permCheckResults = new ArrayList JavaDoc();
54     protected boolean entityPermCheckResult = false;
55     protected String JavaDoc currentContentId = "";
56     protected Map JavaDoc currentContentMap;
57     protected String JavaDoc privilegeEnumId;
58     protected int currentCheckMode;
59     protected GenericValue [] contentPurposeOperations;
60     protected String JavaDoc [] statusTargets;
61     protected String JavaDoc [] targetOperations;
62
63     public static final String JavaDoc module = PermissionRecorder.class.getName();
64
65     public static final String JavaDoc [] opFields = { "contentPurposeTypeId", "contentOperationId", "roleTypeId", "statusId", "privilegeEnumId"};
66     public static final String JavaDoc [] fieldTitles = { "Purpose", "Operation", "Role", "Status", "Privilege"};
67
68     public PermissionRecorder() {
69         isOn = UtilProperties.propertyValueEqualsIgnoreCase("content.properties", "permissionRecorderOn", "true");
70     }
71
72     public void setCheckMode(int val) {
73         currentCheckMode = val;
74     }
75
76     public int getCheckMode() {
77         return currentCheckMode;
78     }
79
80     public boolean isOn() {
81         return isOn;
82     }
83
84     public void setOn(boolean b) {
85         isOn = b;
86     }
87
88     public void setUserLogin(GenericValue user) {
89         userLogin = user;
90     }
91
92     public GenericValue getUserLogin() {
93         return userLogin;
94     }
95
96     public boolean getEntityPermCheckResult() {
97         return entityPermCheckResult;
98     }
99
100     public void setEntityPermCheckResult(boolean b) {
101         entityPermCheckResult = b;
102     }
103
104     public GenericValue [] getContentPurposeOperations() {
105        return contentPurposeOperations;
106     }
107
108     public void setContentPurposeOperations(List JavaDoc opList) {
109        contentPurposeOperations = (GenericValue [])opList.toArray();
110     }
111
112     public void setPrivilegeEnumId(String JavaDoc id) {
113         privilegeEnumId = id;
114     }
115
116     public String JavaDoc getPrivilegeEnumId() {
117         return privilegeEnumId;
118     }
119
120     public String JavaDoc [] getStatusTargets() {
121        return statusTargets;
122     }
123
124     public void setStatusTargets(List JavaDoc opList) {
125        statusTargets = (String JavaDoc [])opList.toArray();
126     }
127
128     public String JavaDoc [] getTargetOperations() {
129        return targetOperations;
130     }
131
132     public void setTargetOperations(List JavaDoc opList) {
133        targetOperations = (String JavaDoc [])opList.toArray();
134     }
135
136     public void setCurrentContentId(String JavaDoc id) {
137         if (!currentContentId.equals(id)) {
138             currentContentMap = new HashMap JavaDoc();
139             permCheckResults.add(currentContentMap);
140             currentContentMap.put("contentId", id);
141             currentContentMap.put("checkResults", new ArrayList JavaDoc());
142         }
143         currentContentId = id;
144     }
145
146     public String JavaDoc getCurrentContentId() {
147         return currentContentId;
148     }
149
150     public void setRoles(List JavaDoc roles) {
151         if (currentContentMap != null) {
152             if (roles != null)
153                 currentContentMap.put("roles", roles.toArray());
154             else
155                 currentContentMap.put("roles", null);
156         }
157     }
158
159     public void setPurposes(List JavaDoc purposes) {
160         if (currentContentMap != null) {
161             if (purposes != null)
162                 currentContentMap.put("purposes", purposes.toArray());
163             else
164                 currentContentMap.put("purposes", null);
165         }
166     }
167
168     public void startMatchGroup(List JavaDoc targetOperations, List JavaDoc purposes, List JavaDoc roles, List JavaDoc targStatusList, String JavaDoc targPrivilegeEnumId, String JavaDoc contentId) {
169
170         currentContentMap = new HashMap JavaDoc();
171         permCheckResults.add(currentContentMap);
172         String JavaDoc s = null;
173         if (targetOperations != null) {
174             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, targetOperations:" + targetOperations, module);
175
s = targetOperations.toString();
176             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, targetOperations(string):" + s, module);
177
currentContentMap.put("contentOperationId", s);
178         }
179         if (purposes != null) {
180             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, purposes:" + purposes, module);
181
s = purposes.toString();
182             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, purposes(string):" + s, module);
183
currentContentMap.put("contentPurposeTypeId", s);
184         }
185         if (roles != null) {
186             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, roles:" + roles, module);
187
s = roles.toString();
188             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, roles(string):" + s, module);
189
currentContentMap.put("roleTypeId", s);
190         }
191         if (targStatusList != null) {
192             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, targStatusList:" + targStatusList, module);
193
s = targStatusList.toString();
194             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, targStatusList(string):" + s, module);
195
currentContentMap.put("statusId", s);
196         }
197         currentContentMap.put("privilegeEnumId", privilegeEnumId);
198         currentContentMap.put("contentId", contentId);
199         currentContentMap.put("checkResultList", new ArrayList JavaDoc());
200         currentContentMap.put("matches", null);
201         currentContentId = contentId;
202             //if (Debug.infoOn()) Debug.logInfo("startMatchGroup, currentContentMap:" + currentContentMap, module);
203
}
204
205     public void record(GenericValue purposeOp, boolean targetOpCond, boolean purposeCond, boolean statusCond, boolean privilegeCond, boolean roleCond) {
206
207         Map JavaDoc map = new HashMap JavaDoc(purposeOp);
208         map.put("contentOperationIdCond", new Boolean JavaDoc(targetOpCond));
209         map.put("contentPurposeTypeIdCond", new Boolean JavaDoc(purposeCond));
210         map.put("statusIdCond", new Boolean JavaDoc(statusCond));
211         map.put("privilegeEnumIdCond", new Boolean JavaDoc(privilegeCond));
212         map.put("roleTypeIdCond", new Boolean JavaDoc(roleCond));
213         map.put("contentId", currentContentId);
214         ((List JavaDoc)currentContentMap.get("checkResultList")).add(map);
215             //if (Debug.infoOn()) Debug.logInfo("record, map:" + map, module);
216
}
217
218     public String JavaDoc toHtml() {
219         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
220         sb.append("<style type=\"text/css\">");
221         sb.append(".pass {background-color:lime; font-family:Verdana,Arial,sans-serif; font-size:10px; }");
222         sb.append(".fail {background-color:red; font-family:Verdana,Arial,sans-serif; font-size:10px; }");
223         sb.append(".target {background-color:lightgrey; font-family:Verdana,Arial,sans-serif; font-size:10px; }");
224         sb.append(".headr {background-color:white; font-weight:bold; font-family:Verdana,Arial,sans-serif; font-size:12px; }");
225         sb.append("</style>");
226
227         //if (Debug.infoOn()) Debug.logInfo("toHtml, style:" + sb.toString(), module);
228
sb.append("<table border=\"1\" >");
229         // Do header row
230
sb.append("<tr>");
231
232         sb.append("<td class=\"headr\">");
233         sb.append("Content Id");
234         sb.append("</td>");
235
236         //if (Debug.infoOn()) Debug.logInfo("renderResultRowHtml, (1):" + sb.toString(), module);
237
String JavaDoc str = null;
238         String JavaDoc s = null;
239         for (int i=0; i < fieldTitles.length; i++) {
240             String JavaDoc opField = (String JavaDoc)fieldTitles[i];
241             sb.append("<td class=\"headr\">");
242             sb.append(opField);
243             sb.append("</td>");
244         }
245         sb.append("<td class=\"headr\" >Pass/Fail</td>");
246         sb.append("</tr>");
247
248         Iterator JavaDoc iter = permCheckResults.iterator();
249         while (iter.hasNext()) {
250             Map JavaDoc cMap = (Map JavaDoc)iter.next();
251             sb.append(renderCurrentContentMapHtml(cMap));
252         }
253         sb.append("</table>");
254         return sb.toString();
255     }
256
257     public String JavaDoc renderCurrentContentMapHtml(Map JavaDoc cMap) {
258         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
259         List JavaDoc resultList = (List JavaDoc)cMap.get("checkResultList");
260         Iterator JavaDoc iter = resultList.iterator();
261         while (iter.hasNext()) {
262             Map JavaDoc rMap = (Map JavaDoc)iter.next();
263             //if (Debug.infoOn()) Debug.logInfo("renderCCMapHtml, (1):" + rMap, module);
264
sb.append(renderResultRowHtml(rMap, cMap));
265         }
266  
267         return sb.toString();
268     }
269
270     //public static final String [] opFields = { "contentPurposeTypeId", "contentOperationId", "roleTypeId", "statusId", "privilegeEnumId"};
271

272     public String JavaDoc renderResultRowHtml(Map JavaDoc rMap, Map JavaDoc currentContentResultMap ) {
273         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
274
275         // Do target row
276
sb.append("<tr>");
277
278         sb.append("<td class=\"target\">");
279         sb.append((String JavaDoc)rMap.get("contentId"));
280         sb.append("</td>");
281
282         //if (Debug.infoOn()) Debug.logInfo("renderResultRowHtml, (1):" + sb.toString(), module);
283
String JavaDoc str = null;
284         String JavaDoc s = null;
285         for (int i=0; i < opFields.length; i++) {
286             String JavaDoc opField = (String JavaDoc)opFields[i];
287             sb.append("<td class=\"target\">");
288             s = (String JavaDoc)currentContentResultMap.get(opField);
289             if (s != null)
290                 str = s;
291             else
292                 str = "&nbsp;";
293             sb.append(str);
294             sb.append("</td>");
295         }
296         sb.append("<td class=\"target\" >&nbsp;</td>");
297         sb.append("</tr>");
298
299         //if (Debug.infoOn()) Debug.logInfo("renderResultRowHtml, (2):" + sb.toString(), module);
300
// Do UUT row
301
sb.append("<tr>");
302
303         sb.append("<td class=\"target\">");
304         sb.append((String JavaDoc)currentContentResultMap.get("contentId"));
305         sb.append("</td>");
306
307         boolean isPass = true;
308         for (int i=0; i < opFields.length; i++) {
309             String JavaDoc opField = (String JavaDoc)opFields[i];
310             Boolean JavaDoc bool = (Boolean JavaDoc)rMap.get(opField + "Cond");
311             String JavaDoc cls = (bool.booleanValue()) ? "pass" : "fail";
312             if (!bool.booleanValue())
313                 isPass = false;
314             sb.append("<td class=\"" + cls + "\">");
315         //if (Debug.infoOn()) Debug.logInfo("renderResultRowHtml, (2b):" + sb.toString(), module);
316
s = (String JavaDoc)rMap.get(opField);
317         //if (Debug.infoOn()) Debug.logInfo("renderResultRowHtml, (s):" + s, module);
318
sb.append(s);
319             sb.append("</td>");
320         }
321         String JavaDoc passFailCls = (isPass) ? "pass" : "fail";
322         sb.append("<td class=\"" + passFailCls +"\">" + passFailCls.toUpperCase() + "</td>");
323         sb.append("</tr>");
324         //if (Debug.infoOn()) Debug.logInfo("renderResultRowHtml, (3):" + sb.toString(), module);
325

326         return sb.toString();
327     }
328 }
329
330
Popular Tags