001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.community.mc.impl.jdo;
013:
014: import com.sun.portal.community.mc.CMCException;
015: import com.sun.portal.community.mc.CMCPrincipal;
016: import com.sun.portal.community.mc.ConfigTable;
017: import com.sun.portal.community.mc.ConfigTable.ConfigKey;
018: import com.sun.portal.community.mc.CMCRolePrincipal;
019: import com.sun.portal.community.mc.impl.jdo.CMCImpl.QueryParameterList;
020: import com.sun.portal.community.mc.impl.jdo.CMCImpl.ConfigKeyOrQueryParameterList;
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.HashMap;
026: import java.util.Properties;
027: import java.util.Set;
028: import java.util.Collections;
029: import java.util.regex.Pattern;
030: import javax.jdo.PersistenceManager;
031: import javax.jdo.PersistenceManagerFactory;
032: import javax.jdo.Query;
033: import javax.jdo.Transaction;
034: import javax.jdo.Extent;
035: import javax.jdo.JDOException;
036: import com.sun.portal.community.mc.impl.jdo.pc.CommunityUser;
037: import com.sun.portal.community.mc.impl.jdo.pc.CommunityRole;
038: import com.sun.portal.community.mc.impl.jdo.pc.CommunityDp;
039:
040: /**
041: * JDO community user constributor.
042: */
043: public class CMCUserImpl extends CMCImpl implements
044: com.sun.portal.community.mc.CMCUser {
045: private String userId = null;
046:
047: public void init(Properties p, String userId) throws CMCException {
048: this .userId = userId;
049: }
050:
051: public String getUserId() {
052: return userId;
053: }
054:
055: public Set getMembership() throws CMCException {
056: return getMembership(null);
057: }
058:
059: public Set getMembership(Set rolePrincipals) throws CMCException {
060: Set byName = getMembershipByName(rolePrincipals);
061: //System.out.println("byName=" + byName);
062: Set byRole = Collections.EMPTY_SET;
063: if (byName.size() > 0) {
064: byRole = getMembershipByRole(rolePrincipals);
065: }
066: //System.out.println("byRole=" + byRole);
067:
068: Set all = byName;
069: all.addAll(byRole);
070:
071: return all;
072: }
073:
074: public Set getMembershipByName() throws CMCException {
075: return getMembershipByName(null);
076: }
077:
078: public Set getMembershipByName(Set rolePrincipals)
079: throws CMCException {
080: Set membership = new HashSet();
081:
082: PersistenceManager pm = getPersistenceManager();
083: Transaction tx = pm.currentTransaction();
084:
085: QueryParameterList qpl = new RoleEqualsQueryParameterList(
086: rolePrincipals);
087:
088: try {
089: tx.begin();
090:
091: Extent e = pm
092: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityUser.class);
093: Query q = pm.newQuery(e);
094:
095: String filter = qpl.toFilterString("userName == user_name");
096: String imports = qpl
097: .toImportsString("import java.lang.String");
098: String params = qpl
099: .toParameterString("java.lang.String user_name");
100:
101: q.setFilter(filter);
102: q.declareImports(imports);
103: q.declareParameters(params);
104: Map parameterMap = qpl.getParameterMap();
105: parameterMap.put("user_name", getUserId());
106:
107: Collection results = (Collection) q
108: .executeWithMap(parameterMap);
109:
110: for (Iterator i = results.iterator(); i.hasNext();) {
111: CommunityUser cu = (CommunityUser) i.next();
112: ConfigKey ck = new ConfigKey(new CMCPrincipal(
113: getType(), cu.getCommunityName()), RoleMapper
114: .getCMCRolePrincipalValue(cu.getRoleId()));
115: membership.add(ck);
116: }
117:
118: tx.commit();
119: } catch (JDOException jdoe) {
120: throw new CMCException(jdoe);
121: } finally {
122: if (tx.isActive()) {
123: tx.rollback();
124: }
125: pm.close();
126: }
127:
128: return membership;
129: }
130:
131: public Set getMembershipByRole() throws CMCException {
132: return getMembershipByRole(null);
133: }
134:
135: public Set getMembershipByRole(Set rolePrincipals)
136: throws CMCException {
137: Set membership = new HashSet();
138:
139: try {
140: PersistenceManager pm = getPersistenceManager();
141: Transaction tx = pm.currentTransaction();
142:
143: QueryParameterList qpl = new RoleEqualsQueryParameterList(
144: rolePrincipals);
145:
146: try {
147: tx.begin();
148:
149: Extent e = pm
150: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityRole.class);
151: Query q = pm.newQuery(e);
152:
153: String filter = qpl
154: .toFilterString("communityName == aUser.communityName && aUser.userName == user_name");
155: String imports = qpl
156: .toImportsString("import java.lang.String; import com.sun.portal.community.mc.impl.jdo.pc.CommunityUser");
157: String params = qpl
158: .toParameterString("String user_name");
159: String vars = "CommunityUser aUser";
160:
161: q.setFilter(filter);
162: q.declareImports(imports);
163: q.declareParameters(params);
164: q.declareVariables(vars);
165: Map parameterMap = qpl.getParameterMap();
166: parameterMap.put("user_name", getUserId());
167:
168: Collection results = (Collection) q
169: .executeWithMap(parameterMap);
170:
171: for (Iterator i = results.iterator(); i.hasNext();) {
172: CommunityRole cr = (CommunityRole) i.next();
173: ConfigKey ck = new ConfigKey(new CMCPrincipal(
174: getType(), cr.getCommunityName()),
175: RoleMapper.getCMCRolePrincipalValue(cr
176: .getRoleId()));
177: membership.add(ck);
178: }
179:
180: tx.commit();
181: } catch (JDOException jdoe) {
182: throw new CMCException(jdoe);
183: } finally {
184: if (tx.isActive()) {
185: tx.rollback();
186: }
187: pm.close();
188: }
189: } catch (Throwable t) {
190: t.printStackTrace();
191: }
192: return membership;
193: }
194:
195: public Set getAvailable() throws CMCException {
196: Set available = new HashSet();
197:
198: PersistenceManager pm = getPersistenceManager();
199: Transaction tx = pm.currentTransaction();
200:
201: try {
202: tx.begin();
203:
204: Extent e = pm
205: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.Community.class);
206: Query q = pm.newQuery(e);
207: // q.setFilter(""); no filter required
208: q.setResult("distinct communityName");
209: Collection results = (Collection) q.execute();
210:
211: for (Iterator i = results.iterator(); i.hasNext();) {
212: String name = (String) i.next();
213: CMCPrincipal cp = new CMCPrincipal(getType(), name);
214: available.add(cp);
215: }
216:
217: tx.commit();
218: } catch (JDOException jdoe) {
219: throw new CMCException(jdoe);
220: } finally {
221: if (tx.isActive()) {
222: tx.rollback();
223: }
224: pm.close();
225: }
226:
227: return available;
228: }
229:
230: public boolean hasRole(CMCPrincipal cp, CMCRolePrincipal rp)
231: throws CMCException {
232: boolean hasRole = false;
233:
234: PersistenceManager pm = getPersistenceManager();
235: Transaction tx = pm.currentTransaction();
236:
237: try {
238: tx.begin();
239:
240: Extent e = pm
241: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityUser.class);
242: Query q = pm.newQuery(e);
243: q
244: .setFilter("userName == user_name && roleId == role_id && community.communityName == community_name");
245: q.declareImports("import java.lang.String");
246: q
247: .declareParameters("java.lang.String user_name,java.lang.Integer role_id,java.lang.String community_name");
248: q.setResult("distinct this");
249: Map parameterMap = new HashMap();
250: parameterMap.put("user_name", getUserId());
251: parameterMap.put("role_id", new Integer(RoleMapper
252: .getIntValue(rp.getName())));
253: parameterMap.put("community_name", cp.getName());
254: q.setUnique(true);
255:
256: CommunityUser cu = (CommunityUser) q
257: .executeWithMap(parameterMap);
258:
259: if (cu != null) {
260: hasRole = true;
261: }
262:
263: tx.commit();
264:
265: if (!hasRole) {
266: tx.begin();
267:
268: e = pm
269: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityRole.class);
270: q = pm.newQuery(e);
271: q
272: .setFilter("community.communityName == community_name && roleId == role_id");
273: q
274: .declareImports("import java.lang.String; import java.lang.Integer");
275: q
276: .declareParameters("String community_name,Integer role_id");
277: q.setResult("distinct this");
278: parameterMap = new HashMap();
279: parameterMap.put("community_name", cp.getName());
280: parameterMap.put("role_id", new Integer(RoleMapper
281: .getIntValue(rp.getName())));
282: q.setUnique(true);
283:
284: CommunityRole cr = (CommunityRole) q
285: .executeWithMap(parameterMap);
286:
287: if (cr != null) {
288: hasRole = true;
289: }
290:
291: tx.commit();
292: }
293: } catch (JDOException jdoe) {
294: throw new CMCException(jdoe);
295: } finally {
296: if (tx.isActive()) {
297: tx.rollback();
298: }
299: pm.close();
300: }
301:
302: return hasRole;
303: }
304:
305: public long getRoleCreationTime(CMCPrincipal cp, CMCRolePrincipal rp)
306: throws CMCException {
307: boolean hasRole;
308:
309: PersistenceManager pm = getPersistenceManager();
310: Transaction tx = pm.currentTransaction();
311: long time = -1;
312:
313: try {
314: tx.begin();
315:
316: Extent e = pm
317: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityUser.class);
318: Query q = pm.newQuery(e);
319: q
320: .setFilter("userName == user_name && roleId == role_id && community.communityName == community_name");
321: q
322: .declareImports("import java.lang.String; import java.lang.Integer");
323: q
324: .declareParameters("String user_name,Integer role_id,String community_name");
325: q.setResult("distinct this");
326: Map parameterMap = new HashMap();
327: parameterMap.put("user_name", getUserId());
328: parameterMap.put("role_id", new Integer(RoleMapper
329: .getIntValue(rp.getName())));
330: parameterMap.put("community_name", cp.getName());
331: q.setUnique(true);
332:
333: CommunityUser cu = (CommunityUser) q
334: .executeWithMap(parameterMap);
335:
336: if (cu == null) {
337: time = -1;
338: } else {
339: time = cu.getCreationTime();
340: }
341:
342: tx.commit();
343: } catch (JDOException jdoe) {
344: throw new CMCException(jdoe);
345: } finally {
346: if (tx.isActive()) {
347: tx.rollback();
348: }
349: pm.close();
350: }
351:
352: return time;
353: }
354:
355: public ConfigTable getDPDocuments(ConfigTable lastReadTimes)
356: throws CMCException {
357: ConfigTable byName = getDPDocuments(lastReadTimes, null);
358: //System.out.println("byName=" + byName);
359:
360: // since no explicit membership set, add in the DP documents that match
361: // by pattern
362: ConfigTable byPattern = getDPDocumentsByRole(lastReadTimes);
363: //System.out.println("byPattern=" + byPattern);
364:
365: ConfigTable all = byName;
366: all.putAll(byPattern);
367:
368: return all;
369: }
370:
371: public ConfigTable getDPDocuments(ConfigTable lastReadTimes,
372: Set membership) throws CMCException {
373: //
374: // if membership == null, that means we need to determine the membership
375: // in this operation. we will buld the query differently, to select
376: // on the user name as well
377: //
378: // if membership != null, that means the membership has been determined
379: // somewhere else, and we do not need to validate it here. we blindly
380: // query on the documents identified by the config keys.
381: //
382: // if membership.size() == 0, that means that the membership was already
383: // determined to be empty, so this entire operation is a no-op
384: //
385:
386: ConfigTable dpDocuments = new ConfigTable();
387:
388: if (membership != null) {
389: membership = filterMembership(membership);
390: if (membership.size() == 0) {
391: return dpDocuments;
392: }
393: }
394:
395: //
396: // this qualifies the query for the given membership
397: // if null, this is a no-op
398: //
399: QueryParameterList qpl = new ConfigKeyOrQueryParameterList(
400: membership);
401:
402: PersistenceManager pm = getPersistenceManager();
403: Transaction tx = pm.currentTransaction();
404:
405: try {
406: tx.begin();
407:
408: Extent e = pm
409: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityDp.class);
410: Query q = pm.newQuery(e);
411:
412: String filter = null;
413: String imports = null;
414: String params = null;
415: String vars = null;
416: Map parameterMap = null;
417:
418: //
419: // if membership is null, that means the query must filter on the
420: // user ID as well as the given membership
421: //
422: if (membership == null) {
423: filter = "community.communityUsers.contains(aUser) && aUser.userName == user_name && aUser.roleId == roleId";
424: imports = "import java.lang.String; import com.sun.portal.community.mc.impl.jdo.pc.CommunityUser";
425: params = "String user_name";
426: vars = "CommunityUser aUser";
427: }
428:
429: filter = qpl.toFilterString(filter);
430: //System.out.println("filter=" + filter);
431: imports = qpl.toImportsString(imports);
432: //System.out.println("imports=" + imports);
433: params = qpl.toParameterString(params);
434: //System.out.println("params=" + params);
435:
436: q.setFilter(filter);
437: q.declareImports(imports);
438: q.declareParameters(params);
439: q.declareVariables(vars);
440:
441: parameterMap = qpl.getParameterMap();
442: //System.out.println("parameterMap=" + parameterMap);
443:
444: if (membership == null) {
445: parameterMap.put("user_name", getUserId());
446: }
447:
448: Collection results = (Collection) q
449: .executeWithMap(parameterMap);
450:
451: for (Iterator i = results.iterator(); i.hasNext();) {
452: //
453: // warning: this will make additional calls to the
454: // persistent store if a document is found ot be modified.
455: //
456: // it boils down to this:
457: // is it better to go ahead and get it knowing that we might
458: // not need it ... or to get it in a second trip
459: // in the unlikely event that we need it. the answer
460: // i suppose is "how unlikely is unlikely." well, the desktop
461: // will be the primary (only?) consumer of this. except
462: // at login, it will almost never need to retrieve any of the
463: // DPs ... seems like leaving it out of the DFG is a good
464: // thing.
465: //
466:
467: CommunityDp cdp = (CommunityDp) i.next();
468: CMCPrincipal cp = new CMCPrincipal(getType(), cdp
469: .getCommunityName());
470: CMCRolePrincipal rp = RoleMapper
471: .getCMCRolePrincipalValue(cdp.getRoleId());
472: ConfigKey ck = new ConfigKey(cp, rp);
473:
474: //
475: // check last modified against last read
476: // note that this might be able to be done more
477: // efficiently by including the test in the query
478: // not sure how to build such a query though
479: //
480:
481: Long lastRead = (Long) lastReadTimes.get(ck);
482: if (lastRead != null && lastRead.longValue() != -1) {
483: //
484: // this means the client passed us a last read time
485: // if the LM time is less than the LR time,
486: // that means the client has the latest version
487: // do not return it to them
488: //
489: long lm = cdp.getLastModified();
490: long lr = lastRead.longValue();
491: if (lm <= lr) {
492: continue;
493: }
494:
495: }
496:
497: //
498: // we're here: document is modified so add it to the
499: // return value note that the dp field is not in the
500: // default fetch group, so we have not retrieved it
501: // until we call getDp() ... which is good for
502: // performance (probably)
503: //
504: byte[] dp = cdp.getDp();
505: dpDocuments.put(ck, dp);
506:
507: //
508: // set the last read time for the client
509: //
510: lastReadTimes.put(ck, new Long(System
511: .currentTimeMillis()));
512: }
513:
514: tx.commit();
515: } catch (JDOException jdoe) {
516: throw new CMCException(jdoe);
517: } finally {
518: if (tx.isActive()) {
519: tx.rollback();
520: }
521: pm.close();
522: }
523: return dpDocuments;
524: }
525:
526: public ConfigTable getDPDocumentsByRole(ConfigTable lastReadTimes)
527: throws CMCException {
528: ConfigTable dpDocuments = new ConfigTable();
529:
530: PersistenceManager pm = getPersistenceManager();
531: Transaction tx = pm.currentTransaction();
532:
533: try {
534: tx.begin();
535:
536: Extent e = pm
537: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityDp.class);
538: Query q = pm.newQuery(e);
539:
540: String filter = null;
541: String imports = null;
542: String params = null;
543: String vars = null;
544: Map parameterMap = null;
545:
546: filter = "community.communityRoles.contains(aRole) && aRole.roleId == roleId";
547: imports = "import java.lang.String; import com.sun.portal.community.mc.impl.jdo.pc.CommunityRole";
548: params = "String user_name";
549: vars = "CommunityRole aRole";
550:
551: //System.out.println("filter=" + filter);
552: //System.out.println("imports=" + imports);
553: //System.out.println("params=" + params);
554:
555: q.setFilter(filter);
556: q.declareImports(imports);
557: q.declareParameters(params);
558: q.declareVariables(vars);
559:
560: parameterMap = Collections.singletonMap("user_name",
561: getUserId());
562: //System.out.println("parameterMap=" + parameterMap);
563:
564: Collection results = (Collection) q
565: .executeWithMap(parameterMap);
566:
567: for (Iterator i = results.iterator(); i.hasNext();) {
568: //
569: // warning: this will make additional calls to the
570: // persistent store if a document is found ot be modified.
571: //
572: // it boils down to this:
573: // is it better to go ahead and get it knowing that we might
574: // not need it ... or to get it in a second trip
575: // in the unlikely event that we need it. the answer
576: // i suppose is "how unlikely is unlikely." well, the desktop
577: // will be the primary (only?) consumer of this. except
578: // at login, it will almost never need to retrieve any of the
579: // DPs ... seems like leaving it out of the DFG is a good
580: // thing.
581: //
582:
583: CommunityDp cdp = (CommunityDp) i.next();
584: CMCPrincipal cp = new CMCPrincipal(getType(), cdp
585: .getCommunityName());
586: CMCRolePrincipal rp = RoleMapper
587: .getCMCRolePrincipalValue(cdp.getRoleId());
588: ConfigKey ck = new ConfigKey(cp, rp);
589:
590: //
591: // check last modified against last read
592: // note that this might be able to be done more
593: // efficiently by including the test in the query
594: // not sure how to build such a query though
595: //
596:
597: Long lastRead = (Long) lastReadTimes.get(ck);
598: if (lastRead != null && lastRead.longValue() != -1) {
599: //
600: // this means the client passed us a last read time
601: // if the LM time is less than the LR time,
602: // that means the client has the latest version
603: // do not return it to them
604: //
605: long lm = cdp.getLastModified();
606: long lr = lastRead.longValue();
607: if (lm <= lr) {
608: continue;
609: }
610:
611: }
612:
613: //
614: // we're here: document is modified so add it to the
615: // return value note that the dp field is not in the
616: // default fetch group, so we have not retrieved it
617: // until we call getDp() ... which is good for
618: // performance (probably)
619: //
620: byte[] dp = cdp.getDp();
621: dpDocuments.put(ck, dp);
622:
623: //
624: // set the last read time for the client
625: //
626: lastReadTimes.put(ck, new Long(System
627: .currentTimeMillis()));
628: }
629:
630: tx.commit();
631: } catch (JDOException jdoe) {
632: throw new CMCException(jdoe);
633: } finally {
634: if (tx.isActive()) {
635: tx.rollback();
636: }
637: pm.close();
638: }
639:
640: return dpDocuments;
641: }
642: }
|