001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.workflow.module.purap.attribute;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collections;
021: import java.util.List;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.kuali.core.exceptions.UserNotFoundException;
025: import org.kuali.core.service.DocumentService;
026: import org.kuali.core.service.UniversalUserService;
027: import org.kuali.core.util.KualiDecimal;
028: import org.kuali.core.util.ObjectUtils;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.kfs.service.ParameterService;
031: import org.kuali.kfs.service.impl.ParameterConstants;
032: import org.kuali.module.purap.PurapParameterConstants;
033: import org.kuali.module.purap.document.PurchaseOrderDocument;
034: import org.kuali.module.purap.document.PurchasingDocumentBase;
035: import org.kuali.module.purap.service.PurchaseOrderService;
036: import org.kuali.workflow.KualiWorkflowUtils;
037:
038: import edu.iu.uis.eden.Id;
039: import edu.iu.uis.eden.engine.RouteContext;
040: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
041: import edu.iu.uis.eden.exception.WorkflowException;
042: import edu.iu.uis.eden.routeheader.DocumentContent;
043: import edu.iu.uis.eden.routetemplate.ResolvedQualifiedRole;
044: import edu.iu.uis.eden.routetemplate.Role;
045: import edu.iu.uis.eden.routetemplate.RuleExtension;
046: import edu.iu.uis.eden.routetemplate.UnqualifiedRoleAttribute;
047: import edu.iu.uis.eden.workgroup.GroupNameId;
048:
049: /**
050: * A document should stop in the Internal Purchasing Review route node if the following conditions are met:<br>
051: * <ol>
052: * <li>The document is not an 'Automatic Purchase Order'
053: * <li>The 'routing user' was not a contract manager
054: * <li>The 'po internal amount limit' is not null and the document total amount is greater than the 'po internal amount limit'<br>
055: * (see
056: * {@link org.kuali.module.purap.service.PurchaseOrderService#getInternalPurchasingDollarLimit(org.kuali.module.purap.document.PurchaseOrderDocument, String, String)}
057: * for more details on the 'po internal amount limit')
058: * </ol>
059: */
060: public class KualiInternalPurchasingRoleAttribute extends
061: UnqualifiedRoleAttribute {
062: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
063: .getLogger(KualiInternalPurchasingRoleAttribute.class);
064:
065: private static final String INTERNAL_PURCHASING_ROLE_KEY = "INTERNAL_PURCHASING_REVIEWER";
066: private static final String INTERNAL_PURCHASING_ROLE_LABEL = "Internal Purchasing Reviewer";
067: private static final Role ROLE = new Role(
068: KualiInternalPurchasingRoleAttribute.class,
069: INTERNAL_PURCHASING_ROLE_KEY,
070: INTERNAL_PURCHASING_ROLE_LABEL);
071:
072: private static final List ROLES;
073: static {
074: ArrayList list = new ArrayList(0);
075: list.add(ROLE);
076: ROLES = Collections.unmodifiableList(list);
077: }
078:
079: /**
080: * @see edu.iu.uis.eden.routetemplate.UnqualifiedRoleAttribute#getRoleNames()
081: */
082: @Override
083: public List<Role> getRoleNames() {
084: return ROLES;
085: }
086:
087: /**
088: * @see edu.iu.uis.eden.routetemplate.AbstractRoleAttribute#isMatch(edu.iu.uis.eden.routeheader.DocumentContent, java.util.List)
089: */
090: @Override
091: public boolean isMatch(DocumentContent docContent,
092: List<RuleExtension> ruleExtensions) {
093: String documentNumber = docContent.getRouteContext()
094: .getDocument().getRouteHeaderId().toString();
095: String authenticationId = "(not found)";
096: try {
097: authenticationId = docContent.getRouteContext()
098: .getDocument().getRoutedByUser()
099: .getAuthenticationUserId().getAuthenticationId();
100: if (StringUtils.isNotEmpty(authenticationId)) {
101: String contractManagersGroupName = SpringContext
102: .getBean(ParameterService.class)
103: .getParameterValue(
104: ParameterConstants.PURCHASING_DOCUMENT.class,
105: PurapParameterConstants.WorkflowParameters.PurchaseOrderDocument.CONTRACT_MANAGERS_WORKGROUP_NAME);
106: if (!SpringContext.getBean(UniversalUserService.class)
107: .getUniversalUserByAuthenticationUserId(
108: authenticationId.toUpperCase())
109: .isMember(contractManagersGroupName)) {
110: // get the document id number from the routeContext doc content
111: PurchasingDocumentBase document = (PurchasingDocumentBase) SpringContext
112: .getBean(DocumentService.class)
113: .getByDocumentHeaderId(documentNumber);
114: document.refreshNonUpdateableReferences();
115: KualiDecimal internalPurchasingLimit = SpringContext
116: .getBean(PurchaseOrderService.class)
117: .getInternalPurchasingDollarLimit(
118: (PurchaseOrderDocument) document);
119: return ((ObjectUtils
120: .isNull(internalPurchasingLimit)) || (internalPurchasingLimit
121: .compareTo(KualiWorkflowUtils
122: .getFinancialDocumentTotalAmount(docContent
123: .getDocument())) < 0));
124: }
125: return false;
126: } else {
127: String errorMsg = "Error while processing doc id '"
128: + documentNumber
129: + "'... Authentication Id not found from routed by user.";
130: LOG.error(errorMsg);
131: throw new RuntimeException(errorMsg);
132: }
133: } catch (EdenUserNotFoundException u) {
134: String errorMsg = "Error trying to get routed by user from doc id '"
135: + documentNumber + "'";
136: LOG.error(errorMsg, u);
137: throw new RuntimeException(errorMsg, u);
138: } catch (WorkflowException we) {
139: String errorMsg = "Error trying to get document using doc id '"
140: + documentNumber + "'";
141: LOG.error(errorMsg, we);
142: throw new RuntimeException(errorMsg, we);
143: } catch (UserNotFoundException e) {
144: String errorMsg = "Error while processing doc id '"
145: + documentNumber
146: + "'... User not found using Authentication Id '"
147: + authenticationId + "'";
148: LOG.error(errorMsg, e);
149: throw new RuntimeException(errorMsg, e);
150: }
151: }
152:
153: /**
154: * TODO delyea - documentation
155: *
156: * @param routeContext the RouteContext
157: * @param roleName the role name
158: * @return a ResolvedQualifiedRole
159: */
160: @Override
161: public ResolvedQualifiedRole resolveRole(RouteContext routeContext,
162: String roleName) throws EdenUserNotFoundException {
163: // assume isMatch above has done it's job
164: String workgroupName = SpringContext
165: .getBean(ParameterService.class)
166: .getParameterValue(
167: ParameterConstants.PURCHASING_DOCUMENT.class,
168: PurapParameterConstants.WorkflowParameters.PurchaseOrderDocument.INTERNAL_PURCHASING_WORKGROUP_NAME);
169: return new ResolvedQualifiedRole(
170: INTERNAL_PURCHASING_ROLE_LABEL, Arrays
171: .asList(new Id[] { new GroupNameId(
172: workgroupName) }));
173: }
174: }
|