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.CMCPrincipal;
016: import com.sun.portal.community.mc.CMCException;
017: import com.sun.portal.community.mc.ConfigTable.ConfigKey;
018: import com.sun.portal.community.mc.impl.CMCProperties;
019: import com.sun.portal.community.mc.CMCRolePrincipal;
020: import com.sun.portal.community.mc.impl.Debug;
021: import java.util.Properties;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Set;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.regex.Pattern;
030: import javax.jdo.Extent;
031: import javax.jdo.JDOHelper;
032: import javax.jdo.PersistenceManager;
033: import javax.jdo.PersistenceManagerFactory;
034: import javax.jdo.Query;
035: import com.sun.portal.community.mc.impl.jdo.pc.Community;
036:
037: /**
038: * This class contains utility methods for use in the
039: * com.sun.portal.community.impl.jdo community contributor implementation.
040: *
041: * This class is extended by the classes CommunityUserImpl and
042: * CommunityNodeImpl in said package.
043: */
044: public class CMCImpl {
045: private static PersistenceManagerFactory pmf;
046:
047: /**
048: * Utility class to map between integer and string role values
049: */
050: protected static class RoleMapper {
051: private static final int OWNER_ROLE_INT = 0;
052: private static final int MEMBER_ROLE_INT = 1;
053: private static final int VISITOR_ROLE_INT = 2;
054: private static final int INVITED_ROLE_INT = 3;
055: private static final int REJECTED_ROLE_INT = 4;
056: private static final int PENDING_ROLE_INT = 5;
057: private static final int BANNED_ROLE_INT = 6;
058: private static final int DISABLED_ROLE_INT = 7;
059: private static final int DELETED_ROLE_INT = 8;
060:
061: private static Map intMapping;
062: private static Map stringMapping;
063:
064: static {
065: intMapping = new HashMap();
066: intMapping.put(CMCRolePrincipal.VISITOR_ROLE, new Integer(
067: VISITOR_ROLE_INT));
068: intMapping.put(CMCRolePrincipal.MEMBER_ROLE, new Integer(
069: MEMBER_ROLE_INT));
070: intMapping.put(CMCRolePrincipal.OWNER_ROLE, new Integer(
071: OWNER_ROLE_INT));
072: intMapping.put(CMCRolePrincipal.INVITED_ROLE, new Integer(
073: INVITED_ROLE_INT));
074: intMapping.put(CMCRolePrincipal.REJECTED_ROLE, new Integer(
075: REJECTED_ROLE_INT));
076: intMapping.put(CMCRolePrincipal.PENDING_ROLE, new Integer(
077: PENDING_ROLE_INT));
078: intMapping.put(CMCRolePrincipal.BANNED_ROLE, new Integer(
079: BANNED_ROLE_INT));
080: intMapping.put(CMCRolePrincipal.DISABLED_ROLE, new Integer(
081: DISABLED_ROLE_INT));
082: intMapping.put(CMCRolePrincipal.DELETED_ROLE, new Integer(
083: DELETED_ROLE_INT));
084:
085: stringMapping = new HashMap();
086: stringMapping.put(new Integer(VISITOR_ROLE_INT),
087: CMCRolePrincipal.VISITOR_ROLE);
088: stringMapping.put(new Integer(MEMBER_ROLE_INT),
089: CMCRolePrincipal.MEMBER_ROLE);
090: stringMapping.put(new Integer(OWNER_ROLE_INT),
091: CMCRolePrincipal.OWNER_ROLE);
092: stringMapping.put(new Integer(INVITED_ROLE_INT),
093: CMCRolePrincipal.INVITED_ROLE);
094: stringMapping.put(new Integer(REJECTED_ROLE_INT),
095: CMCRolePrincipal.REJECTED_ROLE);
096: stringMapping.put(new Integer(PENDING_ROLE_INT),
097: CMCRolePrincipal.PENDING_ROLE);
098: stringMapping.put(new Integer(BANNED_ROLE_INT),
099: CMCRolePrincipal.BANNED_ROLE);
100: stringMapping.put(new Integer(DISABLED_ROLE_INT),
101: CMCRolePrincipal.DISABLED_ROLE);
102: stringMapping.put(new Integer(DELETED_ROLE_INT),
103: CMCRolePrincipal.DELETED_ROLE);
104: }
105:
106: public static int getIntValue(CMCRolePrincipal rp) {
107: Integer i = (Integer) intMapping.get(rp);
108: if (i == null) {
109: throw new RuntimeException("no mapping for rp=" + rp);
110: }
111: return i.intValue();
112: }
113:
114: public static int getIntValue(String roleName) {
115: CMCRolePrincipal rp = CMCRolePrincipal.valueOf(roleName);
116: if (rp == null) {
117: throw new RuntimeException(
118: "could not get role principal for roleName="
119: + roleName);
120: }
121: return getIntValue(rp);
122: }
123:
124: public static CMCRolePrincipal getCMCRolePrincipalValue(
125: int roleId) {
126: CMCRolePrincipal rp = (CMCRolePrincipal) stringMapping
127: .get(new Integer(roleId));
128: if (rp == null) {
129: throw new RuntimeException("no mapping for roleId="
130: + roleId);
131: }
132: return rp;
133: }
134: }
135:
136: /**
137: * Utility class for defining JDO query parameter.
138: */
139: protected static abstract class QueryParameter {
140: protected String name;
141: protected Object val;
142:
143: public QueryParameter(String name, Object val) {
144: if (name == null) {
145: throw new NullPointerException("null name not allowed");
146: }
147: this .name = name;
148: if (val == null) {
149: throw new NullPointerException("null value not allowed");
150: }
151: this .val = val;
152: }
153:
154: /**
155: * Get the JDO dquery declaraction string.
156: */
157: public abstract String toDeclareString();
158:
159: /**
160: * Get the JDO query filter string.
161: */
162: public abstract String toFilterString();
163:
164: /**
165: * What type is this query parameter?
166: */
167: protected static String getType() {
168: return "java.lang.String";
169: }
170:
171: public String getName() {
172: return name;
173: }
174:
175: public Object getValue() {
176: return val;
177: }
178: }
179:
180: /**
181: * Utility class for defining a JDO query parameter
182: * for community role equality.
183: */
184: protected static class RoleEqualsQueryParameter extends
185: QueryParameter {
186: public RoleEqualsQueryParameter(String name, Object val) {
187: super (name, val);
188: }
189:
190: public String toFilterString() {
191: return "roleId == " + getName();
192: }
193:
194: public String toDeclareString() {
195: return getType() + " " + getName();
196: }
197:
198: public static String getType() {
199: return "java.lang.Integer";
200: }
201:
202: }
203:
204: /**
205: * Utility class for defining a JDO query parameter
206: * for user name equality.
207: */
208: protected static class UserEqualsQueryParameter extends
209: QueryParameter {
210: public UserEqualsQueryParameter(String name, Object val) {
211: super (name, val);
212: }
213:
214: public String toFilterString() {
215: return "userName == " + getName();
216: }
217:
218: public String toDeclareString() {
219: return getType() + " " + getName();
220: }
221: }
222:
223: /**
224: * Utility class for defining a JDO query parameter
225: * for user pattern equality.
226: */
227: protected static class PatternEqualsQueryParameter extends
228: QueryParameter {
229: public PatternEqualsQueryParameter(String pattern, Object val) {
230: super (pattern, val);
231: }
232:
233: public String toFilterString() {
234: return "userPattern == " + getName();
235: }
236:
237: public String toDeclareString() {
238: return getType() + " " + getName();
239: }
240: }
241:
242: /**
243: * Utility class for defining a JDO query parameter
244: * for community name equality
245: */
246: protected static class CommunityEqualsQueryParameter extends
247: QueryParameter {
248: public CommunityEqualsQueryParameter(String name, Object val) {
249: super (name, val);
250: }
251:
252: public String toFilterString() {
253: return "community.communityName == " + getName();
254: }
255:
256: public String toDeclareString() {
257: return getType() + " " + getName();
258: }
259:
260: }
261:
262: /**
263: * Utility class for defining a list of JDO query parameters.
264: */
265: protected static interface QueryParameterList {
266: public String toFilterString(String other);
267:
268: public String toParameterString(String others);
269:
270: public String toImportsString(String others);
271:
272: public int size();
273:
274: public Map getParameterMap();
275:
276: public boolean allEquals();
277: }
278:
279: protected static abstract class QueryParameterListBase implements
280: QueryParameterList {
281: protected List params = new ArrayList();
282:
283: public String toFilterString(String other) {
284: StringBuffer b = new StringBuffer();
285: if (other != null) {
286: b.append(other);
287:
288: if (params.size() > 0) {
289: b.append(" && ");
290: }
291: }
292:
293: if (params.size() > 0) {
294: b.append(" ( ");
295: }
296:
297: for (int i = 0; i < params.size(); i++) {
298: if (i > 0) {
299: if (allEquals()) {
300: b.append(" && ");
301: } else {
302: b.append(" || ");
303: }
304: }
305: QueryParameter qp = (QueryParameter) params.get(i);
306: b.append(qp.toFilterString());
307: }
308:
309: if (params.size() > 0) {
310: b.append(" ) ");
311: }
312:
313: //Debug.log("UserEqualsQueryParameterList", "toFilterString", "b: " + b);
314: return b.toString();
315: }
316:
317: public abstract boolean allEquals();
318:
319: /**
320: * Get the JDO query parameters string.
321: */
322: public String toParameterString(String others) {
323: StringBuffer b = new StringBuffer();
324:
325: if (others != null) {
326: b.append(others);
327: if (params.size() > 0) {
328: b.append(",");
329: }
330: }
331:
332: for (int i = 0; i < params.size(); i++) {
333: QueryParameter qp = (QueryParameter) params.get(i);
334: b.append(qp.toDeclareString());
335: if (i < params.size() - 1) {
336: b.append(",");
337: }
338: }
339:
340: //Debug.log("QueryParameterList", "toDeclareString", "b: " + b);
341: return b.toString();
342: }
343:
344: /**
345: * Get the JDO query imports string.
346: */
347: public abstract String toImportsString(String others);
348:
349: public int size() {
350: return params.size();
351: }
352:
353: /**
354: * Get the JDO query parameter map
355: */
356: public Map getParameterMap() {
357: Map m = new HashMap();
358: for (int i = 0; i < params.size(); i++) {
359: QueryParameter qp = (QueryParameter) params.get(i);
360: m.put(qp.getName(), qp.getValue());
361: }
362:
363: return m;
364: }
365: }
366:
367: /**
368: * Utility class for defining a list of JDO query parameters that
369: * set up user name equality
370: */
371: protected static class UserEqualsQueryParameterList extends
372: QueryParameterListBase {
373: public UserEqualsQueryParameterList(Set userNames) {
374: if (userNames != null) {
375: int j = 0;
376: for (Iterator i = userNames.iterator(); i.hasNext();) {
377: String userName = (String) i.next();
378: QueryParameter qp = new UserEqualsQueryParameter(
379: "user_name" + j++, userName);
380: params.add(qp);
381: }
382: }
383: }
384:
385: public boolean allEquals() {
386: return false;
387: }
388:
389: public String toImportsString(String others) {
390: String is;
391:
392: if (others == null) {
393: is = "import " + RoleEqualsQueryParameter.getType();
394: } else {
395: if (others.indexOf(UserEqualsQueryParameter.getType()) != -1) {
396: is = "";
397: } else {
398: is = others + ";import "
399: + RoleEqualsQueryParameter.getType();
400: }
401: }
402:
403: //Debug.log("UserEqualsQueryParameterList", "toImportsString", "is: " + is);
404:
405: return is;
406: }
407: }
408:
409: /**
410: * Utility class for defining a list of JDO query parameters that
411: * set up user pattern equality
412: */
413: protected static class PatternEqualsQueryParameterList extends
414: QueryParameterListBase {
415: public PatternEqualsQueryParameterList(Set userPatterns) {
416: if (userPatterns != null) {
417: int j = 0;
418: for (Iterator i = userPatterns.iterator(); i.hasNext();) {
419: String userPattern = (String) i.next();
420: QueryParameter qp = new PatternEqualsQueryParameter(
421: "pattern" + j++, userPattern);
422: params.add(qp);
423: }
424: }
425: }
426:
427: public boolean allEquals() {
428: return false;
429: }
430:
431: public String toImportsString(String others) {
432: String is;
433:
434: if (others == null) {
435: is = "import " + RoleEqualsQueryParameter.getType();
436: } else {
437: if (others.indexOf(PatternEqualsQueryParameter
438: .getType()) != -1) {
439: is = "";
440: } else {
441: is = others + ";import "
442: + RoleEqualsQueryParameter.getType();
443: }
444: }
445:
446: //Debug.log("UserEqualsQueryParameterList", "toImportsString", "is: " + is);
447:
448: return is;
449: }
450: }
451:
452: /**
453: * Utility class for defining a list of JDO query parameters that
454: * set up role equality
455: */
456: protected static class RoleEqualsQueryParameterList extends
457: QueryParameterListBase {
458: public RoleEqualsQueryParameterList(Set rolePrincipals) {
459: if (rolePrincipals != null) {
460: int j = 0;
461: for (Iterator i = rolePrincipals.iterator(); i
462: .hasNext();) {
463: CMCRolePrincipal rp = (CMCRolePrincipal) i.next();
464: QueryParameter qp = new RoleEqualsQueryParameter(
465: "role_id" + j++, new Integer(RoleMapper
466: .getIntValue(rp.getName())));
467: params.add(qp);
468: }
469: }
470: }
471:
472: public boolean allEquals() {
473: return false;
474: }
475:
476: public String toImportsString(String others) {
477: if (others != null) {
478: if (others.indexOf(RoleEqualsQueryParameter.getType()) != -1) {
479: return "";
480: }
481: }
482:
483: String s = ((others == null) ? "" : others) + ";import "
484: + RoleEqualsQueryParameter.getType();
485: //Debug.log("RoleEqualsQueryParameterList", "toImportsString", "s: " + s);
486:
487: return s;
488: }
489: }
490:
491: /**
492: * Utility class for defining a list of JDO query parameters that
493: * set up role equality
494: */
495: protected static class ConfigKeyQueryParameterList extends
496: QueryParameterListBase {
497: public ConfigKeyQueryParameterList(ConfigKey ck, int n) {
498: if (ck != null) {
499: //Debug.log("ConfigKeyQueryParameterList", "ConfigKeyQueryParameterList", "ck: " + ck);
500:
501: CMCPrincipal cp = ck.getCommunityPrincipal();
502: QueryParameter cpqp = new CommunityEqualsQueryParameter(
503: "community_name" + n, cp.getName());
504: params.add(cpqp);
505:
506: CMCRolePrincipal rp = ck.getRolePrincipal();
507: QueryParameter rpqp = new RoleEqualsQueryParameter(
508: "role_id" + n, new Integer(RoleMapper
509: .getIntValue(rp.getName())));
510: params.add(rpqp);
511: }
512: }
513:
514: public boolean allEquals() {
515: return true;
516: }
517:
518: public String toFilterString(String other) {
519: StringBuffer b = new StringBuffer();
520: if (other != null) {
521: b.append(other);
522: b.append(" && ");
523: }
524:
525: b.append(" ( ");
526: QueryParameter qp = (QueryParameter) params.get(0);
527: b.append(qp.toFilterString());
528: if (allEquals()) {
529: b.append(" && ");
530: } else {
531: b.append(" || ");
532: }
533: qp = (QueryParameter) params.get(1);
534: b.append(qp.toFilterString());
535: b.append(" ) ");
536:
537: //Debug.log("ConfigKeyQueryParameterList", "toFilterString", "b: " + b);
538: return b.toString();
539: }
540:
541: public String toImportsString(String others) {
542: String s = (others == null) ? "" : others;
543:
544: if (s.indexOf(CommunityEqualsQueryParameter.getType()) == -1) {
545: if (s.length() > 0) {
546: s += ";";
547: }
548: s += "import "
549: + CommunityEqualsQueryParameter.getType();
550: }
551: if (s.indexOf(RoleEqualsQueryParameter.getType()) == -1) {
552: if (s.length() > 0) {
553: s += ";";
554: }
555: s += "import " + RoleEqualsQueryParameter.getType();
556: }
557:
558: //Debug.log("ConfigKeyQueryParameterList", "toImportsString", "s: " + s);
559: return s;
560: }
561: }
562:
563: protected static abstract class QueryParameterListList implements
564: QueryParameterList {
565: protected List params = new ArrayList();
566:
567: protected QueryParameterListList() {
568: // nothing, extending class must call populateParameters()
569: }
570:
571: public QueryParameterListList(Set qpls) {
572: populateParameters(qpls);
573:
574: }
575:
576: protected void populateParameters(Set qpls) {
577: if (qpls != null) {
578: int j = 0;
579: for (Iterator i = qpls.iterator(); i.hasNext();) {
580: QueryParameterList qpl = (QueryParameterList) i
581: .next();
582: params.add(qpl);
583: }
584: }
585: }
586:
587: public abstract boolean allEquals();
588:
589: public int size() {
590: return params.size();
591: }
592:
593: public String toFilterString(String other) {
594: StringBuffer b = new StringBuffer();
595: if (other != null) {
596: b.append(other);
597:
598: if (params.size() > 0) {
599: b.append(" && ");
600: }
601: }
602:
603: if (params.size() > 0) {
604: b.append(" ( ");
605: }
606:
607: for (int i = 0; i < params.size(); i++) {
608: if (i > 0) {
609: if (allEquals()) {
610: b.append(" && ");
611: } else {
612: b.append(" || ");
613: }
614: }
615: QueryParameterList qpl = (QueryParameterList) params
616: .get(i);
617: b.append(qpl.toFilterString(null));
618: }
619:
620: if (params.size() > 0) {
621: b.append(" ) ");
622: }
623:
624: //Debug.log("QueryParameterListList", "toFilterString", "b: " + b);
625: return b.toString();
626: }
627:
628: public String toImportsString(String others) {
629: String s = (others == null) ? "" : others;
630: for (int i = 0; i < params.size(); i++) {
631: QueryParameterList qpl = (QueryParameterList) params
632: .get(i);
633: String is = qpl.toImportsString(null);
634:
635: if (s.indexOf(is) == -1) {
636: s += ";" + is;
637: }
638: }
639:
640: //Debug.log("QueryParameterListList", "toImportsString", "s: " + s);
641: return s;
642: }
643:
644: public Map getParameterMap() {
645: Map m = new HashMap();
646: for (int i = 0; i < params.size(); i++) {
647: QueryParameterList qpl = (QueryParameterList) params
648: .get(i);
649: m.putAll(qpl.getParameterMap());
650: }
651:
652: //Debug.log("QueryParameterListList", "getParameterMap", "m: " + m);
653: return m;
654: }
655:
656: public String toParameterString(String others) {
657: StringBuffer b = new StringBuffer();
658:
659: if (others != null) {
660: b.append(others);
661: if (params.size() > 0) {
662: b.append(",");
663: }
664: }
665:
666: for (int i = 0; i < params.size(); i++) {
667: QueryParameterList qpl = (QueryParameterList) params
668: .get(i);
669: b.append(qpl.toParameterString(null));
670: if (i < params.size() - 1) {
671: b.append(",");
672: }
673: }
674:
675: //Debug.log("QueryParameterListList", "toParameterString", "b: " + b);
676: return b.toString();
677: }
678: }
679:
680: protected static class ConfigKeyOrQueryParameterList extends
681: QueryParameterListList {
682: public ConfigKeyOrQueryParameterList(Set configKeys) {
683: if (configKeys != null) {
684: Set qpls = new HashSet();
685: int j = 0;
686: for (Iterator i = configKeys.iterator(); i.hasNext();) {
687: ConfigKey ck = (ConfigKey) i.next();
688: QueryParameterList qpl = new ConfigKeyQueryParameterList(
689: ck, j++);
690: qpls.add(qpl);
691: }
692: populateParameters(qpls);
693: }
694: }
695:
696: public boolean allEquals() {
697: return false;
698: }
699: }
700:
701: static {
702: try {
703: //
704: // gets properties from class path as resource
705: // note that this is loaded once per JVM
706: //
707: pmf = JDOHelper.getPersistenceManagerFactory(CMCProperties
708: .getProperties());
709: } catch (Throwable t) {
710: throw new Error(t);
711: }
712: }
713:
714: public CMCImpl() {
715: // nothing
716: }
717:
718: protected static PersistenceManagerFactory getPersistenceManagerFactory() {
719: //
720: // one PMF per web container, so says the usage guide
721: //
722: return pmf;
723: }
724:
725: protected PersistenceManager getPersistenceManager()
726: throws CMCException {
727: //
728: // whay are we catching Throwable instead of the specific instance?
729: // good question. the called operation is not declared to throw any
730: // exception. however, if there is a problem initializing the
731: // database, an exception results from here. the odd things is
732: // that the exception is not a runtime exception. so how can it be
733: // thrown from the operation below. in summary, if i knew which
734: // instance to catch, i'd catch it.
735: //
736: try {
737: return getPersistenceManagerFactory()
738: .getPersistenceManager();
739: } catch (Throwable t) {
740: throw new CMCException(t);
741: }
742: }
743:
744: /**
745: * Get the PC (persistence capable) Community object from the store
746: */
747: protected Community getCommunity(PersistenceManager pm,
748: CMCPrincipal cp) {
749: //
750: // assumes that caller has setup transaction
751: //
752:
753: // true means validate beyond what is in the cache
754: // this is a potential performance optimization
755: // to set false
756: Extent e = pm.getExtent(Community.class, true);
757: Query q = pm.newQuery(e, "communityName == community_name");
758: q.declareParameters("java.lang.String community_name");
759: q.setUnique(true);
760: Community c = (Community) q.execute(cp.getName());
761:
762: //Debug.log("CommunityNodeImpl", "getCommunity", "c=" + c);
763:
764: return c;
765: }
766:
767: public static String getStaticType() {
768: return "jdo";
769: }
770:
771: public String getType() {
772: return getStaticType();
773: }
774:
775: protected static Set filterMembership(Set membership) {
776: Set filtered = new HashSet();
777: for (Iterator i = membership.iterator(); i.hasNext();) {
778: ConfigKey ck = (ConfigKey) i.next();
779: if (!ck.getCommunityPrincipal().getType().equals(
780: getStaticType())) {
781: continue;
782: }
783: filtered.add(ck);
784: }
785:
786: return filtered;
787: }
788:
789: protected static Set filterPrincipals(Set principals) {
790: Set filtered = new HashSet();
791: for (Iterator i = principals.iterator(); i.hasNext();) {
792: CMCPrincipal cmcp = (CMCPrincipal) i.next();
793: if (!cmcp.getType().equals(getStaticType())) {
794: continue;
795: }
796: filtered.add(cmcp);
797: }
798:
799: return filtered;
800: }
801:
802: protected static short shortValue(boolean x) {
803: if (x) {
804: return 1;
805: } else {
806: return 0;
807: }
808: }
809:
810: protected static boolean booleanValue(short x) {
811: if (x <= 0) {
812: return false;
813: } else {
814: return true;
815: }
816: }
817: }
|