001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.core.authorization;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.kuali.core.web.ui.Field;
020:
021: /**
022: * This class represents the authorization restrictions (or lack of) for a given field.
023: *
024: *
025: */
026: public class FieldAuthorization {
027:
028: private String fieldName;
029: private boolean editable;
030: private boolean viewable;
031:
032: /**
033: * Constructs a FieldAuthorization.java.
034: */
035: public FieldAuthorization() {
036: editable = true;
037: viewable = true;
038: }
039:
040: /**
041: *
042: * Constructs a FieldAuthorization.java.
043: *
044: * @param fieldName - name of field to represent
045: * @param canEdit - true if the field is editable in this context, false otherwise
046: * @param canView - true if thie field is viewable in this context, false otherwise
047: *
048: */
049: public FieldAuthorization(String fieldName, boolean canEdit,
050: boolean canView) {
051: this .fieldName = fieldName;
052: setEditable(canEdit); // using setters here to run impossible combinations check
053: setViewable(canView);
054: }
055:
056: /**
057: *
058: * Constructs a FieldAuthorization.java.
059: *
060: * @param fieldName - name of the field to represent
061: * @param fieldAuthorizationFlag - Field.HIDDEN, Field.READONLY, or Field.EDITABLE
062: *
063: */
064: public FieldAuthorization(String fieldName,
065: String fieldAuthorizationFlag) {
066:
067: // if an invalid flag is passed in, the choke on it
068: if (!fieldAuthorizationFlag.equals(Field.EDITABLE)
069: && !fieldAuthorizationFlag.equals(Field.READONLY)
070: && !fieldAuthorizationFlag.equals(Field.HIDDEN)) {
071: throw new IllegalArgumentException(
072: "The only allowable values are Field.HIDDEN, Field.READONLY, and Field.EDITABLE");
073: }
074:
075: this .fieldName = fieldName;
076:
077: if (fieldAuthorizationFlag.equals(Field.EDITABLE)) {
078: this .editable = true;
079: this .viewable = true;
080: }
081:
082: if (fieldAuthorizationFlag.equals(Field.READONLY)) {
083: this .editable = false;
084: this .viewable = true;
085: }
086:
087: if (fieldAuthorizationFlag.equals(Field.HIDDEN)) {
088: this .editable = false;
089: this .viewable = false;
090: }
091:
092: }
093:
094: /**
095: *
096: * This method returns the correct flag from the Kuali Field object, that corresponds to the particular combination of editable
097: * and viewable set on this object.
098: *
099: * @return Field.HIDDEN, Field.READONLY, or Field.EDITABLE
100: *
101: */
102: public String getKualiFieldDisplayFlag() {
103:
104: if (!editable && !viewable) {
105: return Field.HIDDEN;
106: }
107: if (!editable && viewable) {
108: return Field.READONLY;
109: } else {
110: return Field.EDITABLE;
111: }
112:
113: }
114:
115: /**
116: *
117: * This method returns true if the FieldAuthorization is some kind of restriction, and returns false if it is an editable field.
118: *
119: * @return boolean
120: *
121: */
122: public boolean isRestricted() {
123: if (!editable || !viewable) {
124: return true;
125: } else {
126: return false;
127: }
128: }
129:
130: /**
131: *
132: * This method returns true if this authorization prohibits Viewing and Editing, resulting in a hidden field.
133: *
134: * @return boolean
135: *
136: */
137: public boolean isHidden() {
138: if (!editable && !viewable) {
139: return true;
140: } else {
141: return false;
142: }
143: }
144:
145: /**
146: *
147: * This method returns true if this authorization prohibits Editing but not Viewing, resulting in a ReadOnly field.
148: *
149: * @return boolean
150: *
151: */
152: public boolean isReadOnly() {
153: if (!editable && viewable) {
154: return true;
155: } else {
156: return false;
157: }
158: }
159:
160: /**
161: * Gets the editable attribute.
162: *
163: * @return Returns the editable.
164: */
165: public boolean isEditable() {
166: return editable;
167: }
168:
169: /**
170: * Sets the editable attribute value.
171: *
172: * Note that if editable is being set to true, and the internal value of viewable is false, viewable will be flipped to true, to
173: * avoid impossible combinations of flags.
174: *
175: * @param editable The editable to set.
176: */
177: public void setEditable(boolean editable) {
178: if (editable && !this .viewable) {
179: this .viewable = true;
180: }
181: this .editable = editable;
182: }
183:
184: /**
185: * Gets the fieldName attribute.
186: *
187: * @return Returns the fieldName.
188: */
189: public String getFieldName() {
190: return fieldName;
191: }
192:
193: /**
194: * Sets the fieldName attribute value.
195: *
196: * @param fieldName The fieldName to set.
197: */
198: public void setFieldName(String fieldName) {
199: this .fieldName = fieldName;
200: }
201:
202: /**
203: * Gets the viewable attribute.
204: *
205: * @return Returns the viewable.
206: */
207: public boolean isViewable() {
208: return viewable;
209: }
210:
211: /**
212: * Sets the viewable attribute value.
213: *
214: * Note that if viewable is being set to false, and the internal value of editable is true, then editable will be silently
215: * flipped to false. This is done to avoid impossible combinations of authorization flags.
216: *
217: * @param viewable The viewable to set.
218: */
219: public void setViewable(boolean viewable) {
220: if (!viewable && this .editable) {
221: this .editable = false;
222: }
223: this .viewable = viewable;
224: }
225:
226: /**
227: * @see java.lang.Object#toString()
228: */
229: public String toString() {
230: StringBuffer sb = new StringBuffer();
231: sb.append(this .fieldName);
232: sb.append(" [");
233: if (this .editable) {
234: sb.append("editable");
235: } else {
236: sb.append("not editable");
237: }
238: sb.append(",");
239: if (this .viewable) {
240: sb.append("viewable");
241: } else {
242: sb.append("not viewable");
243: }
244: sb.append("]");
245: return sb.toString();
246: }
247:
248: /**
249: * @see java.lang.Object#equals(java.lang.Object)
250: */
251: public boolean equals(Object obj) {
252: boolean equal = false;
253:
254: if (obj != null) {
255: if (this .getClass().equals(obj.getClass())) {
256: FieldAuthorization other = (FieldAuthorization) obj;
257:
258: if (StringUtils.equals(this .fieldName, other
259: .getFieldName())) {
260: if (this .editable == other.isEditable()
261: && this .viewable == other.isViewable()) {
262: equal = true;
263: }
264: }
265: }
266: }
267:
268: return equal;
269: }
270:
271: /**
272: * @see java.lang.Object#hashCode()
273: */
274: public int hashCode() {
275: return toString().hashCode();
276: }
277:
278: }
|