001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/entity/tags/sakai_2-4-1/entity-impl/impl/src/java/org/sakaiproject/entity/impl/ReferenceComponent.java $
003: * $Id: ReferenceComponent.java 29631 2007-04-26 14:39:15Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.entity.impl;
021:
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.Vector;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.entity.api.Entity;
029: import org.sakaiproject.entity.api.EntityProducer;
030: import org.sakaiproject.entity.api.Reference;
031: import org.sakaiproject.entity.api.ResourceProperties;
032: import org.sakaiproject.entity.cover.EntityManager;
033: import org.sakaiproject.user.api.User;
034: import org.sakaiproject.user.cover.UserDirectoryService;
035:
036: /**
037: * <p>
038: * Implementation of the Reference API
039: * </p>
040: * <p>
041: * Note: a Reference is immutable.
042: * </p>
043: */
044: public class ReferenceComponent implements Reference {
045: /** Our logger. */
046: protected static Log M_log = LogFactory
047: .getLog(ReferenceComponent.class);
048:
049: /** The reference string. */
050: protected String m_reference = null;
051:
052: /** The reference type (a service name string). */
053: protected String m_type = "";
054:
055: /** The reference sub-type. */
056: protected String m_subType = "";
057:
058: /** The reference primary id. */
059: protected String m_id = null;
060:
061: /** The reference containment ids. */
062: protected String m_container = null;
063:
064: /** Another container, the context id. */
065: protected String m_context = null;
066:
067: /** Set to true once the values are set. */
068: protected boolean m_setAlready = false;
069:
070: /** The service owning the entity. */
071: protected EntityProducer m_service = null;
072:
073: /** These are some more services known. */
074: public static final String GRADEBOOK_ROOT = "gradebook";
075:
076: public static final String GRADTOOLS_ROOT = "dissertation";
077:
078: /**
079: * Construct with a reference string.
080: *
081: * @param ref
082: * The resource reference.
083: */
084: public ReferenceComponent(String ref) {
085: m_reference = ref;
086: parse();
087: }
088:
089: /**
090: * Construct with a Reference.
091: *
092: * @param ref
093: * The resource reference.
094: */
095: public ReferenceComponent(Reference copyMe) {
096: ReferenceComponent ref = (ReferenceComponent) copyMe;
097:
098: m_reference = ref.m_reference;
099: m_type = ref.m_type;
100: m_subType = ref.m_subType;
101: m_id = ref.m_id;
102: m_container = ref.m_container;
103: m_context = ref.m_context;
104: m_service = ref.m_service;
105: }
106:
107: /**
108: * Access the reference.
109: *
110: * @return The reference.
111: */
112: public String getReference() {
113: return m_reference;
114: }
115:
116: /**
117: * Access the type, a service id string.
118: *
119: * @return The type, a service id string.
120: */
121: public String getType() {
122: return m_type;
123: }
124:
125: /**
126: * Check if the reference's type is known
127: *
128: * @return true if known, false if not.
129: */
130: public boolean isKnownType() {
131: return m_type.length() > 0;
132: }
133:
134: /**
135: * Access the subType.
136: *
137: * @return The subType.
138: */
139: public String getSubType() {
140: return m_subType;
141: }
142:
143: /**
144: * Access the primary id.
145: *
146: * @return The primary id.
147: */
148: public String getId() {
149: return m_id;
150: }
151:
152: /**
153: * Access a single container id, the from most general (or only)
154: *
155: * @return The single or most general container, if any.
156: */
157: public String getContainer() {
158: return m_container;
159: }
160:
161: /**
162: * Access the context id, if any.
163: *
164: * @return the context id, if any.
165: */
166: public String getContext() {
167: return m_context;
168: }
169:
170: /**
171: * Find the ResourceProperties object for this reference.
172: *
173: * @return A ResourcesProperties object found (or constructed) for this reference.
174: */
175: public ResourceProperties getProperties() {
176: ResourceProperties props = null;
177:
178: if (m_service != null) {
179: props = m_service.getEntityResourceProperties(this );
180: }
181:
182: return props;
183: }
184:
185: /**
186: * Find the Entity that is referenced.
187: *
188: * @return The Entity object that this references.
189: */
190: public Entity getEntity() {
191: Entity e = null;
192:
193: if (m_service != null) {
194: e = m_service.getEntity(this );
195: }
196:
197: return e;
198: }
199:
200: /**
201: * Access the URL which can be used to access the referenced resource.
202: *
203: * @return The URL which can be used to access the referenced resource.
204: */
205: public String getUrl() {
206: String url = null;
207:
208: if (m_service != null) {
209: url = m_service.getEntityUrl(this );
210: }
211:
212: return url;
213: }
214:
215: /**
216: * @return a description of the resource referenced.
217: */
218: public String getDescription() {
219: String rv = "unknown";
220:
221: if (m_service != null) {
222: rv = m_service.getEntityDescription(this );
223:
224: if (rv == null) {
225: rv = m_service.getLabel() + " " + m_reference;
226: }
227: }
228:
229: return rv;
230: }
231:
232: /**
233: * {@inheritDoc}
234: */
235: public Collection getAuthzGroups() {
236: return getAuthzGroups(null);
237: }
238:
239: /**
240: * {@inheritDoc}
241: */
242: public Collection getAuthzGroups(String userId) {
243: Collection realms = null;
244:
245: if (m_service != null) {
246: realms = m_service.getEntityAuthzGroups(this , userId);
247: }
248:
249: if (realms == null)
250: realms = new Vector();
251:
252: return realms;
253: }
254:
255: /**
256: * Add the AuthzGroup(s) for context as a site.
257: *
258: * @param rv
259: * The list.
260: */
261: public void addSiteContextAuthzGroup(Collection rv) {
262: String context = getContext();
263: if (context == null)
264: return;
265:
266: // site using context as id
267: // TODO: taken from site -ggolden was: rv.add(SiteService.siteReference(getContext()));
268: rv.add("/site/" + context);
269:
270: // site helper
271: rv.add("!site.helper");
272: }
273:
274: /**
275: * Add the AuthzGroup for this user id, or for the user's type template, or for the general template.
276: *
277: * @param rv
278: * The list.
279: * @param id
280: * The user id.
281: */
282: public void addUserAuthzGroup(Collection rv, String id) {
283: if (id == null)
284: id = "";
285:
286: // the user's realm (unless it's anon)
287: if (id.length() > 0)
288: rv.add(UserDirectoryService.userReference(id));
289:
290: addUserTemplateAuthzGroup(rv, id);
291: }
292:
293: /**
294: * Add the AuthzGroup for this user id, or for the user's type template, or for the general template.
295: *
296: * @param rv
297: * The list.
298: * @param id
299: * The user id.
300: */
301: public void addUserTemplateAuthzGroup(Collection rv, String id) {
302: if (id == null)
303: id = "";
304:
305: // user type template
306: String template = "!user.template";
307: try {
308: User user = UserDirectoryService.getUser(id);
309: String type = user.getType();
310: if (type != null) {
311: rv.add(template + "." + type);
312: }
313: } catch (Exception ignore) {
314: }
315:
316: // general user template
317: rv.add("!user.template");
318: }
319:
320: /**
321: * Accept the settings for a reference - may be rejected if already set
322: *
323: * @param type
324: * @param subType
325: * @param id
326: * @param container
327: * @param container2
328: * @param context
329: * @return true if settings are accepted, false if not.
330: */
331: public boolean set(String type, String subType, String id,
332: String container, String context) {
333: if (m_setAlready)
334: return false;
335:
336: // these must not be null
337: m_type = type;
338: m_subType = subType;
339: if (m_type == null)
340: m_type = "";
341: if (m_subType == null)
342: m_subType = "";
343:
344: // these should be null if empty
345: m_id = id;
346: m_container = container;
347: m_context = context;
348: if ((m_id != null) && (m_id.length() == 0))
349: m_id = null;
350: if ((m_container != null) && (m_container.length() == 0))
351: m_container = null;
352: if ((m_context != null) && (m_context.length() == 0))
353: m_context = null;
354:
355: m_setAlready = true;
356:
357: return true;
358: }
359:
360: /**
361: * @inheritDoc
362: */
363: public void updateReference(String ref) {
364: m_reference = ref;
365: }
366:
367: /**
368: * @inheritDoc
369: */
370: public EntityProducer getEntityProducer() {
371: return m_service;
372: }
373:
374: /*
375: * Parse the reference
376: */
377: protected void parse() {
378: if (m_reference == null)
379: return;
380:
381: // check with the resource services to see if anyone recognizes this
382: for (Iterator iServices = EntityManager.getEntityProducers()
383: .iterator(); iServices.hasNext();) {
384: EntityProducer service = (EntityProducer) iServices.next();
385: // give each a chance to recognize and parse the reference string, filling in this Reference with a call to set()
386: if (service.parseEntityReference(m_reference, this )) {
387: // save the service
388: m_service = service;
389:
390: // done
391: return;
392: }
393: }
394:
395: if (M_log.isDebugEnabled())
396: M_log.debug("parse(): unhandled reference: " + m_reference);
397: }
398: }
|