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: */
013: package com.sun.portal.community.mc.impl.jdo;
014:
015: import com.sun.portal.community.mc.CMCException;
016: import com.sun.portal.community.mc.CMCExistsException;
017: import com.sun.portal.community.mc.CMCNode;
018: import com.sun.portal.community.mc.CMCPrincipal;
019: import com.sun.portal.community.mc.ConfigTable;
020: import com.sun.portal.community.mc.ConfigTable.ConfigKey;
021: import com.sun.portal.community.mc.impl.Debug;
022: import com.sun.portal.community.mc.CMCRolePrincipal;
023: import com.sun.portal.community.mc.impl.jdo.CMCImpl.QueryParameterList;
024: import com.sun.portal.community.mc.impl.jdo.CMCImpl.RoleEqualsQueryParameterList;
025: import com.sun.portal.community.mc.impl.jdo.CMCImpl.UserEqualsQueryParameterList;
026: import java.util.Collection;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.Properties;
031: import java.util.Set;
032: import java.util.HashMap;
033: import javax.jdo.Extent;
034: import javax.jdo.JDOException;
035: import javax.jdo.PersistenceManager;
036: import javax.jdo.Query;
037: import javax.jdo.Transaction;
038: import com.sun.portal.community.mc.impl.jdo.pc.Community;
039: import com.sun.portal.community.mc.impl.jdo.pc.CommunityUser;
040: import com.sun.portal.community.mc.impl.jdo.pc.CommunityRole;
041: import com.sun.portal.community.mc.impl.jdo.pc.CommunityDp;
042:
043: /**
044: * JDO community node contributor.
045: */
046: public class CMCNodeImpl extends CMCImpl implements CMCNode {
047: private CMCPrincipal communityPrincipal;
048:
049: public CMCNodeImpl() {
050: }
051:
052: public void init(Properties p, CMCPrincipal cp) throws CMCException {
053: this .communityPrincipal = cp;
054: }
055:
056: public CMCPrincipal getCMCPrincipal() {
057: return communityPrincipal;
058: }
059:
060: public Set getUsers() throws CMCException {
061: return getUsers(null);
062: }
063:
064: public Set getUsers(Set rolePrincipals) throws CMCException {
065: PersistenceManager pm = getPersistenceManager();
066: Transaction tx = pm.currentTransaction();
067:
068: Set users = new HashSet();
069: QueryParameterList qpl = new RoleEqualsQueryParameterList(
070: rolePrincipals);
071:
072: String filter = qpl
073: .toFilterString("community.communityName == community_name");
074: String imports = qpl.toImportsString("import java.lang.String");
075: String params = qpl
076: .toParameterString("java.lang.String community_name");
077:
078: try {
079: tx.begin();
080:
081: Extent e = pm
082: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityUser.class);
083: Query q = pm.newQuery(e);
084: q.setFilter(filter);
085: q.declareImports(imports);
086: q.declareParameters(params);
087: q.setResult("distinct this.userName");
088: Map parameterMap = qpl.getParameterMap();
089: parameterMap.put("community_name", getCMCPrincipal()
090: .getName());
091:
092: //System.out.println("filter=" + filter);
093: //System.out.println("imports= " + imports);
094: //System.out.println("params=" + params);
095: //System.out.println("parameterMap=" + parameterMap);
096:
097: Collection results = (Collection) q
098: .executeWithMap(parameterMap);
099: users.addAll(results);
100:
101: tx.commit();
102: } catch (JDOException jdoe) {
103: throw new CMCException(jdoe);
104: } finally {
105: if (tx.isActive()) {
106: tx.rollback();
107: }
108: pm.close();
109: }
110:
111: return users;
112: }
113:
114: public Set getRoles() throws CMCException {
115: PersistenceManager pm = getPersistenceManager();
116: Transaction tx = pm.currentTransaction();
117:
118: Set roles = new HashSet();
119:
120: String filter = "community.communityName == community_name";
121: String imports = "import java.lang.String";
122: String params = "String community_name";
123:
124: try {
125: tx.begin();
126:
127: Extent e = pm
128: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityRole.class);
129: Query q = pm.newQuery(e);
130: q.setFilter(filter);
131: q.declareImports(imports);
132: q.declareParameters(params);
133: Map parameterMap = new HashMap();
134: parameterMap.put("community_name", getCMCPrincipal()
135: .getName());
136:
137: //System.out.println("filter=" + filter);
138: //System.out.println("imports= " + imports);
139: //System.out.println("params=" + params);
140: //System.out.println("parameterMap=" + parameterMap);
141:
142: Collection results = (Collection) q
143: .executeWithMap(parameterMap);
144: for (Iterator i = results.iterator(); i.hasNext();) {
145: CommunityRole r = (CommunityRole) i.next();
146: CMCRolePrincipal rp = RoleMapper
147: .getCMCRolePrincipalValue(r.getRoleId());
148: roles.add(rp);
149: }
150:
151: tx.commit();
152: } catch (JDOException jdoe) {
153: throw new CMCException(jdoe);
154: } finally {
155: if (tx.isActive()) {
156: tx.rollback();
157: }
158: pm.close();
159: }
160:
161: return roles;
162: }
163:
164: public boolean supportsGetRoles() {
165: return true;
166: }
167:
168: public void addUsers(Set newUsers, CMCRolePrincipal rolePrincipal)
169: throws CMCException {
170: //Debug.log("CommunityNodeImpl", "addUsers", "cp: " + getCommunityPrincipal() + ", rolePrincipal: " + rolePrincipal + ", newUsers=" + newUsers);
171:
172: PersistenceManager pm = getPersistenceManager();
173: Transaction tx = pm.currentTransaction();
174:
175: try {
176: tx.begin();
177:
178: Community c = getCommunity(pm);
179: if (c == null) {
180: throw new CMCException("does not exist: "
181: + getCMCPrincipal());
182: }
183:
184: String roleName = rolePrincipal.getName();
185: int roleId = RoleMapper.getIntValue(rolePrincipal);
186: for (Iterator i = newUsers.iterator(); i.hasNext();) {
187: String userName = (String) i.next();
188: CommunityUser cu = new CommunityUser(getCMCPrincipal()
189: .getName(), roleId, userName, System
190: .currentTimeMillis());
191: c.addCommunityUser(cu);
192: }
193:
194: tx.commit();
195: } catch (JDOException jdoe) {
196: throw new CMCException(jdoe);
197: } finally {
198: if (tx.isActive()) {
199: tx.rollback();
200: }
201: pm.close();
202: }
203: }
204:
205: public void addRole(CMCRolePrincipal rolePrincipal)
206: throws CMCException {
207: PersistenceManager pm = getPersistenceManager();
208: Transaction tx = pm.currentTransaction();
209:
210: try {
211: tx.begin();
212:
213: Community c = getCommunity(pm);
214: if (c == null) {
215: throw new CMCException("does not exist: "
216: + getCMCPrincipal());
217: }
218:
219: int roleId = RoleMapper.getIntValue(rolePrincipal);
220: CommunityRole cr = new CommunityRole(getCMCPrincipal()
221: .getName(), roleId, System.currentTimeMillis());
222: c.addCommunityRole(cr);
223:
224: tx.commit();
225: } catch (JDOException jdoe) {
226: throw new CMCException(jdoe);
227: } finally {
228: if (tx.isActive()) {
229: tx.rollback();
230: }
231: pm.close();
232: }
233: }
234:
235: public boolean supportsAddUsers() {
236: return true;
237: }
238:
239: public boolean supportsAddRole() {
240: return true;
241: }
242:
243: public void removeUsers(Set oldUsers, Set rolePrincipals)
244: throws CMCException {
245: PersistenceManager pm = getPersistenceManager();
246: Transaction tx = pm.currentTransaction();
247:
248: QueryParameterList roleQpl = new RoleEqualsQueryParameterList(
249: rolePrincipals);
250: QueryParameterList userQpl = new UserEqualsQueryParameterList(
251: oldUsers);
252:
253: try {
254: tx.begin();
255:
256: String filter = roleQpl
257: .toFilterString(userQpl
258: .toFilterString("community.communityName == community_name"));
259: String imports = roleQpl.toImportsString(userQpl
260: .toImportsString("import java.lang.String"));
261: String params = roleQpl
262: .toParameterString(userQpl
263: .toParameterString("java.lang.String community_name"));
264:
265: Extent e = pm
266: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityUser.class);
267: Query q = pm.newQuery(e);
268: q.setFilter(filter);
269: q.declareImports(imports);
270: q.declareParameters(params);
271: // TODO: do we need this? distinct is a perf issue
272: q.setResult("distinct this");
273: Map parameterMap = roleQpl.getParameterMap();
274: parameterMap.putAll(userQpl.getParameterMap());
275: parameterMap.put("community_name", getCMCPrincipal()
276: .getName());
277:
278: Collection results = (Collection) q
279: .executeWithMap(parameterMap);
280:
281: //
282: // explicit removal of persistent objects required
283: // TODO: when supported, use Query.deletePersistentAll()
284: //
285: pm.deletePersistentAll(results);
286:
287: tx.commit();
288: } catch (JDOException jdoe) {
289: throw new CMCException(jdoe);
290: } finally {
291: if (tx.isActive()) {
292: tx.rollback();
293: }
294: pm.close();
295: }
296: }
297:
298: public void removeRole(CMCRolePrincipal rolePrincipal)
299: throws CMCException {
300: PersistenceManager pm = getPersistenceManager();
301: Transaction tx = pm.currentTransaction();
302:
303: try {
304: tx.begin();
305:
306: Extent e = pm
307: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityRole.class);
308:
309: String filter = "community.communityName == community_name && roleId == role_id";
310: String imports = "import java.lang.String; import java.lang.Integer";
311: String params = "String community_name, Integer role_id";
312:
313: Query q = pm.newQuery(e);
314: q.setFilter(filter);
315: q.declareImports(imports);
316: q.declareParameters(params);
317:
318: Map parameterMap = new HashMap();
319: parameterMap.put("community_name", getCMCPrincipal()
320: .getName());
321: parameterMap.put("role_id", new Integer(RoleMapper
322: .getIntValue(rolePrincipal)));
323:
324: Collection results = (Collection) q
325: .executeWithMap(parameterMap);
326:
327: //
328: // explicit removal of persistent objects required
329: // TODO: when supported, use Query.deletePersistentAll()
330: //
331: pm.deletePersistentAll(results);
332:
333: tx.commit();
334: } catch (JDOException jdoe) {
335: throw new CMCException(jdoe);
336: } finally {
337: if (tx.isActive()) {
338: tx.rollback();
339: }
340: pm.close();
341: }
342: }
343:
344: public boolean supportsRemoveUsers() {
345: return true;
346: }
347:
348: public boolean supportsRemoveRole() {
349: return true;
350: }
351:
352: public boolean supportsRole(CMCRolePrincipal rolePrincipal) {
353: return true;
354: }
355:
356: public void create() throws CMCExistsException, CMCException {
357: PersistenceManager pm = getPersistenceManager();
358: Transaction tx = pm.currentTransaction();
359:
360: try {
361: tx.begin();
362:
363: Community c = getCommunity(pm);
364: if (c != null) {
365: throw new CMCExistsException("exists: "
366: + getCMCPrincipal());
367: }
368:
369: c = new Community(getCMCPrincipal().getName());
370: c.setCommunityCreationTime(System.currentTimeMillis());
371:
372: pm.makePersistent(c);
373:
374: tx.commit();
375: } catch (JDOException jdoe) {
376: throw new CMCException(jdoe);
377: } finally {
378: if (tx.isActive()) {
379: tx.rollback();
380: }
381: pm.close();
382: }
383: }
384:
385: public boolean supportsCreate() {
386: return true;
387: }
388:
389: public void remove() throws CMCException {
390: PersistenceManager pm = getPersistenceManager();
391: Transaction tx = pm.currentTransaction();
392:
393: try {
394: tx.begin();
395:
396: Community c = getCommunity(pm);
397: if (c == null) {
398: throw new CMCException("does not exist: "
399: + getCMCPrincipal());
400: }
401:
402: pm.deletePersistent(c);
403:
404: tx.commit();
405: } catch (JDOException jdoe) {
406: throw new CMCException(jdoe);
407: } finally {
408: if (tx.isActive()) {
409: tx.rollback();
410: }
411: pm.close();
412: }
413: }
414:
415: public boolean supportsRemove() {
416: return true;
417: }
418:
419: public boolean exists() throws CMCException {
420: PersistenceManager pm = getPersistenceManager();
421: Transaction tx = pm.currentTransaction();
422:
423: boolean exists = false;
424:
425: try {
426: tx.begin();
427:
428: Community c = getCommunity(pm);
429:
430: if (c != null) {
431: exists = true;
432: }
433:
434: tx.commit();
435: } catch (JDOException jdoe) {
436: throw new CMCException(jdoe);
437: } finally {
438: if (tx.isActive()) {
439: tx.rollback();
440: }
441: pm.close();
442: }
443:
444: return exists;
445: }
446:
447: public ConfigTable getDPDocuments() throws CMCException {
448: return getDPDocuments(null);
449: }
450:
451: public ConfigTable getDPDocuments(Set rolePrincipals)
452: throws CMCException {
453: PersistenceManager pm = getPersistenceManager();
454: Transaction tx = pm.currentTransaction();
455:
456: ConfigTable dpDocuments = new ConfigTable();
457: QueryParameterList qpl = new RoleEqualsQueryParameterList(
458: rolePrincipals);
459:
460: try {
461: tx.begin();
462:
463: Extent e = pm
464: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityDp.class);
465: Query q = pm.newQuery(e);
466: q
467: .setFilter(qpl
468: .toFilterString("community.communityName == community_name"));
469: q.declareImports(qpl
470: .toImportsString("import java.lang.String"));
471: q
472: .declareParameters(qpl
473: .toParameterString("java.lang.String community_name"));
474: q.setResult("distinct this");
475: Map parameterMap = qpl.getParameterMap();
476: parameterMap.put("community_name", getCMCPrincipal()
477: .getName());
478: Collection results = (Collection) q
479: .executeWithMap(parameterMap);
480:
481: //
482: // convert JDO CommunityDp objects into external form
483: //
484: for (Iterator i = results.iterator(); i.hasNext();) {
485: CommunityDp cdp = (CommunityDp) i.next();
486: CMCRolePrincipal rp = RoleMapper
487: .getCMCRolePrincipalValue(cdp.getRoleId());
488: ConfigTable.ConfigKey ck = new ConfigTable.ConfigKey(
489: getCMCPrincipal(), rp);
490: dpDocuments.put(ck, cdp.getDp());
491: }
492:
493: tx.commit();
494: } catch (JDOException jdoe) {
495: throw new CMCException(jdoe);
496: } finally {
497: if (tx.isActive()) {
498: tx.rollback();
499: }
500: pm.close();
501: }
502:
503: return dpDocuments;
504: }
505:
506: public ConfigTable getDPDocumentsLastModified() throws CMCException {
507: return getDPDocumentsLastModified(null);
508: }
509:
510: public ConfigTable getDPDocumentsLastModified(Set rolePrincipals)
511: throws CMCException {
512: PersistenceManager pm = getPersistenceManager();
513: Transaction tx = pm.currentTransaction();
514:
515: ConfigTable dpDocuments = new ConfigTable();
516: QueryParameterList qpl = new RoleEqualsQueryParameterList(
517: rolePrincipals);
518:
519: try {
520: tx.begin();
521:
522: Extent e = pm
523: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.CommunityDp.class);
524: Query q = pm.newQuery(e);
525: q
526: .setFilter(qpl
527: .toFilterString("community.communityName == community_name"));
528: q.declareImports(qpl
529: .toImportsString("import java.lang.String"));
530: q
531: .declareParameters(qpl
532: .toParameterString("java.lang.String community_name"));
533: q.setResult("distinct this");
534: Map parameterMap = qpl.getParameterMap();
535: parameterMap.put("community_name", getCMCPrincipal()
536: .getName());
537: Collection results = (Collection) q
538: .executeWithMap(parameterMap);
539:
540: for (Iterator i = results.iterator(); i.hasNext();) {
541: CommunityDp cdp = (CommunityDp) i.next();
542: CMCRolePrincipal rp = RoleMapper
543: .getCMCRolePrincipalValue(cdp.getRoleId());
544: ConfigTable.ConfigKey ck = new ConfigTable.ConfigKey(
545: getCMCPrincipal(), rp);
546: dpDocuments.put(ck, new Long(cdp.getLastModified()));
547: }
548:
549: tx.commit();
550: } catch (JDOException jdoe) {
551: throw new CMCException(jdoe);
552: } finally {
553: if (tx.isActive()) {
554: tx.rollback();
555: }
556: pm.close();
557: }
558:
559: return dpDocuments;
560: }
561:
562: public void setDPDocuments(ConfigTable dpDocuments)
563: throws CMCException {
564: PersistenceManager pm = getPersistenceManager();
565: Transaction tx = pm.currentTransaction();
566:
567: try {
568: tx.begin();
569:
570: Community c = getCommunity(pm);
571: if (c == null) {
572: throw new CMCException("does not exist: "
573: + getCMCPrincipal());
574: }
575:
576: for (Iterator i = dpDocuments.getConfigKeys().iterator(); i
577: .hasNext();) {
578: ConfigKey ck = (ConfigKey) i.next();
579: int roleId = RoleMapper.getIntValue(ck
580: .getRolePrincipal());
581: CommunityDp cdp = new CommunityDp(ck
582: .getCommunityPrincipal().getName(), roleId,
583: (byte[]) dpDocuments.get(ck));
584: c.setCommunityDp(cdp);
585: }
586:
587: tx.commit();
588: } catch (JDOException jdoe) {
589: throw new CMCException(jdoe);
590: } finally {
591: if (tx.isActive()) {
592: tx.rollback();
593: }
594: pm.close();
595: }
596: }
597:
598: public boolean supportsSetDPDocuments() {
599: return true;
600: }
601:
602: //
603: // Description related Methods
604: //
605:
606: public String getDescription() throws CMCException {
607: return getCurrentCommunity().getDescription();
608: }
609:
610: public void setDescription(String description) throws CMCException {
611: PersistenceManager pm = getPersistenceManager();
612: Transaction tx = pm.currentTransaction();
613:
614: try {
615: tx.begin();
616: Community currentCommunity = getCommunity(pm,
617: getCMCPrincipal());
618: currentCommunity.setDescription(description);
619: tx.commit();
620: } catch (JDOException jdoe) {
621: throw new CMCException(jdoe);
622: } finally {
623: if (tx.isActive()) {
624: tx.rollback();
625: }
626: pm.close();
627: }
628: }
629:
630: public boolean supportsDescription() throws CMCException {
631: return true;
632: }
633:
634: //
635: // Category related Methods
636: //
637:
638: public String getCategory() throws CMCException {
639: return getCurrentCommunity().getCategory();
640: }
641:
642: public void setCategory(String category) throws CMCException {
643: PersistenceManager pm = getPersistenceManager();
644: Transaction tx = pm.currentTransaction();
645:
646: try {
647: tx.begin();
648: Community currentCommunity = getCommunity(pm,
649: getCMCPrincipal());
650: currentCommunity.setCategory(category);
651: tx.commit();
652: } catch (JDOException jdoe) {
653: throw new CMCException(jdoe);
654: } finally {
655: if (tx.isActive()) {
656: tx.rollback();
657: }
658: pm.close();
659: }
660: }
661:
662: public boolean supportsCategory() throws CMCException {
663: return true;
664: }
665:
666: //
667: // Community creation time related Methods
668: //
669: public long getCommunityCreationTime() throws CMCException {
670: return getCurrentCommunity().getCommunityCreationTime();
671: }
672:
673: public boolean supportsCommunityCreationTime() throws CMCException {
674: return true;
675: }
676:
677: //
678: // Membership restriction related methods
679: //
680: public boolean isMembershipRestricted() throws CMCException {
681: return booleanValue(getCurrentCommunity()
682: .isMembershipRestricted());
683: }
684:
685: public void setMembershipRestricted(boolean isMembershipRestricted)
686: throws CMCException {
687: PersistenceManager pm = getPersistenceManager();
688: Transaction tx = pm.currentTransaction();
689:
690: try {
691: tx.begin();
692: Community currentCommunity = getCommunity(pm,
693: getCMCPrincipal());
694: currentCommunity
695: .setMembershipRestricted(shortValue(isMembershipRestricted));
696: tx.commit();
697: } catch (JDOException jdoe) {
698: throw new CMCException(jdoe);
699: } finally {
700: if (tx.isActive()) {
701: tx.rollback();
702: }
703: pm.close();
704: }
705: }
706:
707: public boolean supportsMembershipRestriction() throws CMCException {
708: return true;
709: }
710:
711: //
712: // Security related methods
713: //
714:
715: public boolean isSecure() throws CMCException {
716: return booleanValue(getCurrentCommunity().isSecure());
717: }
718:
719: public void setSecure(boolean isSecure) throws CMCException {
720: PersistenceManager pm = getPersistenceManager();
721: Transaction tx = pm.currentTransaction();
722:
723: try {
724: tx.begin();
725: Community currentCommunity = getCommunity(pm,
726: getCMCPrincipal());
727: currentCommunity.setSecure(shortValue(isSecure));
728: tx.commit();
729: } catch (JDOException jdoe) {
730: throw new CMCException(jdoe);
731: } finally {
732: if (tx.isActive()) {
733: tx.rollback();
734: }
735: pm.close();
736: }
737: }
738:
739: public boolean supportsSecuring() throws CMCException {
740: return true;
741: }
742:
743: //
744: // Listing related methods
745: //
746: public boolean isListed() throws CMCException {
747: return booleanValue(getCurrentCommunity().isListed());
748: }
749:
750: public void setListed(boolean isListed) throws CMCException {
751: PersistenceManager pm = getPersistenceManager();
752: Transaction tx = pm.currentTransaction();
753:
754: try {
755: tx.begin();
756: Community currentCommunity = getCommunity(pm,
757: getCMCPrincipal());
758: currentCommunity.setListed(shortValue(isListed));
759: tx.commit();
760: } catch (JDOException jdoe) {
761: throw new CMCException(jdoe);
762: } finally {
763: if (tx.isActive()) {
764: tx.rollback();
765: }
766: pm.close();
767: }
768: }
769:
770: public boolean supportsListing() throws CMCException {
771: return true;
772: }
773:
774: public boolean supportsRoleCreationTime() throws CMCException {
775: return true;
776: }
777:
778: private Community getCurrentCommunity() throws CMCException {
779:
780: PersistenceManager pm = getPersistenceManager();
781: Transaction tx = pm.currentTransaction();
782: try {
783: tx.begin();
784:
785: Community currentCommunity = getCommunity(pm);
786: if (currentCommunity == null) {
787: throw new CMCException("does not exist: "
788: + getCMCPrincipal());
789: }
790: tx.commit();
791: return currentCommunity;
792: } catch (JDOException jdoe) {
793: throw new CMCException(jdoe);
794: } finally {
795: if (tx.isActive()) {
796: tx.rollback();
797: }
798: pm.close();
799: }
800: }
801:
802: private Community getCommunity(PersistenceManager pm)
803: throws CMCException {
804: return getCommunity(pm, getCMCPrincipal());
805: }
806: }
|