001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.impl.portlet.state;
023:
024: import org.hibernate.Query;
025: import org.hibernate.Session;
026: import org.hibernate.SessionFactory;
027: import org.jboss.portal.common.value.Value;
028: import org.jboss.portal.jems.as.system.AbstractJBossService;
029: import org.jboss.portal.jems.hibernate.ObjectContextualizer;
030: import org.jboss.portal.portlet.state.InvalidStateIdException;
031: import org.jboss.portal.portlet.state.NoSuchStateException;
032: import org.jboss.portal.portlet.state.PropertyMap;
033: import org.jboss.portal.portlet.state.SimplePropertyMap;
034: import org.jboss.portal.portlet.state.producer.PortletStateContext;
035: import org.jboss.portal.portlet.state.producer.PortletStatePersistenceManager;
036: import org.jboss.portal.registration.Consumer;
037: import org.jboss.portal.registration.ConsumerGroup;
038: import org.jboss.portal.registration.DuplicateRegistrationException;
039: import org.jboss.portal.registration.NoSuchRegistrationException;
040: import org.jboss.portal.registration.Registration;
041: import org.jboss.portal.registration.RegistrationException;
042: import org.jboss.portal.registration.RegistrationLocal;
043: import org.jboss.portal.registration.RegistrationPersistenceManager;
044: import org.jboss.portal.registration.RegistrationStatus;
045:
046: import javax.naming.InitialContext;
047: import java.util.Collection;
048: import java.util.Iterator;
049: import java.util.Map;
050:
051: /**
052: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
053: * @version $Revision: 8786 $
054: */
055: public class PersistentPortletStatePersistenceManager extends
056: AbstractJBossService implements PortletStatePersistenceManager,
057: RegistrationPersistenceManager {
058:
059: /** . */
060: protected SessionFactory sessionFactory;
061:
062: /** . */
063: protected String sessionFactoryJNDIName;
064:
065: /** . */
066: protected ObjectContextualizer contextualizer;
067:
068: public String getSessionFactoryJNDIName() {
069: return sessionFactoryJNDIName;
070: }
071:
072: public void setSessionFactoryJNDIName(String sessionFactoryJNDIName) {
073: this .sessionFactoryJNDIName = sessionFactoryJNDIName;
074: }
075:
076: public PortletStateContext loadState(String id)
077: throws InvalidStateIdException, NoSuchStateException {
078: Session session = getCurrentSession();
079: return loadState(session, id);
080: }
081:
082: public String createState(String portletId, PropertyMap propertyMap) {
083: if (portletId == null) {
084: throw new IllegalArgumentException("id cannot be null");
085: }
086: if (propertyMap == null) {
087: throw new IllegalArgumentException(
088: "No null value map accepted");
089: }
090:
091: //
092: Session session = getCurrentSession();
093:
094: // Create the persistent state
095: PersistentPortletState context = new PersistentPortletState(
096: portletId, propertyMap);
097: session.persist(context);
098:
099: // Create relationship with registration if it exists
100: PersistentRegistration registration = (PersistentRegistration) RegistrationLocal
101: .getRegistration();
102: if (registration != null) {
103: registration.getRelatedPortletStates().add(context);
104: context.setRelatedRegistration(registration);
105: }
106:
107: //
108: session.flush();
109:
110: //
111: return context.getId();
112: }
113:
114: public String cloneState(String stateId, PropertyMap propertyMap)
115: throws InvalidStateIdException, NoSuchStateException {
116: if (stateId == null) {
117: throw new IllegalArgumentException("id cannot be null");
118: }
119: if (propertyMap == null) {
120: throw new IllegalArgumentException(
121: "value map cannot be null");
122: }
123:
124: //
125: Session session = getCurrentSession();
126: PersistentPortletState parentContext = loadState(session,
127: stateId);
128:
129: // Create the persistent state
130: PersistentPortletState context = new PersistentPortletState(
131: parentContext.getPortletId(), propertyMap);
132: session.persist(context);
133:
134: // Make the association
135: context.setParent(parentContext);
136: parentContext.getChildren().add(context);
137: session.update(parentContext);
138:
139: // Create relationship with registration if it exists
140: PersistentRegistration registration = (PersistentRegistration) RegistrationLocal
141: .getRegistration();
142: if (registration != null) {
143: registration.getRelatedPortletStates().add(context);
144: context.setRelatedRegistration(registration);
145: }
146:
147: //
148: session.flush();
149:
150: //
151: return context.getId();
152: }
153:
154: public String cloneState(String stateId)
155: throws IllegalArgumentException, NoSuchStateException,
156: InvalidStateIdException {
157: if (stateId == null) {
158: throw new IllegalArgumentException("id cannot be null");
159: }
160:
161: //
162: Session session = getCurrentSession();
163: PersistentPortletState parentContext = loadState(session,
164: stateId);
165:
166: // Create the persistent state
167: PersistentPortletState context = new PersistentPortletState(
168: parentContext.getPortletId(), new SimplePropertyMap(
169: parentContext.getState().getProperties()));
170: session.persist(context);
171:
172: // Make the association
173: context.setParent(parentContext);
174: parentContext.getChildren().add(context);
175: session.update(parentContext);
176:
177: // Create relationship with registration if it exists
178: PersistentRegistration registration = (PersistentRegistration) RegistrationLocal
179: .getRegistration();
180: if (registration != null) {
181: registration.getRelatedPortletStates().add(context);
182: context.setRelatedRegistration(registration);
183: }
184:
185: //
186: session.flush();
187:
188: //
189: return context.getId();
190: }
191:
192: public void updateState(String stateId, PropertyMap propertyMap)
193: throws InvalidStateIdException, NoSuchStateException {
194: Session session = getCurrentSession();
195: PersistentPortletState context = loadState(session, stateId);
196:
197: //
198: context.entries.clear();
199: for (Iterator i = propertyMap.keySet().iterator(); i.hasNext();) {
200: String key = (String) i.next();
201: Value value = propertyMap.getProperty(key);
202: PersistentPortletStateEntry entry = new PersistentPortletStateEntry(
203: key, value);
204: context.entries.put(key, entry);
205: }
206:
207: //
208: session.update(context);
209: }
210:
211: public void destroyState(String stateId)
212: throws InvalidStateIdException, NoSuchStateException {
213: if (stateId == null) {
214: throw new IllegalArgumentException(
215: "No null state id accepted");
216: }
217:
218: //
219: Session session = getCurrentSession();
220: PersistentPortletState context = loadState(session, stateId);
221:
222: // Efficiently set the children parent to null
223: String update = "update PersistentPortletState p set p.parent=NULL where p.parent=:parent";
224: Query query = session.createQuery(update).setLong("parent",
225: context.getKey().longValue());
226: query.executeUpdate();
227:
228: // Destroy any relationship with registration
229: PersistentRegistration registration = context
230: .getRelatedRegistration();
231: if (registration != null) {
232: registration.getRelatedPortletStates().remove(context);
233: context.setRelatedRegistration(null);
234: }
235:
236: // Delete the state
237: session.delete(context);
238: session.flush();
239: }
240:
241: protected void startService() throws Exception {
242: sessionFactory = (SessionFactory) new InitialContext()
243: .lookup(sessionFactoryJNDIName);
244: contextualizer = new ObjectContextualizer(this );
245: contextualizer.attach(sessionFactory);
246: }
247:
248: protected void stopService() throws Exception {
249: contextualizer = null;
250: sessionFactory = null;
251: }
252:
253: protected Session getCurrentSession() {
254: return sessionFactory.getCurrentSession();
255: }
256:
257: private PersistentPortletState loadState(Session session,
258: String stateId) throws NoSuchStateException,
259: InvalidStateIdException {
260: if (stateId == null) {
261: throw new IllegalArgumentException("id cannot be null");
262: }
263:
264: try {
265: Long key = new Long(stateId);
266: PersistentPortletState context = (PersistentPortletState) session
267: .get(PersistentPortletState.class, key);
268:
269: //
270: if (context == null) {
271: throw new NoSuchStateException(stateId);
272: }
273:
274: //
275: return context;
276: } catch (NumberFormatException e) {
277: throw new InvalidStateIdException(e, stateId);
278: }
279: }
280:
281: // RegistrationPersistenceManager ***********************************************************************************
282:
283: public Consumer createConsumer(String consumerId,
284: String consumerName) throws RegistrationException {
285: if (consumerId == null) {
286: throw new IllegalArgumentException(
287: "No null consumer id accepted");
288: }
289: if (consumerName == null) {
290: throw new IllegalArgumentException(
291: "No null consumer name accepted");
292: }
293:
294: // Get hibernate session
295: Session session = getCurrentSession();
296:
297: //
298: PersistentConsumer consumer = new PersistentConsumer(
299: consumerId, consumerName);
300: session.persist(consumer);
301:
302: //
303: return consumer;
304: }
305:
306: public ConsumerGroup getConsumerGroup(String name)
307: throws RegistrationException {
308: // Get hibernate session
309: Session session = getCurrentSession();
310:
311: //
312: return findGroupByName(session, name);
313: }
314:
315: public ConsumerGroup createConsumerGroup(String name)
316: throws RegistrationException {
317: if (name == null) {
318: throw new IllegalArgumentException("No null name accepted");
319: }
320:
321: // Get hibernate session
322: Session session = getCurrentSession();
323: PersistentConsumerGroup group = null;
324:
325: // Detect duplicate
326: if (findGroupByName(session, name) != null) {
327: throw new DuplicateRegistrationException("Group " + name
328: + " already exists");
329: }
330:
331: // Create and persist
332: group = new PersistentConsumerGroup(this , name);
333: session.persist(group);
334:
335: //
336: return group;
337: }
338:
339: public void removeConsumerGroup(String name)
340: throws RegistrationException {
341: // Get hibernate session
342: Session session = getCurrentSession();
343:
344: //
345: PersistentConsumerGroup group = getGroupByName(session, name);
346:
347: //
348: session.delete(group);
349: }
350:
351: public void removeConsumer(String consumerId)
352: throws RegistrationException {
353: // Get hibernate session
354: Session session = getCurrentSession();
355:
356: //
357: PersistentConsumer consumer = getConsumerById(session,
358: consumerId);
359:
360: //
361: session.delete(consumer);
362: }
363:
364: public void removeRegistration(String registrationId)
365: throws RegistrationException {
366: if (registrationId == null) {
367: throw new IllegalArgumentException(
368: "No null registration id accepted");
369: }
370:
371: // Get hibernate session
372: Session session = getCurrentSession();
373:
374: //
375: PersistentRegistration registration = getRegistrationById(
376: session, registrationId);
377:
378: // Get related consumer
379: PersistentConsumer consumer = registration.getRelatedConsumer();
380:
381: // Destroy relationship
382: consumer.getRelatedRegistrations().remove(registration);
383: registration.setRelatedConsumer(null);
384:
385: // Delete the registration
386: session.delete(registration);
387: session.flush();
388: }
389:
390: public Consumer getConsumerById(String consumerId)
391: throws RegistrationException {
392: // Get hibernate session
393: Session session = getCurrentSession();
394:
395: //
396: return findConsumerById(session, consumerId);
397: }
398:
399: public Registration addRegistrationFor(String consumerId,
400: Map registrationProperties) throws RegistrationException {
401: if (registrationProperties == null) {
402: throw new IllegalArgumentException(
403: "No null registration properties accepted");
404: }
405:
406: // Get hibernate session
407: Session session = getCurrentSession();
408:
409: // Perform lookup
410: PersistentConsumer consumer = getConsumerById(session,
411: consumerId);
412:
413: // Create and persist registration and build relationship
414: PersistentRegistration registration = new PersistentRegistration(
415: registrationProperties, RegistrationStatus.PENDING);
416: registration.setRelatedConsumer(consumer);
417: consumer.getRelatedRegistrations().add(registration);
418: session.persist(registration);
419: session.saveOrUpdate(consumer);
420:
421: //
422: return registration;
423: }
424:
425: public Registration getRegistration(String registrationId) {
426: // Get hibernate session
427: Session session = getCurrentSession();
428:
429: //
430: return findRegistrationById(session, registrationId);
431: }
432:
433: public Consumer addConsumerToGroupNamed(String consumerId,
434: String groupName) throws RegistrationException {
435: Consumer consumer = getConsumerById(consumerId);
436:
437: // Build relationship
438: ConsumerGroup group = getConsumerGroup(groupName);
439: consumer.setGroup(group);
440:
441: //
442: return consumer;
443: }
444:
445: public Collection getConsumerGroups() {
446: Session session = getCurrentSession();
447: Query query = session
448: .createQuery("from PersistentConsumerGroup");
449: return query.list();
450: }
451:
452: public Collection getConsumers() {
453: Session session = getCurrentSession();
454: Query query = session.createQuery("from PersistentConsumer");
455: return query.list();
456: }
457:
458: public Collection getRegistrations() {
459: Session session = getCurrentSession();
460: Query query = session
461: .createQuery("from PersistentRegistration");
462: return query.list();
463: }
464:
465: private PersistentRegistration getRegistrationById(Session session,
466: String registrationId) throws IllegalArgumentException,
467: NoSuchRegistrationException {
468: PersistentRegistration registration = findRegistrationById(
469: session, registrationId);
470:
471: //
472: if (registration == null) {
473: throw new NoSuchRegistrationException(
474: "Cant find a consumer with the id "
475: + registrationId);
476: }
477:
478: //
479: return registration;
480: }
481:
482: private PersistentRegistration findRegistrationById(
483: Session session, String registrationId)
484: throws IllegalArgumentException {
485: if (registrationId == null) {
486: throw new IllegalArgumentException(
487: "No null consumer id accepted");
488: }
489:
490: try {
491: // Parse the key
492: Long key = new Long(registrationId);
493:
494: // Perform lookup
495: return (PersistentRegistration) session.get(
496: PersistentRegistration.class, key);
497: } catch (NumberFormatException e) {
498: throw new IllegalArgumentException(
499: "Bad registration id format " + registrationId);
500: }
501: }
502:
503: private PersistentConsumer getConsumerById(Session session,
504: String consumerId) throws IllegalArgumentException,
505: NoSuchRegistrationException {
506: PersistentConsumer consumer = findConsumerById(session,
507: consumerId);
508:
509: //
510: if (consumer == null) {
511: throw new NoSuchRegistrationException(
512: "Cant find a consumer with the id " + consumerId);
513: }
514:
515: //
516: return consumer;
517: }
518:
519: private PersistentConsumer findConsumerById(Session session,
520: String consumerId) throws IllegalArgumentException {
521: if (consumerId == null) {
522: throw new IllegalArgumentException(
523: "No null consumer id accepted");
524: }
525:
526: //
527: Query query = session
528: .createQuery("from PersistentConsumer where persistentId=:consumerId");
529: query.setString("consumerId", consumerId);
530: return (PersistentConsumer) query.uniqueResult();
531: }
532:
533: private PersistentConsumerGroup getGroupByName(Session session,
534: String groupName) throws IllegalArgumentException,
535: NoSuchRegistrationException {
536: PersistentConsumerGroup group = findGroupByName(session,
537: groupName);
538:
539: //
540: if (group == null) {
541: throw new NoSuchRegistrationException(
542: "Cant find a group with the name " + groupName);
543: }
544:
545: //
546: return group;
547: }
548:
549: private PersistentConsumerGroup findGroupByName(Session session,
550: String groupName) throws IllegalArgumentException {
551: if (groupName == null) {
552: throw new IllegalArgumentException(
553: "No null group name accepted");
554: }
555:
556: //
557: Query query = session
558: .createQuery("from PersistentConsumerGroup where persistentName=:groupName");
559: query.setString("groupName", groupName);
560: return (PersistentConsumerGroup) query.uniqueResult();
561: }
562: }
|