001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.getc
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036: package com.sun.portal.community.mc.impl.jdo;
037:
038: import com.sun.portal.community.mc.CMCNodeManager;
039: import com.sun.portal.community.mc.CMCException;
040: import com.sun.portal.community.mc.CMCPrincipal;
041: import com.sun.portal.community.mc.CMCRolePrincipal;
042: import com.sun.portal.community.mc.ConfigTable.ConfigKey;
043: import com.sun.portal.community.mc.impl.jdo.CMCImpl.RoleMapper;
044: import com.sun.portal.community.mc.impl.jdo.pc.Community;
045: import javax.jdo.Extent;
046: import javax.jdo.JDOException;
047: import javax.jdo.PersistenceManager;
048: import javax.jdo.Query;
049: import javax.jdo.Transaction;
050: import java.util.ArrayList;
051: import java.util.Collection;
052: import java.util.Collections;
053: import java.util.HashMap;
054: import java.util.HashSet;
055: import java.util.Iterator;
056: import java.util.List;
057: import java.util.Map;
058:
059: import java.util.Properties;
060: import java.util.Set;
061: import java.util.regex.Pattern;
062:
063: public class CMCNodeManagerImpl extends CMCImpl implements
064: CMCNodeManager, SchemaNames {
065: private static abstract class QueryElement {
066: protected String queryString;
067: protected Object[] parameters;
068:
069: public String getQueryString() {
070: return queryString;
071: }
072:
073: public Object[] getParameters() {
074: return parameters;
075: }
076: }
077:
078: private static class RoleUserCountQueryElement extends QueryElement {
079: public RoleUserCountQueryElement(Set communityPrincipals,
080: Set rolePrincipals) {
081: if (communityPrincipals == null) {
082: throw new NullPointerException(
083: "null community principal set is not allowed");
084: }
085: if (rolePrincipals == null) {
086: throw new NullPointerException(
087: "null role principal set is not allowed");
088: }
089: if (communityPrincipals.size() == 0) {
090: throw new IllegalArgumentException(
091: "empty community principal set is not allowed");
092: }
093:
094: buildQuery(communityPrincipals, rolePrincipals);
095: }
096:
097: private void buildQuery(Set communityPrincipals,
098: Set rolePrincipals) {
099: StringBuffer q = new StringBuffer();
100: List parameterList = new ArrayList();
101:
102: q.append("select " + COMMUNITY_USER_TABLE + "."
103: + COMMUNITY_NAME_COLUMN + " as communityname, "
104: + COMMUNITY_USER_TABLE + "." + ROLE_ID_COLUMN
105: + " as roleid, count(distinct "
106: + COMMUNITY_USER_TABLE + "." + USER_NAME_COLUMN
107: + ") as usercount from " + COMMUNITY_USER_TABLE
108: + " where " + COMMUNITY_USER_TABLE + "."
109: + COMMUNITY_NAME_COLUMN + " in");
110: q.append(" ( ");
111: for (Iterator i = communityPrincipals.iterator(); i
112: .hasNext();) {
113: CMCPrincipal cmcp = (CMCPrincipal) i.next();
114: q.append("?");
115: parameterList.add(cmcp.getName());
116: if (i.hasNext()) {
117: q.append(", ");
118: }
119: }
120: q.append(" ) ");
121:
122: q.append(" and ");
123: q.append(COMMUNITY_USER_TABLE);
124: q.append(".");
125: q.append(ROLE_ID_COLUMN);
126: q.append(" in ( ");
127: for (Iterator i = rolePrincipals.iterator(); i.hasNext();) {
128: CMCRolePrincipal cmcrp = (CMCRolePrincipal) i.next();
129: q.append("?");
130: parameterList.add(new Integer(RoleMapper
131: .getIntValue(cmcrp.getName())));
132: if (i.hasNext()) {
133: q.append(", ");
134: }
135: }
136: q.append(" )");
137: q.append(" group by ");
138: q.append(COMMUNITY_USER_TABLE);
139: q.append(".");
140: q.append(COMMUNITY_NAME_COLUMN);
141: q.append(", ");
142: q.append(COMMUNITY_USER_TABLE);
143: q.append(".");
144: q.append(ROLE_ID_COLUMN);
145:
146: queryString = q.toString();
147: //System.out.println("queryString=" + queryString);
148: //System.out.println("parameterList=" + parameterList);
149: parameters = (Object[]) parameterList
150: .toArray(new Object[0]);
151: }
152: }
153:
154: private static class UserCountQueryElement extends QueryElement {
155: public UserCountQueryElement(Set communityPrincipals) {
156: if (communityPrincipals == null) {
157: throw new NullPointerException(
158: "null community principal set is not allowed");
159: }
160: if (communityPrincipals.size() == 0) {
161: throw new IllegalArgumentException(
162: "empty community principal set is not allowed");
163: }
164:
165: buildQuery(communityPrincipals);
166: }
167:
168: private void buildQuery(Set communityPrincipals) {
169: StringBuffer q = new StringBuffer();
170: List parameterList = new ArrayList();
171:
172: q.append("select " + COMMUNITY_USER_TABLE + "."
173: + COMMUNITY_NAME_COLUMN
174: + " as communityname, count(distinct "
175: + COMMUNITY_USER_TABLE + "." + USER_NAME_COLUMN
176: + ") as usercount from " + COMMUNITY_USER_TABLE
177: + " where " + COMMUNITY_USER_TABLE + "."
178: + COMMUNITY_NAME_COLUMN + " in");
179: q.append(" ( ");
180: for (Iterator i = communityPrincipals.iterator(); i
181: .hasNext();) {
182: CMCPrincipal cmcp = (CMCPrincipal) i.next();
183: q.append("?");
184: parameterList.add(cmcp.getName());
185: if (i.hasNext()) {
186: q.append(", ");
187: }
188: }
189: q.append(" ) group by ");
190: q.append(COMMUNITY_USER_TABLE);
191: q.append(".");
192: q.append(COMMUNITY_NAME_COLUMN);
193:
194: queryString = q.toString();
195: //System.out.println("queryString=" + queryString);
196: //System.out.println("parameterList=" + parameterList);
197: parameters = (String[]) parameterList
198: .toArray(new String[0]);
199: }
200: }
201:
202: public static class RoleUserCount {
203: private int roleid;
204: private String communityname;
205: private int usercount;
206:
207: public int getRoleid() {
208: return roleid;
209: }
210:
211: public void setRoleid(int role) {
212: roleid = role;
213: }
214:
215: public String getCommunityname() {
216: return communityname;
217: }
218:
219: public void setCommunityname(String name) {
220: communityname = name;
221: }
222:
223: public int getUsercount() {
224: return usercount;
225: }
226:
227: public void setUsercount(int count) {
228: usercount = count;
229: }
230:
231: public String toString() {
232: return "{ communityname=" + getCommunityname()
233: + ", roleid=" + getRoleid() + ", usercount="
234: + getUsercount();
235: }
236: }
237:
238: public static class UserCount {
239: private String communityname;
240: private int usercount;
241:
242: public String getCommunityname() {
243: return communityname;
244: }
245:
246: public int getUsercount() {
247: return usercount;
248: }
249:
250: public void setCommunityname(String name) {
251: communityname = name;
252: }
253:
254: public void setUsercount(int count) {
255: usercount = count;
256: }
257:
258: public String toString() {
259: return "{ communityname=" + getCommunityname()
260: + ", usercount=" + getUsercount();
261: }
262: }
263:
264: public void init(Properties p) throws CMCException {
265: // nothing
266: }
267:
268: public Map getUserCounts(Set communityPrincipals)
269: throws CMCException {
270: communityPrincipals = filterPrincipals(communityPrincipals);
271: Map counts = new HashMap();
272:
273: QueryElement qe = new UserCountQueryElement(communityPrincipals);
274:
275: PersistenceManager pm = getPersistenceManager();
276: Transaction tx = pm.currentTransaction();
277:
278: try {
279: tx.begin();
280:
281: Query query = pm.newQuery("javax.jdo.query.SQL", qe
282: .getQueryString());
283: query.setResultClass(UserCount.class);
284: Collection results = (Collection) query.executeWithArray(qe
285: .getParameters());
286:
287: for (Iterator i = results.iterator(); i.hasNext();) {
288: UserCount uc = (UserCount) i.next();
289: CMCPrincipal cmcp = new CMCPrincipal(getType(), uc
290: .getCommunityname());
291: Integer count = new Integer(uc.getUsercount());
292: counts.put(cmcp, count);
293: }
294:
295: tx.commit();
296: } catch (JDOException jdoe) {
297: throw new CMCException(jdoe);
298: } finally {
299: if (tx.isActive()) {
300: tx.rollback();
301: }
302: pm.close();
303: }
304:
305: return counts;
306: }
307:
308: public Map getRoleUserCounts(Set communityPrincipals,
309: Set rolePrincipals) throws CMCException {
310: communityPrincipals = filterPrincipals(communityPrincipals);
311: Map counts = new HashMap();
312:
313: QueryElement qe = new RoleUserCountQueryElement(
314: communityPrincipals, rolePrincipals);
315:
316: PersistenceManager pm = getPersistenceManager();
317: Transaction tx = pm.currentTransaction();
318:
319: try {
320: tx.begin();
321:
322: Query query = pm.newQuery("javax.jdo.query.SQL", qe
323: .getQueryString());
324: query.setResultClass(RoleUserCount.class);
325: Collection results = (Collection) query.executeWithArray(qe
326: .getParameters());
327:
328: for (Iterator i = results.iterator(); i.hasNext();) {
329: RoleUserCount uc = (RoleUserCount) i.next();
330: //System.out.println("uc=" + uc);
331: CMCPrincipal cmcp = new CMCPrincipal(getType(), uc
332: .getCommunityname());
333: CMCRolePrincipal cmcrp = RoleMapper
334: .getCMCRolePrincipalValue(uc.getRoleid());
335: ConfigKey ck = new ConfigKey(cmcp, cmcrp);
336: Integer count = new Integer(uc.getUsercount());
337: counts.put(ck, count);
338: }
339:
340: tx.commit();
341: } catch (JDOException jdoe) {
342: throw new CMCException(jdoe);
343: } finally {
344: if (tx.isActive()) {
345: tx.rollback();
346: }
347: pm.close();
348: }
349:
350: return counts;
351: }
352:
353: public Set match(Pattern p, boolean nameOnly) throws CMCException {
354: PersistenceManager pm = getPersistenceManager();
355: Transaction tx = pm.currentTransaction();
356:
357: Set principals = new HashSet();
358:
359: try {
360: tx.begin();
361:
362: Extent e = pm
363: .getExtent(com.sun.portal.community.mc.impl.jdo.pc.Community.class);
364: Query q = pm.newQuery(e);
365: StringBuffer filterBuf = new StringBuffer(
366: "communityName.matches(pattern)");
367: if (!nameOnly) {
368: filterBuf.append(" || description.matches(pattern)");
369: }
370: q.setFilter(filterBuf.toString());
371: q.declareImports("import java.lang.String");
372: q.declareParameters("java.lang.String pattern");
373: Map parameterMap = Collections.singletonMap("pattern", p
374: .pattern());
375:
376: Collection results = (Collection) q
377: .executeWithMap(parameterMap);
378:
379: for (Iterator i = results.iterator(); i.hasNext();) {
380: Community c = (Community) i.next();
381: CMCPrincipal principal = new CMCPrincipal(getType(), c
382: .getCommunityName());
383: principals.add(principal);
384: }
385:
386: tx.commit();
387: } catch (JDOException jdoe) {
388: throw new CMCException(jdoe);
389: } finally {
390: if (tx.isActive()) {
391: tx.rollback();
392: }
393: pm.close();
394: }
395:
396: return principals;
397: }
398:
399: }
|