001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.rice.kew.doctype;
017
018import org.kuali.rice.kew.doctype.bo.DocumentType;
019import org.kuali.rice.kew.api.KewApiConstants;
020import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
021
022import javax.persistence.*;
023
024import static org.kuali.rice.kew.api.doctype.DocumentTypePolicy.*;
025
026/**
027 * Model bean representing a policy of a document type.
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031@Entity
032@Table(name="KREW_DOC_TYP_PLCY_RELN_T")
033public class DocumentTypePolicy extends PersistableBusinessObjectBase {
034        private static final long serialVersionUID = -4612246888683336474L;
035
036        @EmbeddedId
037        private DocumentTypePolicyId documentTypePolicyId;
038    @Column(name="PLCY_NM")
039        private Boolean policyValue;
040    @Column(name="PLCY_VAL")
041    private String policyStringValue;
042    @Transient
043    private Boolean inheritedFlag;
044    @MapsId("documentTypeId")
045    @ManyToOne(fetch=FetchType.EAGER)
046        @JoinColumn(name="DOC_TYP_ID")
047        private DocumentType documentType;
048    
049    @Transient
050    private String documentTypeId;
051    @Transient
052    private String policyName;
053
054    public DocumentTypePolicy() {
055    }
056
057    public DocumentTypePolicy(String policyName, Boolean policyValue) {
058        this.policyName = policyName;
059        this.getDocumentTypePolicyId().setPolicyName(policyName);
060        this.policyValue = policyValue;
061    }
062
063    public DocumentTypePolicyId getDocumentTypePolicyId() {
064        if (this.documentTypePolicyId == null) {
065                this.documentTypePolicyId = new DocumentTypePolicyId();
066        }
067                return this.documentTypePolicyId;
068        }
069
070        public void setDocumentTypePolicyId(DocumentTypePolicyId documentTypePolicyId) {
071                this.documentTypePolicyId = documentTypePolicyId;
072        }
073
074        public String getPolicyDisplayValue() {
075        if(policyValue != null){
076            if(policyValue.booleanValue()){
077                return "Active";
078            } else {
079                return "Inactive";
080            }
081        }
082        return "Inherited";
083    }
084
085    public Boolean getInheritedFlag() {
086        return inheritedFlag;
087    }
088
089    public void setInheritedFlag(Boolean inheritedFlag) {
090        this.inheritedFlag = inheritedFlag;
091    }
092    
093    public boolean isAllowUnrequestedAction() {
094        return ALLOW_UNREQUESTED_ACTION.equals(this.getPolicyName());
095    }
096
097    public boolean isDefaultApprove() {
098        return DEFAULT_APPROVE.equals(this.getPolicyName());
099    }
100
101    public boolean isDisApprove() {
102        return DISAPPROVE.getCode().equals(this.getPolicyName());
103    }
104
105    public String getDocumentTypeId() {
106        return (getDocumentTypePolicyId().getDocumentTypeId() != null) ? getDocumentTypePolicyId().getDocumentTypeId() : this.documentTypeId;
107    }
108
109    public void setDocumentTypeId(String documentTypeId) {
110        this.documentTypeId = documentTypeId;
111        this.getDocumentTypePolicyId().setDocumentTypeId(documentTypeId);
112    }
113
114    public String getPolicyName() {
115        return (this.getDocumentTypePolicyId().getPolicyName() != null) ? this.getDocumentTypePolicyId().getPolicyName() : this.policyName;
116    }
117
118    public void setPolicyName(String policyName) {
119        /* Cleanse the input.
120         * This is surely not the best way to validate the policy name;
121         * it would probably be better to use typesafe enums accross the board
122         * but that would probably entail refactoring large swaths of code, not
123         * to mention reconfiguring OJB (can typesafe enums be used?) and dealing
124         * with serialization compatibility issues (if any).
125         * So instead, let's just be sure to fail-fast.
126         */
127        org.kuali.rice.kew.api.doctype.DocumentTypePolicy policy = fromCode(policyName);
128        this.policyName = policy.getCode();
129        this.getDocumentTypePolicyId().setPolicyName(policy.getCode());
130    }
131
132    public Boolean getPolicyValue() {
133        return policyValue;
134    }
135
136    public void setPolicyValue(Boolean policyValue) {
137        this.policyValue = policyValue;
138    }
139
140    public String getPolicyStringValue() {
141        return policyStringValue;
142    }
143
144    public void setPolicyStringValue(String policyStringValue) {
145        this.policyStringValue = policyStringValue;
146    }
147
148    public Object copy(boolean preserveKeys) {
149        DocumentTypePolicy clone = new DocumentTypePolicy();
150
151        if(preserveKeys && this.getDocumentTypeId() != null){
152            clone.setDocumentTypeId(this.getDocumentTypeId());
153        }
154        if(this.getPolicyName() != null){
155            clone.setPolicyName(new String(this.getPolicyName()));
156        }
157
158        if(policyValue != null){
159            clone.setPolicyValue(new Boolean(policyValue.booleanValue()));
160        }
161        
162        if(policyStringValue != null){
163            clone.setPolicyStringValue(new String(policyStringValue));
164        }
165
166        return clone;
167    }
168
169    public DocumentType getDocumentType() {
170        return documentType;
171    }
172
173    public void setDocumentType(DocumentType documentType) {
174        this.documentType = documentType;
175    }
176}