KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workeffort > workeffort > WorkEffortWorker


1 /*
2  * $Id: WorkEffortWorker.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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.workeffort.workeffort;
26
27 import java.util.Collection JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.jsp.PageContext JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilMisc;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.entity.GenericValue;
37 import org.ofbiz.security.Security;
38 import org.ofbiz.service.GenericServiceException;
39 import org.ofbiz.service.LocalDispatcher;
40 import org.ofbiz.service.ModelService;
41
42 /**
43  * WorkEffortWorker - Worker class to reduce code in JSPs & make it more reusable
44  *
45  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 2.0
49  */

50 public class WorkEffortWorker {
51     
52     public static final String JavaDoc module = WorkEffortWorker.class.getName();
53     
54     // TODO: REMOVE this method when JSPs/etc are moved to FreeMarker; this is replaced by a corresponding service
55
public static void getWorkEffort(PageContext JavaDoc pageContext, String JavaDoc workEffortIdAttrName, String JavaDoc workEffortAttrName, String JavaDoc partyAssignsAttrName,
56         String JavaDoc canViewAttrName, String JavaDoc tryEntityAttrName, String JavaDoc currentStatusAttrName) {
57         GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator");
58         Security security = (Security) pageContext.getRequest().getAttribute("security");
59         GenericValue userLogin = (GenericValue) pageContext.getSession().getAttribute("userLogin");
60
61         String JavaDoc workEffortId = pageContext.getRequest().getParameter("workEffortId");
62
63         // if there was no parameter, check the request attribute, this may be a newly created entity
64
if (workEffortId == null)
65             workEffortId = (String JavaDoc) pageContext.getRequest().getAttribute("workEffortId");
66
67         GenericValue workEffort = null;
68
69         try {
70             workEffort = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId));
71         } catch (GenericEntityException e) {
72             Debug.logWarning(e, module);
73         }
74
75         Boolean JavaDoc canView = null;
76         Collection JavaDoc workEffortPartyAssignments = null;
77         Boolean JavaDoc tryEntity = null;
78         GenericValue currentStatus = null;
79
80         if (workEffort == null) {
81             tryEntity = new Boolean JavaDoc(false);
82             canView = new Boolean JavaDoc(true);
83
84             String JavaDoc statusId = pageContext.getRequest().getParameter("currentStatusId");
85
86             if (statusId != null && statusId.length() > 0) {
87                 try {
88                     currentStatus = delegator.findByPrimaryKeyCache("StatusItem", UtilMisc.toMap("statusId", statusId));
89                 } catch (GenericEntityException e) {
90                     Debug.logWarning(e, module);
91                 }
92             }
93         } else {
94             // get a collection of workEffortPartyAssignments, if empty then this user CANNOT view the event, unless they have permission to view all
95
if (userLogin != null && userLogin.get("partyId") != null && workEffortId != null) {
96                 try {
97                     workEffortPartyAssignments =
98                             delegator.findByAnd("WorkEffortPartyAssignment", UtilMisc.toMap("workEffortId", workEffortId, "partyId", userLogin.get("partyId")));
99                 } catch (GenericEntityException e) {
100                     Debug.logWarning(e, module);
101                 }
102             }
103             canView = (workEffortPartyAssignments != null && workEffortPartyAssignments.size() > 0) ? new Boolean JavaDoc(true) : new Boolean JavaDoc(false);
104             if (!canView.booleanValue() && security.hasEntityPermission("WORKEFFORTMGR", "_VIEW", pageContext.getSession())) {
105                 canView = new Boolean JavaDoc(true);
106             }
107
108             tryEntity = new Boolean JavaDoc(true);
109
110             if (workEffort.get("currentStatusId") != null) {
111                 try {
112                     currentStatus = delegator.findByPrimaryKeyCache("StatusItem", UtilMisc.toMap("statusId", workEffort.get("currentStatusId")));
113                 } catch (GenericEntityException e) {
114                     Debug.logWarning(e, module);
115                 }
116             }
117         }
118
119         // if there was an error message, don't get values from entity
120
if (pageContext.getRequest().getAttribute("_ERROR_MESSAGE_") != null) {
121             tryEntity = new Boolean JavaDoc(false);
122         }
123
124         if (workEffortId != null)
125             pageContext.setAttribute(workEffortIdAttrName, workEffortId);
126         if (workEffort != null)
127             pageContext.setAttribute(workEffortAttrName, workEffort);
128         if (canView != null)
129             pageContext.setAttribute(canViewAttrName, canView);
130         if (workEffortPartyAssignments != null)
131             pageContext.setAttribute(partyAssignsAttrName, workEffortPartyAssignments);
132         if (tryEntity != null)
133             pageContext.setAttribute(tryEntityAttrName, tryEntity);
134         if (currentStatus != null)
135             pageContext.setAttribute(currentStatusAttrName, currentStatus);
136     }
137
138     public static void getMonthWorkEffortEvents(PageContext JavaDoc pageContext, String JavaDoc attributeName) {}
139
140     public static void getActivityContext(PageContext JavaDoc pageContext, String JavaDoc workEffortId) {
141         getActivityContext(pageContext, workEffortId, "activityContext");
142     }
143
144     public static void getActivityContext(PageContext JavaDoc pageContext, String JavaDoc workEffortId, String JavaDoc attribute) {
145         GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator");
146         LocalDispatcher dispatcher = (LocalDispatcher) pageContext.getRequest().getAttribute("dispatcher");
147         GenericValue userLogin = (GenericValue) pageContext.getSession().getAttribute("userLogin");
148         Map JavaDoc svcCtx = UtilMisc.toMap("workEffortId", workEffortId, "userLogin", userLogin);
149         Map JavaDoc result = null;
150
151         try {
152             result = dispatcher.runSync("wfGetActivityContext", svcCtx);
153         } catch (GenericServiceException e) {
154             Debug.logError(e, module);
155         }
156         if (result != null && result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))
157             Debug.logError((String JavaDoc) result.get(ModelService.ERROR_MESSAGE), module);
158         if (result != null && result.containsKey("activityContext")) {
159             Map JavaDoc aC = (Map JavaDoc) result.get("activityContext");
160
161             pageContext.setAttribute(attribute, aC);
162         }
163     }
164 }
165
Popular Tags