KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > crmsfa > teams > TeamServices


1 /*
2  * Copyright (c) 2006 - 2007 Open Source Strategies, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the Honest Public License.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * Honest Public License for more details.
11  *
12  * You should have received a copy of the Honest Public License
13  * along with this program; if not, write to Funambol,
14  * 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USA
15  */

16 /* Copyright (c) 2005-2006 Open Source Strategies, Inc. */
17
18 /*
19  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining a
22  * copy of this software and associated documentation files (the "Software"),
23  * to deal in the Software without restriction, including without limitation
24  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
25  * and/or sell copies of the Software, and to permit persons to whom the
26  * Software is furnished to do so, subject to the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be included
29  * in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
35  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
36  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
37  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */

39
40 package com.opensourcestrategies.crmsfa.teams;
41
42 import java.util.Map JavaDoc;
43 import java.util.Locale JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47 import org.ofbiz.base.util.Debug;
48 import org.ofbiz.base.util.UtilMisc;
49 import org.ofbiz.base.util.UtilDateTime;
50 import org.ofbiz.entity.GenericDelegator;
51 import org.ofbiz.entity.GenericEntityException;
52 import org.ofbiz.entity.GenericValue;
53 import org.ofbiz.entity.util.EntityUtil;
54 import org.ofbiz.service.DispatchContext;
55 import org.ofbiz.service.GenericServiceException;
56 import org.ofbiz.service.LocalDispatcher;
57 import org.ofbiz.service.ServiceUtil;
58 import org.ofbiz.security.Security;
59
60 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity;
61 import com.opensourcestrategies.crmsfa.party.PartyHelper;
62 import com.opensourcestrategies.crmsfa.util.UtilCommon;
63
64 /**
65  * Team services. The service documentation is in services_teams.xml.
66  *
67  * @author <a HREF="mailto:leon@opensourcestrategies.com">Leon Torres</a>
68  * @version $Rev: 312 $
69  */

70
71 public class TeamServices {
72
73     public static final String JavaDoc module = TeamServices.class.getName();
74
75     // TODO: if duplicates become an issue with this service, first ensure there are no ASSIGNED_TO relationships before proceeding
76
public static Map JavaDoc assignTeamToAccount(DispatchContext dctx, Map JavaDoc context) {
77         GenericDelegator delegator = dctx.getDelegator();
78         LocalDispatcher dispatcher = dctx.getDispatcher();
79         Security security = dctx.getSecurity();
80         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
81         GenericValue userLogin = (GenericValue) context.get("userLogin");
82
83         String JavaDoc accountPartyId = (String JavaDoc) context.get("accountPartyId");
84         String JavaDoc teamPartyId = (String JavaDoc) context.get("teamPartyId");
85
86         // ensure team assign permission on this account
87
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_TEAM", "_ASSIGN", userLogin, accountPartyId)) {
88             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
89         }
90         try {
91             // assign the team
92
PartyHelper.copyToPartyRelationships(teamPartyId, "ACCOUNT_TEAM", accountPartyId, "ACCOUNT", userLogin, delegator, dispatcher);
93         } catch (GenericServiceException e) {
94             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module);
95         } catch (GenericEntityException e) {
96             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module);
97         }
98         return ServiceUtil.returnSuccess();
99     }
100
101     public static Map JavaDoc addTeamMember(DispatchContext dctx, Map JavaDoc context) {
102         GenericDelegator delegator = dctx.getDelegator();
103         LocalDispatcher dispatcher = dctx.getDispatcher();
104         Security security = dctx.getSecurity();
105         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
106         GenericValue userLogin = (GenericValue) context.get("userLogin");
107
108         String JavaDoc teamMemberPartyId = (String JavaDoc) context.get("teamMemberPartyId");
109         String JavaDoc accountTeamPartyId = (String JavaDoc) context.get("accountTeamPartyId");
110         String JavaDoc securityGroupId = (String JavaDoc) context.get("securityGroupId");
111
112         // ensure team assign permission on this account
113
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_TEAM", "_ASSIGN", userLogin, accountTeamPartyId)) {
114             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
115         }
116         try {
117             // get the first valid role for the accountTeamPartyId (which could be either ACCOUNT or ACCOUT_TEAM)
118
String JavaDoc roleTypeIdFrom = PartyHelper.getFirstValidRoleTypeId(accountTeamPartyId, UtilMisc.toList("ACCOUNT", "ACCOUNT_TEAM"), delegator);
119
120             // the the first valid role for the team member (which could be either ACCOUNT_MANAGER, ACCOUNT_REP, or CUST_SERVICE_REP)
121
String JavaDoc roleTypeIdTo = PartyHelper.getFirstValidRoleTypeId(teamMemberPartyId, UtilMisc.toList("ACCOUNT_MANAGER", "ACCOUNT_REP", "CUS_SERVICE_REP"), delegator);
122
123             // find out if the candidate is already a member in this role
124
List JavaDoc relationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountTeamPartyId, "partyRelationshipTypeId", "ASSIGNED_TO",
125                         "roleTypeIdFrom", roleTypeIdFrom, "partyIdTo", teamMemberPartyId, "roleTypeIdFrom", roleTypeIdFrom));
126             List JavaDoc activeRelations = EntityUtil.filterByDate(relationships, UtilDateTime.nowTimestamp()); // filter out expired relationships
127
if (activeRelations.size() > 0) {
128                 return UtilCommon.createAndLogServiceError("CrmErrorAlreadyMember", locale, module);
129             }
130
131             // create the PartyRelationship
132
Map JavaDoc input = UtilMisc.toMap("partyIdFrom", accountTeamPartyId,"roleTypeIdFrom", roleTypeIdFrom , "partyIdTo", teamMemberPartyId, "roleTypeIdTo", roleTypeIdTo);
133             input.put("partyRelationshipTypeId", "ASSIGNED_TO");
134             input.put("securityGroupId", securityGroupId);
135             input.put("fromDate", UtilDateTime.nowTimestamp());
136             input.put("userLogin", userLogin);
137             Map JavaDoc serviceResults = dispatcher.runSync("createPartyRelationship", input);
138             if (ServiceUtil.isError(serviceResults)) {
139                 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorAssignFail", locale, module);
140             }
141         } catch (GenericServiceException e) {
142             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module);
143         } catch (GenericEntityException e) {
144             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module);
145         }
146         return ServiceUtil.returnSuccess();
147     }
148
149     public static Map JavaDoc removeTeamMember(DispatchContext dctx, Map JavaDoc context) {
150         GenericDelegator delegator = dctx.getDelegator();
151         LocalDispatcher dispatcher = dctx.getDispatcher();
152         Security security = dctx.getSecurity();
153         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
154         GenericValue userLogin = (GenericValue) context.get("userLogin");
155
156         String JavaDoc teamMemberPartyId = (String JavaDoc) context.get("teamMemberPartyId");
157         String JavaDoc accountTeamPartyId = (String JavaDoc) context.get("accountTeamPartyId");
158
159         // ensure team remove permission on this account
160
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_TEAM", "_REMOVE", userLogin, accountTeamPartyId)) {
161             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
162         }
163         try {
164             // get the first valid role for the accountTeamPartyId (which could be either ACCOUNT or ACCOUT_TEAM)
165
String JavaDoc roleTypeIdFrom = PartyHelper.getFirstValidRoleTypeId(accountTeamPartyId, UtilMisc.toList("ACCOUNT", "ACCOUNT_TEAM"), delegator);
166
167             // find all relationships where partyIdFrom = accountTeamPartyId, partyIdTo=teamMemberPartyId, roleTypeIdFrom = one just found
168
List JavaDoc relationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountTeamPartyId,
169                         "roleTypeIdFrom", roleTypeIdFrom, "partyIdTo", teamMemberPartyId));
170
171             // expire them as of now
172
PartyHelper.expirePartyRelationships(relationships, UtilDateTime.nowTimestamp(), dispatcher, userLogin);
173             
174         } catch (GenericServiceException e) {
175             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module);
176         } catch (GenericEntityException e) {
177             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module);
178         }
179         return ServiceUtil.returnSuccess();
180     }
181
182     public static Map JavaDoc setTeamMemberSecurityGroup(DispatchContext dctx, Map JavaDoc context) {
183         GenericDelegator delegator = dctx.getDelegator();
184         LocalDispatcher dispatcher = dctx.getDispatcher();
185         Security security = dctx.getSecurity();
186         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
187         GenericValue userLogin = (GenericValue) context.get("userLogin");
188
189         String JavaDoc teamMemberPartyId = (String JavaDoc) context.get("teamMemberPartyId");
190         String JavaDoc accountTeamPartyId = (String JavaDoc) context.get("accountTeamPartyId");
191         String JavaDoc securityGroupId = (String JavaDoc) context.get("securityGroupId");
192
193         // ensure team update permission on this account
194
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_TEAM", "_UPDATE", userLogin, accountTeamPartyId)) {
195             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
196         }
197         try {
198             // find the relationships of this team member on this account of relationship type ASSIGNED_TO
199
List JavaDoc relationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountTeamPartyId,
200                         "partyRelationshipTypeId", "ASSIGNED_TO", "partyIdTo", teamMemberPartyId));
201
202             // get the active relationships as of now
203
List JavaDoc activeRelations = EntityUtil.filterByDate(relationships, UtilDateTime.nowTimestamp());
204
205             // expire them as of now
206
PartyHelper.expirePartyRelationships(activeRelations, UtilDateTime.nowTimestamp(), dispatcher, userLogin);
207
208             // for each _active_ relationship (which is why we filtered them), create a new one with the changed securityGroupId
209
for (Iterator JavaDoc iter = activeRelations.iterator(); iter.hasNext(); ) {
210                 GenericValue relationship = (GenericValue) iter.next();
211
212                 Map JavaDoc input = UtilMisc.toMap("partyIdFrom", accountTeamPartyId, "roleTypeIdFrom", relationship.getString("roleTypeIdFrom"), "partyIdTo", teamMemberPartyId, "roleTypeIdTo", relationship.getString("roleTypeIdTo"));
213                 input.put("partyRelationshipTypeId", "ASSIGNED_TO");
214                 input.put("securityGroupId", securityGroupId);
215                 input.put("fromDate", UtilDateTime.nowTimestamp());
216                 input.put("userLogin", userLogin);
217                 Map JavaDoc serviceResults = dispatcher.runSync("createPartyRelationship", input);
218                 if (ServiceUtil.isError(serviceResults)) {
219                     return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorAssignFail", locale, module);
220                 }
221             }
222         } catch (GenericServiceException e) {
223             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module);
224         } catch (GenericEntityException e) {
225             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module);
226         }
227         return ServiceUtil.returnSuccess();
228     }
229 }
230
Popular Tags