001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/rights/tags/sakai_2-4-1/rights-impl/impl/src/java/org/sakaiproject/rights/impl/BaseRightsService.java $
003: * $Id: BaseRightsService.java 17729 2006-11-01 15:48:20Z lance@indiana.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.rights.impl;
021:
022: import java.util.Collection;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028: import java.util.Stack;
029: import java.util.TreeSet;
030: import java.util.Vector;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.sakaiproject.entity.api.Entity;
035: import org.sakaiproject.exception.IdUnusedException;
036: import org.sakaiproject.id.cover.IdManager;
037: import org.sakaiproject.rights.api.Copyright;
038: import org.sakaiproject.rights.api.RightsPolicy;
039: import org.sakaiproject.rights.api.RightsService;
040: import org.sakaiproject.rights.api.CreativeCommonsLicense;
041: import org.sakaiproject.rights.api.RightsAssignment;
042: import org.sakaiproject.rights.api.SiteRightsPolicy;
043: import org.sakaiproject.rights.api.UserRightsPolicy;
044: import org.sakaiproject.rights.util.RightsException;
045:
046: import org.w3c.dom.Document;
047: import org.w3c.dom.Element;
048:
049: public abstract class BaseRightsService implements RightsService {
050: public class BasicCopyright implements Copyright {
051:
052: protected String m_id;
053: protected String m_entityRef;
054: protected String m_year;
055: protected String m_owner;
056:
057: public BasicCopyright(String entityRef) {
058: m_entityRef = entityRef;
059: }
060:
061: public BasicCopyright(String entityRef, String year,
062: String owner) {
063: m_entityRef = entityRef;
064: m_year = year;
065: m_owner = owner;
066: }
067:
068: public String getCopyrightId() {
069: return m_id;
070: }
071:
072: public String getEntityRef() {
073: return m_entityRef;
074: }
075:
076: public String getOwner() {
077: return m_owner;
078: }
079:
080: public String getYear() {
081: return m_year;
082: }
083:
084: public void setOwner(String owner) {
085: m_owner = owner;
086: }
087:
088: public void setYear(String year) {
089: m_year = year;
090: }
091:
092: public Element toXml(Document doc, Stack stack) {
093: // TODO Auto-generated method stub
094: return null;
095: }
096:
097: } // class BasicCopyright
098:
099: public class BasicCreativeCommonsLicense implements
100: CreativeCommonsLicense {
101: protected String m_id;
102: protected Set m_permissions = new TreeSet();
103: protected Set m_prohibitions = new TreeSet();
104: protected Set m_requirements = new TreeSet();
105:
106: public void addPermission(Permission permission) {
107: if (m_permissions == null) {
108: m_permissions = new TreeSet();
109: }
110: m_permissions.add(permission);
111: }
112:
113: public void addPermission(String permission)
114: throws RightsException {
115: Permission p = Permission.fromString(permission);
116: if (p == null) {
117: throw new RightsException();
118: }
119: addPermission(p);
120: }
121:
122: public void addProhibition(Prohibition prohibition) {
123: if (m_prohibitions == null) {
124: m_prohibitions = new TreeSet();
125: }
126: m_prohibitions.add(prohibition);
127: }
128:
129: public void addProhibition(String prohibition)
130: throws RightsException {
131: Prohibition p = Prohibition.fromString(prohibition);
132: if (p == null) {
133: throw new RightsException();
134: }
135: addProhibition(p);
136: }
137:
138: public void addRequirement(Requirement requirement) {
139: if (m_requirements == null) {
140: m_requirements = new TreeSet();
141: }
142: m_requirements.add(requirement);
143: }
144:
145: public void addRequirement(String requirement)
146: throws RightsException {
147: Requirement r = Requirement.fromString(requirement);
148: if (r == null) {
149: throw new RightsException();
150: }
151: addRequirement(r);
152: }
153:
154: public String getIdentifier() {
155: // TODO Auto-generated method stub
156: return null;
157: }
158:
159: public Collection getPermissions() {
160: return m_permissions;
161: }
162:
163: public Collection getProhibitions() {
164: return m_prohibitions;
165: }
166:
167: public Collection getRequirements() {
168: return m_requirements;
169: }
170:
171: public String getUri() {
172: // TODO Auto-generated method stub
173: return null;
174: }
175:
176: public boolean hasPermissions() {
177: return m_permissions != null && !m_permissions.isEmpty();
178: }
179:
180: public boolean hasProhibitions() {
181: return m_prohibitions != null && !m_prohibitions.isEmpty();
182: }
183:
184: public boolean hasRequirements() {
185: return m_requirements != null && !m_requirements.isEmpty();
186: }
187:
188: public void removePermission(String permission) {
189: Permission p = Permission.fromString(permission);
190: if (p != null) {
191: this .m_permissions.remove(p);
192: }
193: }
194:
195: public void removeProhibitions(Collection prohibitions) {
196: if (prohibitions != null) {
197: Iterator it = prohibitions.iterator();
198: while (it.hasNext()) {
199: Prohibition p = null;
200: Object obj = it.next();
201: if (obj instanceof Prohibition) {
202: p = (Prohibition) obj;
203: } else if (obj instanceof String) {
204: p = Prohibition.fromString((String) obj);
205: }
206: if (p != null) {
207: this .m_prohibitions.remove(p);
208: }
209: }
210: }
211: }
212:
213: public void removeRequirements(Collection requirements) {
214: if (this .m_requirements == null) {
215: this .m_requirements = new TreeSet();
216: }
217: this .m_requirements.clear();
218:
219: if (requirements != null) {
220: Iterator it = requirements.iterator();
221: while (it.hasNext()) {
222: Requirement r = null;
223: Object obj = it.next();
224: if (obj instanceof Requirement) {
225: r = (Requirement) obj;
226: } else if (obj instanceof String) {
227: r = Requirement.fromString((String) obj);
228: }
229: if (r != null) {
230: this .m_requirements.remove(r);
231: }
232: }
233: }
234: }
235:
236: public void setPermissions(Collection permissions) {
237: if (this .m_permissions == null) {
238: this .m_permissions = new TreeSet();
239: }
240: this .m_permissions.clear();
241:
242: if (permissions != null) {
243: Iterator it = permissions.iterator();
244: while (it.hasNext()) {
245: Permission p = null;
246: Object obj = it.next();
247: if (obj instanceof Permission) {
248: p = (Permission) obj;
249: } else if (obj instanceof String) {
250: p = Permission.fromString((String) obj);
251: }
252: if (p != null) {
253: this .m_permissions.add(p);
254: }
255: }
256: }
257: }
258:
259: public void setProhibitions(Collection prohibitions) {
260: if (this .m_prohibitions == null) {
261: this .m_prohibitions = new TreeSet();
262: }
263: this .m_prohibitions.clear();
264:
265: if (prohibitions != null) {
266: Iterator it = prohibitions.iterator();
267: while (it.hasNext()) {
268: Prohibition p = null;
269: Object obj = it.next();
270: if (obj instanceof Prohibition) {
271: p = (Prohibition) obj;
272: } else if (obj instanceof String) {
273: p = Prohibition.fromString((String) obj);
274: }
275: if (p != null) {
276: this .m_prohibitions.add(p);
277: }
278: }
279: }
280: }
281:
282: public void setRequirements(Collection requirements) {
283: // TODO Auto-generated method stub
284:
285: }
286:
287: public Element toXml(Document doc, Stack stack) {
288: // TODO Auto-generated method stub
289: return null;
290: }
291:
292: } // class BasicCreativeCommonsLicense
293:
294: /**********************************************************************************************************************************************************************************************************************************************************
295: * Init and Destroy
296: *********************************************************************************************************************************************************************************************************************************************************/
297:
298: public class BasicRightsAssignment implements RightsAssignment {
299: protected Map m_licenses = new Hashtable();
300: protected Copyright m_copyright;
301: protected String m_entityRef = null;
302: protected String m_id;
303: protected boolean m_copyrightAlert = false;
304:
305: public BasicRightsAssignment(String entityRef) {
306: m_id = IdManager.createUuid();
307: m_entityRef = entityRef;
308:
309: }
310:
311: public void addLicense(CreativeCommonsLicense license) {
312: if (m_licenses == null) {
313: m_licenses = new Hashtable();
314: }
315: m_licenses.put(license.getIdentifier(), license);
316: }
317:
318: public int countLicenses() {
319: return m_licenses.size();
320: }
321:
322: public Copyright getCopyright() {
323: return m_copyright;
324: }
325:
326: public String getEntityRef() {
327: return m_entityRef;
328: }
329:
330: public Collection getLicenses() {
331: return m_licenses.values();
332: }
333:
334: public String getRightsId() {
335: return m_id;
336: }
337:
338: public boolean hasCopyright() {
339: return m_copyright != null;
340: }
341:
342: public boolean hasCopyrightAlert() {
343: return m_copyrightAlert;
344: }
345:
346: public boolean hasLicense() {
347: return m_licenses != null && !m_licenses.isEmpty();
348: }
349:
350: public void setCopyright(Copyright copyright) {
351: m_copyright = copyright;
352: }
353:
354: public void setLicenses(Collection licenses) {
355:
356: }
357:
358: public Element toXml(Document doc, Stack stack) {
359: // TODO Auto-generated method stub
360: return null;
361: }
362:
363: }
364:
365: public interface Storage {
366: public void close();
367:
368: public Copyright getCopyright(String copyrightId)
369: throws IdUnusedException;
370:
371: public CreativeCommonsLicense getLicense(String licenseId)
372: throws IdUnusedException;
373:
374: public RightsAssignment getRightsAssignment(String entityRef)
375: throws IdUnusedException;
376:
377: public RightsPolicy getRightsPolicy(String context,
378: String userId) throws IdUnusedException;
379:
380: public Copyright newCopyright(String rightsId);
381:
382: public CreativeCommonsLicense newLicense(String rightsId);
383:
384: public RightsAssignment newRightsAssignment(String entityRef);
385:
386: public RightsPolicy newRightsPolicy(String context,
387: String userId);
388:
389: public void open();
390:
391: public void remove(Copyright copyright);
392:
393: public void remove(CreativeCommonsLicense license);
394:
395: public void remove(RightsAssignment rights);
396:
397: public void remove(RightsPolicy policy);
398:
399: public String save(Copyright copyright);
400:
401: public String save(CreativeCommonsLicense license);
402:
403: public String save(RightsAssignment rights);
404:
405: public String save(RightsPolicy policy);
406:
407: }
408:
409: /** Our logger. */
410: private static Log M_log = LogFactory
411: .getLog(BaseRightsService.class);
412:
413: protected Storage m_storage = null;
414:
415: /**
416: * @param entityRef
417: * @return
418: */
419: public RightsAssignment addRightsAssignment(String entityRef) {
420: return m_storage.newRightsAssignment(entityRef);
421: }
422:
423: public SiteRightsPolicy addSiteRightsPolicy(String context) {
424: // TODO Auto-generated method stub
425: return null;
426: }
427:
428: public UserRightsPolicy addUserRightsPolicy(String context,
429: String userId) {
430: // TODO Auto-generated method stub
431: return null;
432: }
433:
434: /**
435: * Returns to uninitialized state.
436: */
437: public void destroy() {
438: m_storage.close();
439: m_storage = null;
440:
441: M_log.info("destroy()");
442:
443: }
444:
445: public RightsAssignment getRightsAssignment(String entityRef)
446: throws IdUnusedException {
447: return m_storage.getRightsAssignment(entityRef);
448: }
449:
450: public SiteRightsPolicy getSiteRightsPolicy(String context) {
451: // TODO Auto-generated method stub
452: return null;
453: }
454:
455: public UserRightsPolicy getUserRightsPolicy(String context,
456: String userId) {
457: // TODO Auto-generated method stub
458: return null;
459: }
460:
461: /**
462: * Final initialization, once all dependencies are set.
463: */
464: public void init() {
465: try {
466: // construct a storage helper and read
467: m_storage = newStorage();
468: m_storage.open();
469:
470: M_log.info("init()");
471: } catch (Throwable t) {
472: M_log.warn("init(): ", t);
473: }
474:
475: } // init
476:
477: /**
478: * Construct a Storage object.
479: *
480: * @return The new storage object.
481: */
482: protected abstract Storage newStorage();
483:
484: /**
485: * @param rights
486: */
487: public void save(RightsAssignment rights) {
488: m_storage.save(rights);
489: }
490:
491: public void save(RightsPolicy policy) {
492: m_storage.save(policy);
493: }
494:
495: public void setRightsAssignment(String entityRef,
496: RightsAssignment rights) {
497: // m_storage
498: }
499:
500: } // class BaseCopyrightService
|