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.kuali.core.service.DataDictionaryService;
024: import org.kuali.core.service.DocumentService;
025: import org.kuali.core.util.ObjectUtils;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.module.purap.document.PurchasingAccountsPayableDocument;
028:
029: import edu.iu.uis.eden.Id;
030: import edu.iu.uis.eden.engine.RouteContext;
031: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
032: import edu.iu.uis.eden.exception.WorkflowException;
033: import edu.iu.uis.eden.routetemplate.ResolvedQualifiedRole;
034: import edu.iu.uis.eden.routetemplate.Role;
035: import edu.iu.uis.eden.routetemplate.UnqualifiedRoleAttribute;
036: import edu.iu.uis.eden.user.AuthenticationUserId;
037:
038: /**
039: * TODO delyea - documentation
040: */
041: public class PurApSourceDocumentRouteUserRoleAttribute extends
042: UnqualifiedRoleAttribute {
043: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
044: .getLogger(PurApSourceDocumentRouteUserRoleAttribute.class);
045:
046: private static final String SOURCE_DOC_ROUTED_BY_USER_ROLE_KEY = "SOURCE_DOC_ROUTED_BY_USER";
047: private static final String SOURCE_DOC_ROUTED_BY_USER_ROLE_LABEL = "User who Routed Source Document";
048:
049: private static final Role ROLE = new Role(
050: PurApSourceDocumentRouteUserRoleAttribute.class,
051: SOURCE_DOC_ROUTED_BY_USER_ROLE_KEY,
052: SOURCE_DOC_ROUTED_BY_USER_ROLE_LABEL);
053: private static final List<Role> ROLES;
054: static {
055: ArrayList<Role> roles = new ArrayList<Role>(1);
056: roles.add(ROLE);
057: ROLES = Collections.unmodifiableList(roles);
058: }
059:
060: /**
061: * @see edu.iu.uis.eden.routetemplate.UnqualifiedRoleAttribute#getRoleNames()
062: */
063: @Override
064: public List<Role> getRoleNames() {
065: return ROLES;
066: }
067:
068: private void assertDocumentNotNull(
069: PurchasingAccountsPayableDocument document) {
070: if (ObjectUtils.isNull(document)) {
071: String errorMessage = "Document with doc id '"
072: + document.getDocumentNumber() + "' and class '"
073: + document.getClass()
074: + "' does not exist in system";
075: LOG.error("resolveRole() " + errorMessage);
076: throw new RuntimeException(errorMessage);
077: }
078: }
079:
080: /**
081: * TODO delyea - documentation
082: *
083: * @param routeContext the RouteContext
084: * @param roleName the role name
085: * @return a ResolvedQualifiedRole
086: */
087: @Override
088: public ResolvedQualifiedRole resolveRole(RouteContext routeContext,
089: String roleName) throws EdenUserNotFoundException {
090: String documentNumber = null;
091: try {
092: documentNumber = routeContext.getDocument()
093: .getRouteHeaderId().toString();
094: PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) SpringContext
095: .getBean(DocumentService.class)
096: .getByDocumentHeaderId(documentNumber);
097: assertDocumentNotNull(document);
098: document.refreshNonUpdateableReferences();
099: PurchasingAccountsPayableDocument sourceDocument = document
100: .getPurApSourceDocumentIfPossible();
101: // method getSourceDocumentIfPossible() could return null but for using this instance we should get something back
102: assertDocumentNotNull(sourceDocument);
103: // return the user who routed the source document
104: DataDictionaryService dataDictionaryService = SpringContext
105: .getBean(DataDictionaryService.class);
106: String label = "User Who Routed "
107: + document.getPurApSourceDocumentLabelIfPossible()
108: + " " + sourceDocument.getPurapDocumentIdentifier();
109: return new ResolvedQualifiedRole(label, Arrays
110: .asList(new Id[] { new AuthenticationUserId(
111: sourceDocument.getDocumentHeader()
112: .getWorkflowDocument()
113: .getRoutedByUserNetworkId()) }));
114: } catch (WorkflowException e) {
115: String errorMessage = "Workflow problem while trying to get document using doc id '"
116: + documentNumber + "'";
117: LOG.error("resolveRole() " + errorMessage, e);
118: throw new RuntimeException(errorMessage, e);
119: }
120: }
121: }
|