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.krad.datadictionary;
017
018import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
019import org.kuali.rice.krad.datadictionary.exception.ClassValidationException;
020import org.kuali.rice.krad.document.Document;
021import org.kuali.rice.krad.maintenance.MaintenanceDocumentAuthorizer;
022import org.kuali.rice.krad.maintenance.MaintenanceDocumentAuthorizerBase;
023import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase;
024import org.kuali.rice.krad.maintenance.Maintainable;
025import org.kuali.rice.krad.maintenance.MaintenanceDocumentPresentationControllerBase;
026
027import java.util.ArrayList;
028import java.util.List;
029
030/**
031 * Data dictionary entry class for <code>MaintenanceDocument</code>
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class MaintenanceDocumentEntry extends DocumentEntry {
036    private static final long serialVersionUID = 4990040987835057251L;
037
038    protected Class<?> dataObjectClass;
039    protected Class<? extends Maintainable> maintainableClass;
040
041    protected List<String> lockingKeys = new ArrayList<String>();
042
043    protected boolean allowsNewOrCopy = true;
044    protected boolean preserveLockingKeysOnCopy = false;
045    protected boolean allowsRecordDeletion = false;
046
047    public MaintenanceDocumentEntry() {
048        super();
049
050        setDocumentClass(getStandardDocumentBaseClass());
051        documentAuthorizerClass = MaintenanceDocumentAuthorizerBase.class;
052        documentPresentationControllerClass = MaintenanceDocumentPresentationControllerBase.class;
053    }
054
055    public Class<? extends Document> getStandardDocumentBaseClass() {
056        return MaintenanceDocumentBase.class;
057    }
058
059    /*
060            This attribute is used in many contexts, for example, in maintenance docs, it's used to specify the classname
061            of the BO being maintained.
062     */
063    public void setDataObjectClass(Class<?> dataObjectClass) {
064        if (dataObjectClass == null) {
065            throw new IllegalArgumentException("invalid (null) dataObjectClass");
066        }
067
068        this.dataObjectClass = dataObjectClass;
069    }
070
071    public Class<?> getDataObjectClass() {
072        return dataObjectClass;
073    }
074
075    /**
076     * @see org.kuali.rice.krad.datadictionary.DocumentEntry#getEntryClass()
077     */
078    @SuppressWarnings("unchecked")
079    @Override
080    public Class getEntryClass() {
081        return dataObjectClass;
082    }
083
084    /*
085            The maintainableClass element specifies the name of the
086            java class which is responsible for implementing the
087            maintenance logic.
088            The normal one is KualiMaintainableImpl.java.
089     */
090    public void setMaintainableClass(Class<? extends Maintainable> maintainableClass) {
091        if (maintainableClass == null) {
092            throw new IllegalArgumentException("invalid (null) maintainableClass");
093        }
094        this.maintainableClass = maintainableClass;
095    }
096
097    public Class<? extends Maintainable> getMaintainableClass() {
098        return maintainableClass;
099    }
100
101    /**
102     * @return List of all lockingKey fieldNames associated with this LookupDefinition, in the order in which they were
103     *         added
104     */
105    public List<String> getLockingKeyFieldNames() {
106        return lockingKeys;
107    }
108
109    /**
110     * Gets the allowsNewOrCopy attribute.
111     *
112     * @return Returns the allowsNewOrCopy.
113     */
114    public boolean getAllowsNewOrCopy() {
115        return allowsNewOrCopy;
116    }
117
118    /**
119     * The allowsNewOrCopy element contains a value of true or false.
120     * If true, this indicates the maintainable should allow the
121     * new and/or copy maintenance actions.
122     */
123    public void setAllowsNewOrCopy(boolean allowsNewOrCopy) {
124        this.allowsNewOrCopy = allowsNewOrCopy;
125    }
126
127    /**
128     * Directly validate simple fields, call completeValidation on Definition fields.
129     *
130     * @see org.kuali.rice.krad.datadictionary.DocumentEntry#completeValidation()
131     */
132    public void completeValidation() {
133        super.completeValidation();
134
135        for (String lockingKey : lockingKeys) {
136            if (!DataDictionary.isPropertyOf(dataObjectClass, lockingKey)) {
137                throw new AttributeValidationException(
138                        "unable to find attribute '" + lockingKey + "' for lockingKey in dataObjectClass '" +
139                                dataObjectClass.getName());
140            }
141        }
142
143        for (ReferenceDefinition reference : defaultExistenceChecks) {
144            reference.completeValidation(dataObjectClass, null);
145        }
146
147
148        if (documentAuthorizerClass != null &&
149                !MaintenanceDocumentAuthorizer.class.isAssignableFrom(documentAuthorizerClass)) {
150            throw new ClassValidationException(
151                    "This maintenance document for '" + getDataObjectClass().getName() + "' has an invalid " +
152                            "documentAuthorizerClass ('" + documentAuthorizerClass.getName() + "').  " +
153                            "Maintenance Documents must use an implementation of MaintenanceDocumentAuthorizer.");
154        }
155    }
156
157    /**
158     * @see java.lang.Object#toString()
159     */
160    public String toString() {
161        return "MaintenanceDocumentEntry for documentType " + getDocumentTypeName();
162    }
163
164    public List<String> getLockingKeys() {
165        return lockingKeys;
166    }
167
168    /*
169           The lockingKeys element specifies a list of fields
170           that comprise a unique key.  This is used for record locking
171           during the file maintenance process.
172    */
173    public void setLockingKeys(List<String> lockingKeys) {
174        for (String lockingKey : lockingKeys) {
175            if (lockingKey == null) {
176                throw new IllegalArgumentException("invalid (null) lockingKey");
177            }
178        }
179        this.lockingKeys = lockingKeys;
180    }
181
182    /**
183     * @return the preserveLockingKeysOnCopy
184     */
185    public boolean getPreserveLockingKeysOnCopy() {
186        return this.preserveLockingKeysOnCopy;
187    }
188
189    /**
190     * @param preserveLockingKeysOnCopy the preserveLockingKeysOnCopy to set
191     */
192    public void setPreserveLockingKeysOnCopy(boolean preserveLockingKeysOnCopy) {
193        this.preserveLockingKeysOnCopy = preserveLockingKeysOnCopy;
194    }
195
196    /**
197     * @return the allowRecordDeletion
198     */
199    public boolean getAllowsRecordDeletion() {
200        return this.allowsRecordDeletion;
201    }
202
203    /**
204     * @param allowsRecordDeletion the allowRecordDeletion to set
205     */
206    public void setAllowsRecordDeletion(boolean allowsRecordDeletion) {
207        this.allowsRecordDeletion = allowsRecordDeletion;
208    }
209
210}