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