001 package org.nakedobjects.applib;
002
003 import java.util.List;
004
005 import org.nakedobjects.applib.annotation.Hidden;
006 import org.nakedobjects.applib.query.Query;
007 import org.nakedobjects.applib.security.UserMemento;
008
009
010 /**
011 * Convenience super class for all classes that wish to interact with the container.
012 *
013 * @see org.nakedobjects.applib.DomainObjectContainer
014 */
015 public abstract class AbstractContainedObject {
016
017
018 // {{ factory methods
019 @Hidden
020 protected <T> T newTransientInstance(final Class<T> ofType) {
021 return getContainer().newTransientInstance(ofType);
022 }
023 // }}
024
025 // {{ allInstances, allMatches
026 /**
027 * Convenience method that delegates to {@link DomainObjectContainer}.
028 *
029 * @see DomainObjectContainer#allInstances(Class)
030 */
031 protected <T> List<T> allInstances(final Class<T> ofType) {
032 return getContainer().allInstances(ofType);
033 }
034 /**
035 * Convenience method that delegates to {@link DomainObjectContainer}.
036 *
037 * @see DomainObjectContainer#allMatches(Class, Filter)
038 */
039 protected <T> List<T> allMatches(final Class<T> ofType, final Filter<T> filter) {
040 return getContainer().allMatches(ofType, filter);
041 }
042
043 /**
044 * Convenience method that delegates to {@link DomainObjectContainer}.
045 *
046 * @see DomainObjectContainer#allMatches(Class, Object)
047 */
048 protected <T> List<T> allMatches(final Class<T> ofType, final T pattern) {
049 return getContainer().allMatches(ofType, pattern);
050 }
051
052 /**
053 * Convenience method that delegates to {@link DomainObjectContainer}.
054 *
055 * @see DomainObjectContainer#allMatches(Class, String)
056 */
057 protected <T> List<T> allMatches(final Class<T> ofType, final String title) {
058 return getContainer().allMatches(ofType, title);
059 }
060
061 /**
062 * Convenience method that delegates to {@link DomainObjectContainer}.
063 *
064 * @see DomainObjectContainer#allMatches(Query)
065 */
066 protected <T> List<T> allMatches(final Query<T> query) {
067 return getContainer().allMatches(query);
068 }
069 // }}
070
071 // {{ firstMatch
072 /**
073 * Convenience method that delegates to {@link DomainObjectContainer}.
074 *
075 * @see DomainObjectContainer#firstMatch(Class, Filter)
076 */
077 protected <T> T firstMatch(final Class<T> ofType, final Filter<T> filter) {
078 return getContainer().firstMatch(ofType, filter);
079 }
080
081 /**
082 * Convenience method that delegates to {@link DomainObjectContainer}.
083 *
084 * @see DomainObjectContainer#firstMatch(Class, Object)
085 */
086 protected <T> T firstMatch(final Class<T> ofType, final T pattern) {
087 return getContainer().firstMatch(ofType, pattern);
088 }
089
090 /**
091 * Convenience method that delegates to {@link DomainObjectContainer}.
092 *
093 * @see DomainObjectContainer#firstMatch(Class, String)
094 */
095 protected <T> T firstMatch(final Class<T> ofType, final String title) {
096 return getContainer().firstMatch(ofType, title);
097 }
098
099 /**
100 * Convenience method that delegates to {@link DomainObjectContainer}.
101 *
102 * @see DomainObjectContainer#firstMatch(Query)
103 */
104 protected <T> T firstMatch(final Query<T> query) {
105 return getContainer().firstMatch(query);
106 }
107 // }}
108
109
110 // {{ uniqueMatch
111 /**
112 * Convenience method that delegates to {@link DomainObjectContainer}.
113 *
114 * @see DomainObjectContainer#uniqueMatch(Class, Filter)
115 */
116 protected <T> T uniqueMatch(final Class<T> ofType, final Filter<T> filter) {
117 return getContainer().uniqueMatch(ofType, filter);
118 }
119
120 /**
121 * Convenience method that delegates to {@link DomainObjectContainer}.
122 *
123 * @see DomainObjectContainer#uniqueMatch(Class, String)
124 */
125 protected <T> T uniqueMatch(final Class<T> ofType, final String title) {
126 return getContainer().uniqueMatch(ofType, title);
127 }
128
129 /**
130 * Convenience method that delegates to {@link DomainObjectContainer}.
131 *
132 * @see DomainObjectContainer#uniqueMatch(Class, Object)
133 */
134 protected <T> T uniqueMatch(final Class<T> ofType, final T pattern) {
135 return getContainer().uniqueMatch(ofType, pattern);
136 }
137
138 /**
139 * Convenience method that delegates to {@link DomainObjectContainer}.
140 *
141 * @see DomainObjectContainer#uniqueMatch(Query)
142 */
143 protected <T> T uniqueMatch(final Query<T> query) {
144 return getContainer().uniqueMatch(query);
145 }
146 // }}
147
148
149 // {{ isValid, validate
150 /**
151 * Convenience methods that delegates to {@link DomainObjectContainer}.
152 *
153 * @see DomainObjectContainer#isValid(Object)
154 */
155 protected boolean isValid(Object domainObject) {
156 return getContainer().isValid(domainObject);
157 }
158 /**
159 * Convenience methods that delegates to {@link DomainObjectContainer}.
160 *
161 * @see DomainObjectContainer#validate(Object)
162 */
163 protected String validate(Object domainObject) {
164 return getContainer().validate(domainObject);
165 }
166 // }}
167
168 // {{ isPersistent, persist, remove
169 /**
170 * Whether the provided object is persistent.
171 */
172 @Hidden
173 protected boolean isPersistent(final Object domainObject) {
174 return getContainer().isPersistent(domainObject);
175 }
176
177 /**
178 * Save provided object to the persistent object store.
179 *
180 * <p>
181 * If the object {@link #isPersistent(Object) is persistent} already, then
182 * will throw an exception.
183 *
184 * @see #persistIfNotAlready(Object)
185 */
186 @Hidden
187 protected void persist(final Object transientDomainObject) {
188 getContainer().persist(transientDomainObject);
189 }
190
191 /**
192 * Saves the object, but only if not already {@link #isPersistent() persistent}.
193 *
194 * @see #isPersistent()
195 * @see #persist(Object)
196 */
197 @Hidden
198 protected void persistIfNotAlready(final Object domainObject) {
199 getContainer().persistIfNotAlready(domainObject);
200 }
201
202 /**
203 * Delete the provided object from the persistent object store.
204 */
205 protected void remove(final Object persistentDomainObject) {
206 getContainer().remove(persistentDomainObject);
207 }
208 // }}
209
210
211
212 // {{ Error/Warning Methods
213 /**
214 * Display the specified message to the user, in a non-intrusive fashion.
215 */
216 protected void informUser(final String message) {
217 getContainer().informUser(message);
218 }
219
220 /**
221 * Display the specified message as a warning to the user, in a more visible fashion, but without
222 * requiring explicit acknowledgement.
223 */
224 protected void warnUser(final String message) {
225 getContainer().warnUser(message);
226 }
227
228 /**
229 * Display the specified message as an error to the user, ensuring that it is acknowledged.
230 */
231 protected void raiseError(final String message) {
232 getContainer().raiseError(message);
233 }
234 // }}
235
236 // {{ Security Methods
237 protected UserMemento getUser() {
238 return getContainer().getUser();
239 }
240 // }}
241
242
243 // {{ Injected: Container
244 private DomainObjectContainer container;
245
246 /**
247 * This field is not persisted, nor displayed to the user.
248 */
249 @Hidden
250 protected DomainObjectContainer getContainer() {
251 return this.container;
252 }
253
254 /**
255 * Injected by the application container itself.
256 */
257 public final void setContainer(final DomainObjectContainer container) {
258 this.container = container;
259 }
260 // }}
261
262 }
263
264 // Copyright (c) Naked Objects Group Ltd.