001/**
002 * Copyright 2005-2018 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.kns.document.authorization;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kns.web.ui.Field;
020import org.kuali.rice.krad.datadictionary.mask.MaskFormatter;
021
022/**
023 * This class represents the authorization restrictions (or lack of) for a given field.
024 * 
025 * 
026 */
027/**
028 * This is a description of what this class does - zjzhou don't forget to fill this in. 
029 * 
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 *
032 * @deprecated Only used in KNS classes, use KRAD.
033 */
034@Deprecated
035public class FieldRestriction {
036
037    private String fieldName;
038    private boolean editable;
039    private boolean viewable;
040    private boolean masked;
041    private boolean partiallyMasked;
042    private MaskFormatter maskFormatter;
043    private boolean shouldBeEncrypted;
044    /**
045     * Constructs a FieldAuthorization.java.
046     */
047    public FieldRestriction() {
048        editable = true;
049        viewable = true;
050    }
051
052    /**
053     * 
054     * Constructs a FieldAuthorization.java.
055     * 
056     * @param fieldName - name of field to represent
057     * @param canEdit - true if the field is editable in this context, false otherwise
058     * @param canView - true if thie field is viewable in this context, false otherwise
059     * 
060     */
061    public FieldRestriction(String fieldName, boolean canEdit, boolean canView) {
062        this.fieldName = fieldName;
063        setEditable(canEdit); // using setters here to run impossible combinations check
064        setViewable(canView);
065    }
066    
067    /**
068     * 
069     * Constructs a FieldAuthorization.java.
070     * 
071     * @param fieldName - name of the field to represent
072     * @param fieldAuthorizationFlag - Field.HIDDEN, Field.READONLY, or Field.EDITABLE
073     */
074    public FieldRestriction(String fieldName, String fieldAuthorizationFlag) {
075        // if an invalid flag is passed in, the choke on it
076        if (!fieldAuthorizationFlag.equals(Field.EDITABLE) && !fieldAuthorizationFlag.equals(Field.READONLY) 
077                        && !fieldAuthorizationFlag.equals(Field.HIDDEN) && !fieldAuthorizationFlag.equals(Field.MASKED)
078                        && !fieldAuthorizationFlag.equals(Field.PARTIALLY_MASKED)) {
079            throw new IllegalArgumentException("The only allowable values are " +
080                        "Field.HIDDEN, Field.READONLY, Field.EDITABLE, Field.MASKED and Field.PARTIALLY_MASKED");
081        }
082
083        this.fieldName = fieldName;
084
085        if (fieldAuthorizationFlag.equals(Field.EDITABLE)) {
086            this.editable = true;
087            this.viewable = true;
088        } else if (fieldAuthorizationFlag.equals(Field.READONLY)) {
089            this.editable = false;
090            this.viewable = true;
091        } else if (fieldAuthorizationFlag.equals(Field.HIDDEN)) {
092            this.editable = false;
093            this.viewable = false;
094        } else if(fieldAuthorizationFlag.equals(Field.MASKED)){
095                        this.masked = true;
096                        this.viewable = true;
097                        this.editable = false;
098                } else if(fieldAuthorizationFlag.equals(Field.PARTIALLY_MASKED)){
099                        this.partiallyMasked = true;
100                        this.viewable = true;
101                        this.editable = false;
102                }
103    }
104
105    /**
106     * 
107     * This method returns the correct flag from the Kuali Field object, that corresponds to the particular combination of editable
108     * and viewable set on this object.
109     * 
110     * @return Field.HIDDEN, Field.READONLY, or Field.EDITABLE
111     * 
112     */
113    public String getKualiFieldDisplayFlag() {
114
115        if (!editable && !viewable) {
116            return Field.HIDDEN;
117        }
118        if (!editable && viewable) {
119            return Field.READONLY;
120        }
121        else {
122            return Field.EDITABLE;
123        }
124
125    }
126
127    /**
128     * 
129     * This method returns true if the FieldAuthorization is some kind of restriction, and returns false if it is an editable field.
130     * 
131     * @return boolean
132     * 
133     */
134    public boolean isRestricted() {
135        if (!editable || !viewable) {
136            return true;
137        }
138        else {
139            return false;
140        }
141    }
142
143    /**
144     * 
145     * This method returns true if this authorization prohibits Viewing and Editing, resulting in a hidden field.
146     * 
147     * @return boolean
148     * 
149     */
150    public boolean isHidden() {
151        if (!editable && !viewable) {
152            return true;
153        }
154        else {
155            return false;
156        }
157    }
158
159    /**
160     * 
161     * This method returns true if this authorization prohibits Editing but not Viewing, resulting in a ReadOnly field.
162     * 
163     * @return boolean
164     * 
165     */
166    public boolean isReadOnly() {
167        if (!editable && viewable) {
168            return true;
169        }
170        else {
171            return false;
172        }
173    }
174
175    /**
176     * Gets the editable attribute.
177     * 
178     * @return Returns the editable.
179     */
180    public boolean isEditable() {
181        return editable;
182    }
183
184    /**
185     * Sets the editable attribute value.
186     * 
187     * Note that if editable is being set to true, and the internal value of viewable is false, viewable will be flipped to true, to
188     * avoid impossible combinations of flags.
189     * 
190     * @param editable The editable to set.
191     */
192    public void setEditable(boolean editable) {
193        if (editable && !this.viewable) {
194            this.viewable = true;
195        }
196        this.editable = editable;
197    }
198
199    /**
200     * Gets the fieldName attribute.
201     * 
202     * @return Returns the fieldName.
203     */
204    public String getFieldName() {
205        return fieldName;
206    }
207
208    /**
209     * Sets the fieldName attribute value.
210     * 
211     * @param fieldName The fieldName to set.
212     */
213    public void setFieldName(String fieldName) {
214        this.fieldName = fieldName;
215    }
216
217    /**
218     * Gets the viewable attribute.
219     * 
220     * @return Returns the viewable.
221     */
222    public boolean isViewable() {
223        return viewable;
224    }
225
226    /**
227     * Sets the viewable attribute value.
228     * 
229     * Note that if viewable is being set to false, and the internal value of editable is true, then editable will be silently
230     * flipped to false. This is done to avoid impossible combinations of authorization flags.
231     * 
232     * @param viewable The viewable to set.
233     */
234    public void setViewable(boolean viewable) {
235        if (!viewable && this.editable) {
236            this.editable = false;
237        }
238        this.viewable = viewable;
239    }
240
241    /**
242     * @see java.lang.Object#toString()
243     */
244    public String toString() {
245        StringBuffer sb = new StringBuffer();
246        sb.append(this.fieldName);
247        sb.append(" [");
248        if (this.editable) {
249            sb.append("editable");
250        }
251        else {
252            sb.append("not editable");
253        }
254        sb.append(",");
255        if (this.viewable) {
256            sb.append("viewable");
257        }
258        else {
259            sb.append("not viewable");
260        }
261        sb.append("]");
262        return sb.toString();
263    }
264
265    /**
266     * @see java.lang.Object#equals(java.lang.Object)
267     */
268    public boolean equals(Object obj) {
269        boolean equal = false;
270
271        if (obj != null) {
272            if (this.getClass().equals(obj.getClass())) {
273                FieldRestriction other = (FieldRestriction) obj;
274
275                if (StringUtils.equals(this.fieldName, other.getFieldName())) {
276                    if (this.editable == other.isEditable() && this.viewable == other.isViewable()) {
277                        equal = true;
278                    }
279                }
280            }
281        }
282
283        return equal;
284    }
285
286    /**
287     * @see java.lang.Object#hashCode()
288     */
289    public int hashCode() {
290        return toString().hashCode();
291    }
292
293        /**
294         * @return the masked
295         */
296        public boolean isMasked() {
297                return this.masked;
298        }
299
300        /**
301         * @return the partiallyMasked
302         */
303        public boolean isPartiallyMasked() {
304                return this.partiallyMasked;
305        }
306
307        
308        /**
309         * @return the shouldBeEncrypted
310         */
311        public boolean isShouldBeEncrypted() {
312                return this.shouldBeEncrypted;
313        }
314
315        /**
316         * @param shouldBeEncrypted the shouldBeEncrypted to set
317         */
318        public void setShouldBeEncrypted(boolean shouldBeEncrypted) {
319                this.shouldBeEncrypted = shouldBeEncrypted;
320        }
321
322        /**
323         * @return the maskFormatter
324         */
325        public MaskFormatter getMaskFormatter() {
326                return this.maskFormatter;
327        }
328
329        /**
330         * @param maskFormatter the maskFormatter to set
331         */
332        public void setMaskFormatter(MaskFormatter maskFormatter) {
333                this.maskFormatter = maskFormatter;
334        }
335        
336        
337}