001: /*
002: * Copyright 2004 Geert Bevin <gbevin[remove] at uwyn dot com>
003: *
004: * $Id: ConstrainedUtils.java 3750 2007-05-12 15:05:53Z gbevin $
005: */
006: package com.uwyn.rife.site;
007:
008: import com.uwyn.rife.tools.BeanUtils;
009: import com.uwyn.rife.tools.ClassUtils;
010: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
011: import java.util.Iterator;
012:
013: public class ConstrainedUtils {
014: public static Constrained getConstrainedInstance(Class beanClass) {
015: if (null == beanClass) {
016: return null;
017: }
018:
019: Constrained constrained = null;
020: if (Constrained.class.isAssignableFrom(beanClass)) {
021: try {
022: constrained = (Constrained) beanClass.newInstance();
023: } catch (Throwable e) {
024: return null;
025: }
026: }
027:
028: return constrained;
029: }
030:
031: public static Constrained makeConstrainedInstance(Object bean) {
032: if (null == bean) {
033: return null;
034: }
035:
036: Constrained constrained = null;
037: if (bean instanceof Constrained) {
038: constrained = (Constrained) bean;
039: }
040:
041: return constrained;
042: }
043:
044: public static ConstrainedProperty getConstrainedProperty(
045: Object bean, String name) {
046: if (null == bean) {
047: return null;
048: }
049:
050: Constrained constrained = ConstrainedUtils
051: .makeConstrainedInstance(bean);
052: ConstrainedProperty contrained_property = null;
053: if (constrained != null) {
054: contrained_property = constrained
055: .getConstrainedProperty(name);
056: }
057:
058: return contrained_property;
059: }
060:
061: public static String getIdentityProperty(Class beanClass) {
062: String identity_property = null;
063:
064: Constrained constrained_bean = getConstrainedInstance(beanClass);
065: if (constrained_bean != null) {
066: Iterator<ConstrainedProperty> properties_it = constrained_bean
067: .getConstrainedProperties().iterator();
068: ConstrainedProperty property;
069: while (properties_it.hasNext()) {
070: property = properties_it.next();
071: if (property.isIdentifier()) {
072: identity_property = property.getPropertyName();
073: break;
074: }
075: }
076: }
077:
078: if (null == identity_property) {
079: identity_property = "id";
080: }
081:
082: return identity_property;
083: }
084:
085: public static boolean editConstrainedProperty(Constrained bean,
086: String propertyName, String prefix) {
087: if (null == bean) {
088: return true;
089: }
090:
091: if (prefix != null && propertyName.startsWith(prefix)) {
092: propertyName = propertyName.substring(prefix.length());
093: }
094:
095: ConstrainedProperty constrained_property = bean
096: .getConstrainedProperty(propertyName);
097:
098: return !(constrained_property != null && !constrained_property
099: .isEditable());
100: }
101:
102: public static boolean persistConstrainedProperty(Constrained bean,
103: String propertyName, String prefix) {
104: if (null == bean) {
105: return true;
106: }
107:
108: if (prefix != null && propertyName.startsWith(prefix)) {
109: propertyName = propertyName.substring(prefix.length());
110: }
111:
112: ConstrainedProperty constrained_property = bean
113: .getConstrainedProperty(propertyName);
114: if (constrained_property != null) {
115: if (!constrained_property.isPersistent()
116: || constrained_property.isSameAs()
117: || constrained_property.hasManyToOneAssociation()
118: || constrained_property.hasManyToMany()
119: || constrained_property.hasManyToManyAssociation()) {
120: return false;
121: }
122:
123: if (constrained_property.hasManyToOne()
124: && !isManyToOneJoinProperty(bean.getClass(),
125: propertyName)) {
126: return false;
127: }
128: }
129:
130: return true;
131: }
132:
133: private static boolean isManyToOneJoinProperty(Class beanClass,
134: String propertyName) {
135: try {
136: Class property_type = BeanUtils.getPropertyType(beanClass,
137: propertyName);
138: if (ClassUtils.isBasic(property_type)) {
139: return true;
140: }
141: } catch (BeanUtilsException e) {
142: return true;
143: }
144:
145: return false;
146: }
147:
148: public static boolean saveConstrainedProperty(Constrained bean,
149: String propertyName, String prefix) {
150: if (null == bean) {
151: return true;
152: }
153:
154: if (prefix != null && propertyName.startsWith(prefix)) {
155: propertyName = propertyName.substring(prefix.length());
156: }
157:
158: ConstrainedProperty constrained_property = bean
159: .getConstrainedProperty(propertyName);
160: if (constrained_property != null) {
161: if (!constrained_property.isPersistent()
162: || !constrained_property.isSaved()
163: || constrained_property.isSameAs()
164: || constrained_property.hasManyToOneAssociation()
165: || constrained_property.hasManyToMany()
166: || constrained_property.hasManyToManyAssociation()) {
167: return false;
168: }
169:
170: if (constrained_property.hasManyToOne()
171: && !isManyToOneJoinProperty(bean.getClass(),
172: propertyName)) {
173: return false;
174: }
175: }
176:
177: return true;
178: }
179:
180: public static boolean fileConstrainedProperty(Constrained bean,
181: String propertyName, String prefix) {
182: if (null == bean) {
183: return false;
184: }
185:
186: if (prefix != null && propertyName.startsWith(prefix)) {
187: propertyName = propertyName.substring(prefix.length());
188: }
189:
190: ConstrainedProperty constrained_property = bean
191: .getConstrainedProperty(propertyName);
192:
193: return constrained_property != null
194: && constrained_property.isFile();
195: }
196: }
|