001: /*
002: * Copyright 2005-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.core.service.impl;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.kuali.core.datadictionary.DataDictionary;
022: import org.kuali.core.datadictionary.DocumentEntry;
023: import org.kuali.core.document.Document;
024: import org.kuali.core.document.authorization.DocumentAuthorizer;
025: import org.kuali.core.service.AuthorizationService;
026: import org.kuali.core.service.DataDictionaryService;
027: import org.kuali.core.service.DocumentAuthorizationService;
028: import org.springframework.transaction.annotation.Transactional;
029:
030: /**
031: * Most frequently, isAuthorized(group,action,targetType) will be called from isAuthorized(user,action,target) from inside the loop,
032: * so it'd be a good idea to optimize getting an answer for a given group...
033: */
034: @Transactional
035: public class DocumentAuthorizationServiceImpl implements
036: DocumentAuthorizationService {
037: private static Log LOG = LogFactory
038: .getLog(DocumentAuthorizationServiceImpl.class);
039: private AuthorizationService authorizationService;
040: private DataDictionaryService dataDictionaryService;
041:
042: /**
043: * @see org.kuali.core.service.DocumentAuthorizationService#getDocumentAuthorizer(java.lang.String)
044: */
045: public DocumentAuthorizer getDocumentAuthorizer(String documentType) {
046: DataDictionary dataDictionary = getDataDictionaryService()
047: .getDataDictionary();
048:
049: if (StringUtils.isBlank(documentType)) {
050: throw new IllegalArgumentException(
051: "invalid (blank) documentType");
052: }
053:
054: DocumentEntry documentEntry = dataDictionary
055: .getDocumentEntry(documentType);
056: if (documentEntry == null) {
057: throw new IllegalArgumentException("unknown documentType '"
058: + documentType + "'");
059: }
060:
061: Class documentAuthorizerClass = documentEntry
062: .getDocumentAuthorizerClass();
063:
064: DocumentAuthorizer documentAuthorizer = null;
065: try {
066: documentAuthorizer = (DocumentAuthorizer) documentAuthorizerClass
067: .newInstance();
068: } catch (InstantiationException e) {
069: throw new RuntimeException(
070: "unable to instantiate documentAuthorizer '"
071: + documentAuthorizerClass.getName()
072: + "' for doctype '" + documentType + "'", e);
073: } catch (IllegalAccessException e) {
074: throw new RuntimeException(
075: "unable to instantiate documentAuthorizer '"
076: + documentAuthorizerClass.getName()
077: + "' for doctype '" + documentType + "'", e);
078: }
079:
080: return documentAuthorizer;
081: }
082:
083: /**
084: * @see org.kuali.core.service.DocumentAuthorizationService#getDocumentAuthorizer(org.kuali.core.document.Document)
085: */
086: public DocumentAuthorizer getDocumentAuthorizer(Document document) {
087: if (document == null) {
088: throw new IllegalArgumentException(
089: "invalid (null) document");
090: } else if (document.getDocumentHeader() == null) {
091: throw new IllegalArgumentException(
092: "invalid (null) document.documentHeader");
093: } else if (!document.getDocumentHeader().hasWorkflowDocument()) {
094: throw new IllegalArgumentException(
095: "invalid (null) document.documentHeader.workflowDocument");
096: }
097:
098: String documentType = document.getDocumentHeader()
099: .getWorkflowDocument().getDocumentType();
100:
101: DocumentAuthorizer documentAuthorizer = getDocumentAuthorizer(documentType);
102: return documentAuthorizer;
103: }
104:
105: /* spring-injected services */
106: public void setDataDictionaryService(
107: DataDictionaryService dataDictionaryService) {
108: this .dataDictionaryService = dataDictionaryService;
109: }
110:
111: public DataDictionaryService getDataDictionaryService() {
112: return this.dataDictionaryService;
113: }
114: }
|