View Javadoc

1   package net.sf.r4h;
2   
3   import java.io.Serializable;
4   
5   /**
6    * Restrictions that support all Hibernate restriction types and introduces {@link Restrictions.Restriction#disabledIfValueIsNull}
7    * 
8    * Allows restrictions on properties of associated entities without need of explicitly setting association path.
9    * 
10   * Can be used in environments(GWT,RMI,Web Services clients) without direct Hibernate dependencies.
11   * 
12   * The class is immutable and thread safe.
13   * 
14   * @author vitaliy.se@gmail.com
15   * 
16   * @since 1.0.0
17   */
18  public final class Restrictions implements Serializable {
19  	private static final long serialVersionUID = -7038123173749667155L;
20  
21  	/**
22  	 * Hibernate restriction types
23  	 * 
24  	 */
25  	enum Type {
26  		LESS, LESS_OR_EQUAL, EQUAL, NOT_EQUAL, GREATER_OR_EQUAL, GREATER, LIKE, ILIKE, IN,NOT_IN, IS_EMPTY, NOT_EMPTY, IS_NULL, NOT_NULL, SIZE_LESS, SIZE_LESS_OR_EQUAL, SIZE_EQUAL, SIZE_GREATER_OR_EQUAL, SIZE_GREATER, PROPERTY_LESS, PROPERTY_LESS_OR_EQUAL, PROPERTY_EQUAL, PROPERTY_GREATER_OR_EQUAL, PROPERTY_GREATER, EXAMPLE;
27  	}
28  
29  	/**
30  	 *
31  	 * Can be used in environments(GWT,RMI,Web Services clients) without direct Hibernate dependencies.
32  	 *
33  	 * Represents particular type of restriction
34  	 * 
35  	 * The class is immutable and thread safe.
36  	 *
37  	 */
38  	public static class Restriction implements Serializable {
39  
40  		/**
41  		 * Needed for GWT RPC
42  		 */
43  		private Restriction() {
44  			this(null);
45  		}
46  
47  		private static final long serialVersionUID = -293845708751983797L;
48  
49  		private final Type type;
50  
51  		private boolean disabledIfValueIsNull;
52  
53  		public boolean isDisabledIfValueIsNull() {
54  			return disabledIfValueIsNull;
55  		}
56  
57  		/**
58  		 * The restriction will not be used in case the value for restriction is null
59  		 * 
60  		 * @return Same instance of {@link Restrictions} with disabledIfValueIsNull set to true
61  		 */
62  		public Restriction disabledIfValueIsNull() {
63  			this.disabledIfValueIsNull = true;
64  			return this;
65  		}
66  
67  		private Restriction(Type type) {
68  			this.type = type;
69  		}
70  
71  		Type getType() {
72  			return type;
73  		}
74  	}
75  
76  	public static CaseAwareRestriction less() {
77  		return new CaseAwareRestriction(Type.LESS);
78  	};
79  
80  	public static CaseAwareRestriction lessOrEqual() {
81  		return new CaseAwareRestriction(Type.LESS_OR_EQUAL);
82  	};
83  
84  	public static CaseAwareRestriction equal() {
85  		return new CaseAwareRestriction(Type.EQUAL);
86  	}
87  
88  	public static CaseAwareRestriction notEqual() {
89  		return new CaseAwareRestriction(Type.NOT_EQUAL);
90  	}
91  
92  	public static CaseAwareRestriction greaterOrEqual() {
93  		return new CaseAwareRestriction(Type.GREATER_OR_EQUAL);
94  	}
95  
96  	public static CaseAwareRestriction greater() {
97  		return new CaseAwareRestriction(Type.GREATER);
98  	}
99  
100 	public static CaseAwareRestriction like() {
101 		return new CaseAwareRestriction(Type.LIKE);
102 	}
103 
104 	public static Restriction ilike() {
105 		return new Restriction(Type.ILIKE);
106 	}
107 
108 	public static EmptinessAwareRestriction in() {
109 		return new EmptinessAwareRestriction(Type.IN);
110 	}
111 	
112 	public static EmptinessAwareRestriction notIn() {
113 		return new EmptinessAwareRestriction(Type.NOT_IN);
114 	}
115 
116 	public static Restriction isEmpty() {
117 		return new Restriction(Type.IS_EMPTY);
118 	}
119 
120 	public static Restriction notEmpty() {
121 		return new Restriction(Type.NOT_EMPTY);
122 	}
123 
124 	public static Restriction isNull() {
125 		return new Restriction(Type.IS_NULL);
126 	}
127 
128 	public static Restriction notNull() {
129 		return new Restriction(Type.NOT_NULL);
130 	}
131 
132 	public static Restriction sizeLess() {
133 		return new Restriction(Type.SIZE_LESS);
134 	}
135 
136 	public static Restriction sizeLessOrEqual() {
137 		return new Restriction(Type.SIZE_LESS_OR_EQUAL);
138 	}
139 
140 	public static Restriction sizeEqual() {
141 		return new Restriction(Type.SIZE_EQUAL);
142 	}
143 
144 	public static Restriction sizeGreaterOrEqual() {
145 		return new Restriction(Type.SIZE_GREATER_OR_EQUAL);
146 	}
147 
148 	public static Restriction sizeGreater() {
149 		return new Restriction(Type.SIZE_GREATER);
150 	}
151 
152 	public static Restriction propertyLess() {
153 		return new Restriction(Type.PROPERTY_LESS);
154 	}
155 
156 	public static Restriction propertyLessOrEqual() {
157 		return new Restriction(Type.PROPERTY_LESS_OR_EQUAL);
158 	}
159 
160 	public static Restriction propertyEqual() {
161 		return new Restriction(Type.PROPERTY_EQUAL);
162 	}
163 
164 	public static Restriction propertyGreaterOrEqual() {
165 		return new Restriction(Type.PROPERTY_GREATER_OR_EQUAL);
166 	}
167 
168 	public static Restriction propertyGreater() {
169 		return new Restriction(Type.PROPERTY_GREATER);
170 	}
171 
172 	public static ExampleRestriction example() {
173 		return new ExampleRestriction();
174 	}
175 
176 	/**
177 	 * Case sensitivity aware restriction.
178 	 */
179 	public static class CaseAwareRestriction extends Restriction {
180 		private static final long serialVersionUID = 5556311249131241169L;
181 		private boolean caseSensitive = true;
182 		
183 		/**
184 		 * Needed for GWT RPC
185 		 */
186 		private CaseAwareRestriction() {
187 			this(null);
188 		}
189 
190 		private CaseAwareRestriction(Type type) {
191 			super(type);
192 		}
193 
194 		public CaseAwareRestriction caseInsensitive() {
195 			caseSensitive = false;
196 			return this;
197 		}
198 
199 		public boolean isCaseSensitive() {
200 			return caseSensitive;
201 		}
202 
203 		@Override
204 		public CaseAwareRestriction disabledIfValueIsNull() {
205 			return (CaseAwareRestriction) super.disabledIfValueIsNull();
206 		}
207 
208 	}
209 
210 	/**
211 	 * 
212 	 * This restriction allows to disable IN statement in case it is empty;
213 	 */
214 	public static class EmptinessAwareRestriction extends Restriction {
215 		private static final long serialVersionUID = 4091181099917296199L;
216 		private boolean disabledIfEmpty = false;
217 
218 		public EmptinessAwareRestriction(Type type) {
219 			super(type);
220 			if (type != Type.IN && type !=Type.NOT_IN){
221 				throw new IllegalArgumentException("Only IN and NOT_IN restrictions are supported");
222 			}
223 		}
224 
225 		public EmptinessAwareRestriction disableIfEmpty() {
226 			disabledIfEmpty = true;
227 			return this;
228 		}
229 
230 		public boolean isDisabledIfValueIsEmpty() {
231 			return disabledIfEmpty;
232 		}
233 
234 		@Override
235 		public EmptinessAwareRestriction disabledIfValueIsNull() {
236 			return (EmptinessAwareRestriction) super.disabledIfValueIsNull();
237 		}
238 
239 	}
240 
241 	/**
242 	 * Restriction for Hibernate example criteria.
243 	 */
244 	public static class ExampleRestriction extends Restriction {
245 		private static final long serialVersionUID = -6029638181078542427L;
246 		private boolean isLikeEnabled;
247 		private boolean areZeroesExcluded;
248 		private boolean isCaseIgnored;
249 		private boolean isExcludeNone;
250 
251 		private ExampleRestriction() {
252 			super(Type.EXAMPLE);
253 		}
254 
255 		public ExampleRestriction ignoreCase() {
256 			isCaseIgnored = true;
257 			return this;
258 		}
259 
260 		public ExampleRestriction enableLike() {
261 			isLikeEnabled = true;
262 			return this;
263 		}
264 
265 		public ExampleRestriction excludeZeroes() {
266 			areZeroesExcluded = true;
267 			return this;
268 		}
269 
270 		public ExampleRestriction excludeNone() {
271 			isExcludeNone = true;
272 			return this;
273 		}
274 
275 		public boolean isLikeEnabled() {
276 			return isLikeEnabled;
277 		}
278 
279 		public boolean areZeroesExcluded() {
280 			return areZeroesExcluded;
281 		}
282 
283 		public boolean isCaseIgnored() {
284 			return isCaseIgnored;
285 		}
286 
287 		public boolean isExcludeNone() {
288 			return isExcludeNone;
289 		}
290 
291 		@Override
292 		public ExampleRestriction disabledIfValueIsNull() {
293 			return (ExampleRestriction) super.disabledIfValueIsNull();
294 		}
295 	}
296 
297 }