001/**
002 * Copyright 2005-2017 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.krad.bo.PersistableBusinessObjectBase;
020import org.kuali.rice.krad.data.jpa.converters.Boolean01BigDecimalConverter;
021
022import javax.persistence.Column;
023import javax.persistence.Convert;
024import javax.persistence.Entity;
025import javax.persistence.Id;
026import javax.persistence.IdClass;
027import javax.persistence.JoinColumn;
028import javax.persistence.ManyToOne;
029import javax.persistence.Table;
030import javax.persistence.Transient;
031
032import static org.kuali.rice.kew.api.doctype.DocumentTypePolicy.*;
033
034/**
035 * Model bean representing a policy of a document type.
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039@Entity
040@IdClass(DocumentTypePolicyId.class)
041@Table(name="KREW_DOC_TYP_PLCY_RELN_T")
042public class DocumentTypePolicy extends PersistableBusinessObjectBase {
043    private static final long serialVersionUID = -4612246888683336474L;
044
045    @Transient
046    private String documentTypeId;
047
048    @Id
049    @Column(name = "DOC_PLCY_NM", nullable = false)
050    private String policyName;
051
052    @Column(name="PLCY_NM", nullable = false)
053    @Convert(converter=Boolean01BigDecimalConverter.class)
054    private Boolean policyValue;
055
056    @Column(name="PLCY_VAL")
057    private String policyStringValue;
058
059    @Id
060    @JoinColumn(name="DOC_TYP_ID", nullable = false)
061    @ManyToOne
062    private DocumentType documentType;
063
064    @Transient private Boolean inheritedFlag;
065
066    public DocumentTypePolicy() {}
067
068    public DocumentTypePolicy(String documentTypeId, String policyName, Boolean policyValue) {
069        this.documentTypeId = documentTypeId;
070        this.policyName = policyName;
071        this.policyValue = policyValue;
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 documentTypeId;
107    }
108
109    public void setDocumentTypeId(String documentTypeId) {
110        this.documentTypeId = documentTypeId;
111    }
112
113    public String getPolicyName() {
114        return policyName;
115    }
116
117    public void setPolicyName(String policyName) {
118        /* Cleanse the input.
119         * This is surely not the best way to validate the policy name;
120         * it would probably be better to use typesafe enums across the board
121         * but that would probably entail refactoring large swaths of code, not
122         * to mention reconfiguring OJB (can typesafe enums be used?) and dealing
123         * with serialization compatibility issues (if any).
124         * So instead, let's just be sure to fail-fast.
125         */
126        org.kuali.rice.kew.api.doctype.DocumentTypePolicy policy = fromCode(policyName);
127        this.policyName = policy.getCode();
128    }
129
130    public Boolean getPolicyValue() {
131        return policyValue;
132    }
133
134    public void setPolicyValue(Boolean policyValue) {
135        this.policyValue = policyValue;
136    }
137
138    public String getPolicyStringValue() {
139        return policyStringValue;
140    }
141
142    public void setPolicyStringValue(String policyStringValue) {
143        this.policyStringValue = policyStringValue;
144    }
145
146    /**
147     * Return the actual value from the policy
148     *
149     * If there is a policy string value it will return it, else return the policy value (a boolean).
150     * This was needed for building the XML representation to return from the document type service.
151     *
152     * @return string of policy value
153     */
154    public String getActualPolicyValue() {
155        if (this.policyStringValue != null && this.policyStringValue.length() > 0) {
156            return this.policyStringValue;
157        }
158
159        return this.policyValue.toString();
160    }
161
162    public Object copy(boolean preserveKeys) {
163        DocumentTypePolicy clone = new DocumentTypePolicy();
164        if (preserveKeys) {
165            //clone.setDocumentTypeId(getDocumentTypeId());
166            clone.setPolicyName(getPolicyName());
167        }
168        if (policyValue != null) {
169            clone.setPolicyValue(new Boolean(policyValue.booleanValue()));
170        }
171        if (policyStringValue != null) {
172            clone.setPolicyStringValue(new String(policyStringValue));
173        }
174        return clone;
175    }
176
177    public DocumentType getDocumentType() {
178        return documentType;
179    }
180
181    public void setDocumentType(DocumentType documentType) {
182        this.documentType = documentType;
183    }
184
185}