001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.membership.ejb;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceResourceBase;
037:
038: import org.libresource.kernel.KernelConstants;
039: import org.libresource.kernel.interfaces.KernelService;
040:
041: import org.libresource.membership.MembershipConstants;
042: import org.libresource.membership.util.GroupResourceUtil;
043:
044: import java.net.URI;
045:
046: import java.util.Collection;
047: import java.util.Vector;
048:
049: import javax.ejb.CreateException;
050:
051: /**
052: * LibreSource
053: *
054: * @author <a href="mailto:bort@loria.fr">Guillaume Bort</a> - <a
055: * href="http://www.inria.fr">INRIA Lorraine</a>
056: *
057: * A Group of the Membership service
058: *
059: * @libresource.resource name="Group" service="Membership"
060: *
061: * @ejb.finder signature= "java.util.Collection
062: * findWhereIsMember(java.lang.String membersPattern)" query
063: * ="SELECT OBJECT(g) FROM GroupResource g WHERE g.membersList LIKE
064: * ?1" transaction-type="Supports"
065: */
066: public abstract class GroupResourceBean extends LibresourceResourceBase {
067: /**
068: * @ejb.create-method
069: */
070: public String ejbCreate(String name, String description)
071: throws CreateException {
072: setId(GroupResourceUtil.generateGUID(this ));
073: setName(name);
074: setDescription(description);
075: setMembersList("");
076:
077: return null;
078: }
079:
080: // persistents fields
081:
082: /**
083: * @ejb.persistent-field
084: * @ejb.interface-method
085: * @ejb.value-object match="libresource"
086: * @ejb.transaction type="Supports"
087: */
088: public abstract String getId();
089:
090: /**
091: * @ejb.persistent-field
092: * @ejb.interface-method
093: * @ejb.transaction type="Mandatory"
094: */
095: public abstract void setId(String id);
096:
097: /**
098: * @ejb.interface-method
099: * @ejb.value-object match="libresource"
100: * @ejb.transaction type="Supports"
101: */
102: public String getShortResourceName() {
103: return getName();
104: }
105:
106: /**
107: * @ejb.persistent-field
108: * @ejb.interface-method
109: * @ejb.value-object match="libresource"
110: * @ejb.transaction type="Supports"
111: */
112: public abstract String getName();
113:
114: /**
115: * @ejb.persistent-field
116: * @ejb.interface-method
117: * @ejb.transaction type="Mandatory"
118: */
119: public abstract void setName(String name);
120:
121: /**
122: * @ejb.persistent-field
123: * @ejb.interface-method
124: * @ejb.value-object match="libresource"
125: * @ejb.transaction type="Supports"
126: */
127: public abstract String getDescription();
128:
129: /**
130: * @ejb.persistent-field
131: * @ejb.interface-method
132: * @ejb.transaction type="Mandatory"
133: */
134: public abstract void setDescription(String description);
135:
136: /**
137: * @ejb.persistent-field
138: * @ejb.interface-method
139: * @ejb.value-object match="libresource"
140: * @ejb.transaction type="Supports"
141: */
142: public abstract String getMembersList();
143:
144: /**
145: * @ejb.persistent-field
146: * @ejb.interface-method
147: * @ejb.transaction type="Mandatory"
148: */
149: public abstract void setMembersList(String members);
150:
151: // business methods
152:
153: /**
154: * @ejb.interface-method
155: * @ejb.value-object match="libresource"
156: * @ejb.transaction type="Supports"
157: */
158: public String[] getMembers() {
159: if ((getMembersList() == null)
160: || getMembersList().trim().equals("")) {
161: return new String[0];
162: }
163:
164: return getMembersList().split(",");
165: }
166:
167: /**
168: * @ejb.interface-method
169: * @ejb.value-object match="libresource"
170: * @ejb.transaction type="Mandatory"
171: */
172: public void setMembers(String[] members) {
173: StringBuffer buffer = new StringBuffer();
174:
175: for (int i = 0; i < members.length; i++) {
176: buffer.append(members[i]);
177:
178: if (i != (members.length - 1)) {
179: buffer.append(",");
180: }
181: }
182:
183: setMembersList(buffer.toString());
184: }
185:
186: /**
187: * @ejb.interface-method
188: * @ejb.transaction type="Mandatory"
189: */
190: public void removeMember(String member) throws Exception {
191: KernelService kernelService = (KernelService) Libresource
192: .getService(KernelConstants.SERVICE);
193: member = kernelService.normalizeAbsoluteURIPath(new URI(
194: getMemberUriPath(member)));
195:
196: String[] members = getMembers();
197: Vector newMembers = new Vector();
198:
199: for (int i = 0; i < members.length; i++) {
200: if (!kernelService.normalizeAbsoluteURIPath(
201: new URI(getMemberUriPath(members[i]))).equals(
202: member)) {
203: newMembers.add(members[i]);
204: }
205: }
206:
207: String[] result = new String[newMembers.size()];
208:
209: for (int i = 0; i < newMembers.size(); i++) {
210: result[i] = (String) newMembers.get(i);
211: }
212:
213: setMembers(result);
214: }
215:
216: private String getMemberUriPath(String memberId) {
217: if (memberId != null) {
218: if (memberId.startsWith("Group:")) {
219: return memberId.substring(6, memberId.length());
220: }
221:
222: if (memberId.startsWith("User:")) {
223: return memberId.substring(5, memberId.length());
224: }
225: }
226:
227: return null;
228: }
229:
230: /**
231: * @ejb.interface-method
232: * @ejb.transaction type="Mandatory"
233: */
234: public void addMember(String member) {
235: if (!isMember(member)) {
236: String[] members = getMembers();
237: String[] newMembers = new String[members.length + 1];
238: System.arraycopy(members, 0, newMembers, 0, members.length);
239: newMembers[members.length] = member;
240: setMembers(newMembers);
241: }
242: }
243:
244: /**
245: * @ejb.interface-method
246: * @ejb.transaction type="Supports"
247: */
248: public boolean isMember(String member) {
249: String[] members = getMembers();
250:
251: for (int i = 0; i < members.length; i++) {
252: if (members[i].equals(member)) {
253: return true;
254: }
255: }
256:
257: return false;
258: }
259:
260: /**
261: * @ejb.interface-method
262: * @ejb.transaction type="Supports"
263: */
264: public Collection getGroups() throws Exception {
265: Vector result = new Vector();
266: String[] members = getMembers();
267:
268: for (int i = 0; i < members.length; i++) {
269: if (members[i].startsWith("Group:")) {
270: result.add(new URI(members[i].substring(6)));
271:
272: //result.add(Libresource.findResource(new LibresourceResourceIdentifier("Membership", "Group", members[i].substring(6))));
273: }
274: }
275:
276: return result;
277: }
278: }
|