weaver      2004/10/29 07:04:16

  Modified:    
components/registry/src/java/org/apache/jetspeed/components/portletentity
                        PortletEntityImpl.java
                        PortletEntityAccessComponentImpl.java
  Added:       
components/registry/src/java/org/apache/jetspeed/components/portletentity
                        PersistenceBrokerPortletEntityAccess.java
  Log:
  see: http://nagoya.apache.org/jira/browse/JS2-144
  - refactor of registry and entity DAO
  
  Revision  Changes    Path
  1.8       +30 -69    
jakarta-jetspeed-2/components/registry/src/java/org/apache/jetspeed/components/portletentity/PortletEntityImpl.java
  
  Index: PortletEntityImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jetspeed-2/components/registry/src/java/org/apache/jetspeed/components/portletentity/PortletEntityImpl.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PortletEntityImpl.java    9 Jul 2004 18:18:48 -0000       1.7
  +++ PortletEntityImpl.java    29 Oct 2004 14:04:16 -0000      1.8
  @@ -28,17 +28,13 @@
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -import org.apache.jetspeed.components.persistence.Storeable;
  -import org.apache.jetspeed.components.persistence.store.Filter;
   import org.apache.jetspeed.components.persistence.store.PersistenceStore;
   import 
org.apache.jetspeed.components.persistence.store.PersistenceStoreRuntimeExcpetion;
   import org.apache.jetspeed.components.persistence.store.RemovalAware;
  -import org.apache.jetspeed.components.persistence.store.Transaction;
   import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
   import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
   import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
   import org.apache.jetspeed.om.common.portlet.PrincipalAware;
  -import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
   import org.apache.jetspeed.om.preference.impl.PrefsPreference;
   import org.apache.jetspeed.om.preference.impl.PrefsPreferenceSetImpl;
   import org.apache.jetspeed.om.window.impl.PortletWindowListImpl;
  @@ -59,7 +55,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Scott T. Weaver </a>
    * @version $Id$
    */
  -public class PortletEntityImpl implements MutablePortletEntity, Storeable, 
PrincipalAware, RemovalAware
  +public class PortletEntityImpl implements MutablePortletEntity, PrincipalAware, 
RemovalAware
   {
   
       private long oid;
  @@ -68,7 +64,7 @@
   
       private JetspeedObjectID id;
   
  -    protected PersistenceStore store;
  +    protected static PortletEntityAccessComponent pac;
   
       private static final Log log = LogFactory.getLog(PortletEntityImpl.class);
   
  @@ -98,19 +94,6 @@
   
       public static final String NO_PRINCIPAL = "no-principal";
   
  -    /**
  -     * <p>
  -     * setPersistenceStore
  -     * </p>
  -     * 
  -     * @see org.apache.jetspeed.components.persistence.Storeable#setStore(Object)
  -     * @param store
  -     */
  -    public void setStore( Object store )
  -    {
  -        this.store = (PersistenceStore) store;
  -    }
  -
       public ObjectID getId()
       {
           return id;
  @@ -193,14 +176,14 @@
   
       public PortletDefinition getPortletDefinition()
       {
  -        if(portletDefinition == null)
  -        {
  -            Filter filter = store.newFilter();
  -            filter.addEqualTo("app.name", appName);
  -            filter.addEqualTo("name", portletName);
  -            Object query = store.newQuery(PortletDefinitionImpl.class, filter);
  -            this.portletDefinition = (PortletDefinitionComposite) 
store.getObjectByQuery(query);
  -        }
  +//        if(portletDefinition == null)
  +//        {
  +//            Filter filter = store.newFilter();
  +//            filter.addEqualTo("app.name", appName);
  +//            filter.addEqualTo("name", portletName);
  +//            Object query = store.newQuery(PortletDefinitionImpl.class, filter);
  +//            this.portletDefinition = (PortletDefinitionComposite) 
store.getObjectByQuery(query);
  +//        }
           return this.portletDefinition;
       }
   
  @@ -223,37 +206,19 @@
        */
       public void store() throws IOException
       {
  -        if (store == null)
  +        if (pac == null)
           {
  -            throw new IllegalStateException("You must call 
PortletEntityImpl.setStore() before "
  +            throw new IllegalStateException("You must call 
PortletEntityImpl.setPorteltEntityDao() before "
                       + "invoking PortletEntityImpl.store().");
           }
   
  -        PrefsPreferenceSetImpl preferenceSet = (PrefsPreferenceSetImpl) 
preferenceSetRef.get();
  -        try
  -        {
  -            prepareTransaction(store);
  -            store.lockForWrite(this);
  -            if (preferenceSet != null)
  -            {
  -                preferenceSet.flush();
  -            }
  -            store.getTransaction().checkpoint();
  -            dirty = false;
  -            if (preferenceSet != null)
  -            {
  -                backupValues(preferenceSet);
  -            }
  -        }
  -        catch (Exception e)
  +        PreferenceSet preferenceSet = (PreferenceSet)preferenceSetRef.get();
  +        pac.storePreferenceSet(preferenceSet, this);
  +        dirty = false;
  +        if (preferenceSet != null)
           {
  -            String msg = "Failed to store portlet entity:" + e.toString();
  -            IOException ioe = new IOException(msg);
  -            ioe.initCause(e);
  -            store.getTransaction().rollback();
  -            throw ioe;
  +            backupValues(preferenceSet);
           }
  -
       }
   
       /**
  @@ -331,7 +296,14 @@
           buffer.append("'");
           StringUtils.newLine(buffer, indent);
           buffer.append("definition-id='");
  -        buffer.append(portletDefinition.getId().toString());
  +        if(portletDefinition != null)
  +        {
  +            buffer.append(portletDefinition.getId().toString());
  +        }
  +        else
  +        {
  +            buffer.append("null");
  +        }
           buffer.append("'");
   
           StringUtils.newLine(buffer, indent);
  @@ -366,21 +338,6 @@
       }
   
       /**
  -     * Checks to see if the <code>store</code>'s current transaction needs to
  -     * be started or not.
  -     * 
  -     * @param store
  -     */
  -    protected void prepareTransaction( PersistenceStore store )
  -    {
  -        Transaction tx = store.getTransaction();
  -        if (!tx.isOpen())
  -        {
  -            tx.begin();
  -        }
  -    }
  -
  -    /**
        * @return Returns the principal.
        */
       public Principal getPrincipal()
  @@ -513,5 +470,9 @@
               throw new PersistenceStoreRuntimeExcpetion(e.toString(), e);
           }        
   
  +    }
  +    public String getPortletUniqueName()
  +    {
  +        return this.appName+"::"+this.portletName;
       }
   }
  
  
  
  1.24      +39 -11    
jakarta-jetspeed-2/components/registry/src/java/org/apache/jetspeed/components/portletentity/PortletEntityAccessComponentImpl.java
  
  Index: PortletEntityAccessComponentImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jetspeed-2/components/registry/src/java/org/apache/jetspeed/components/portletentity/PortletEntityAccessComponentImpl.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- PortletEntityAccessComponentImpl.java     12 Oct 2004 19:56:20 -0000      1.23
  +++ PortletEntityAccessComponentImpl.java     29 Oct 2004 14:04:16 -0000      1.24
  @@ -15,6 +15,7 @@
    */
   package org.apache.jetspeed.components.portletentity;
   
  +import java.io.IOException;
   import java.security.Principal;
   import java.util.Collection;
   import java.util.HashMap;
  @@ -22,17 +23,19 @@
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -import org.apache.jetspeed.components.persistence.Storeable;
   import org.apache.jetspeed.components.persistence.store.Filter;
   import org.apache.jetspeed.components.persistence.store.LockFailedException;
   import org.apache.jetspeed.components.persistence.store.PersistenceStore;
   import org.apache.jetspeed.components.persistence.store.Transaction;
  -import org.apache.jetspeed.components.portletregistry.PortletRegistryComponent;
  +import org.apache.jetspeed.components.portletregistry.PortletRegistry;
   import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
   import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
  +import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
   import org.apache.jetspeed.om.page.Fragment;
  +import org.apache.jetspeed.om.preference.impl.PrefsPreferenceSetImpl;
   import org.apache.jetspeed.util.JetspeedObjectID;
   import org.apache.pluto.om.common.ObjectID;
  +import org.apache.pluto.om.common.PreferenceSet;
   import org.apache.pluto.om.entity.PortletEntity;
   import org.apache.pluto.om.entity.PortletEntityCtrl;
   import org.apache.pluto.om.portlet.PortletDefinition;
  @@ -58,7 +61,7 @@
   
       private PersistenceStore persistenceStore;
   
  -    private PortletRegistryComponent registry;
  +    private PortletRegistry registry;
   
       protected Principal principal;
   
  @@ -67,10 +70,11 @@
        * @param persistenceStore
        * @param registry
        */
  -    public PortletEntityAccessComponentImpl( PersistenceStore persistenceStore, 
PortletRegistryComponent registry )
  +    public PortletEntityAccessComponentImpl( PersistenceStore persistenceStore, 
PortletRegistry registry )
       {
           this.persistenceStore = persistenceStore;
           this.registry = registry;
  +        PortletEntityImpl.pac = this;
   
       }
   
  @@ -190,8 +194,7 @@
       {
           if (entityCache.get(entityId) != null)
           {
  -            PortletEntityImpl entity = (PortletEntityImpl) 
entityCache.get(entityId);
  -            entity.setStore(persistenceStore);
  +            PortletEntityImpl entity = (PortletEntityImpl) 
entityCache.get(entityId);            
               return entity;
           }
           else
  @@ -202,14 +205,18 @@
               Filter filter = store.newFilter();
               filter.addEqualTo("id", entityId.toString());
               Object q = store.newQuery(PortletEntityImpl.class, filter);
  -            PortletEntity portletEntity = (PortletEntity) store.getObjectByQuery(q);
  +            MutablePortletEntity portletEntity = (MutablePortletEntity) 
store.getObjectByQuery(q);
               if (portletEntity == null)
               {
                   return null;
               }
               else
               {
  -                ((Storeable) portletEntity).setStore(persistenceStore);
  +                
  +                String portletUniqueName = portletEntity.getPortletUniqueName();
  +                PortletDefinitionComposite parentPortletDef = registry
  +                        .getPortletDefinitionByUniqueName(portletUniqueName);
  +                ((PortletEntityCtrl) 
portletEntity).setPortletDefinition(parentPortletDef);
                   entityCache.put(entityId, portletEntity);
                   return (PortletEntityImpl) portletEntity;
               }
  @@ -225,10 +232,7 @@
   
           portletEntity.setPortletDefinition(portletDefinition);
   
  -        portletEntity.setStore(persistenceStore);
  -
           return (PortletEntityImpl) portletEntity;
  -
       }
   
       /**
  @@ -352,4 +356,28 @@
           return persistenceStore;
       }
   
  +    public void storePreferenceSet( PreferenceSet prefSet, PortletEntity entity ) 
throws IOException
  +    {
  +        PrefsPreferenceSetImpl preferenceSet = (PrefsPreferenceSetImpl) prefSet;
  +        try
  +        {
  +            prepareTransaction(persistenceStore);
  +            persistenceStore.lockForWrite(entity);
  +            if (preferenceSet != null)
  +            {
  +                preferenceSet.flush();
  +            }
  +            persistenceStore.getTransaction().checkpoint();
  +
  +        }
  +        catch (Exception e)
  +        {
  +            String msg = "Failed to store portlet entity:" + e.toString();
  +            IOException ioe = new IOException(msg);
  +            ioe.initCause(e);
  +            persistenceStore.getTransaction().rollback();
  +            throw ioe;
  +        }
  +
  +    }
   }
  
  
  
  1.1                  
jakarta-jetspeed-2/components/registry/src/java/org/apache/jetspeed/components/portletentity/PersistenceBrokerPortletEntityAccess.java
  
  Index: PersistenceBrokerPortletEntityAccess.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.components.portletentity;
  
  import java.io.IOException;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.prefs.BackingStoreException;
  
  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
  import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
  import org.apache.jetspeed.om.page.Fragment;
  import org.apache.jetspeed.om.preference.impl.PrefsPreferenceSetImpl;
  import org.apache.jetspeed.util.JetspeedObjectID;
  import org.apache.ojb.broker.query.Criteria;
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.broker.query.QueryFactory;
  import org.apache.pluto.om.common.ObjectID;
  import org.apache.pluto.om.common.PreferenceSet;
  import org.apache.pluto.om.entity.PortletEntity;
  import org.apache.pluto.om.entity.PortletEntityCtrl;
  import org.apache.pluto.om.portlet.PortletDefinition;
  import org.springframework.orm.ojb.support.PersistenceBrokerDaoSupport;
  
  /**
   * <p>
   * PersistenceStorePortletEntityAccess
   * </p>
   * <p>
   * 
   * </p>
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Scott T. Weaver </a>
   * @version $Id: PersistenceBrokerPortletEntityAccess.java,v 1.1 2004/10/29 14:04:16 
weaver Exp $
   *  
   */
  public class PersistenceBrokerPortletEntityAccess extends PersistenceBrokerDaoSupport
          implements
              PortletEntityAccessComponent
  {
      private PortletRegistry registry;
  
      /**
       * 
       * @param registry
       */
      public PersistenceBrokerPortletEntityAccess( PortletRegistry registry )
      {
          super();
          this.registry = registry;
          PortletEntityImpl.pac = this;
      }
  
      /**
       * 
       * <p>
       * generateEntityFromFragment
       * </p>
       * 
       * @see 
org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent#generateEntityFromFragment(org.apache.jetspeed.om.page.Fragment,
       *      java.lang.String)
       * @param fragment
       * @param principal
       * @return @throws
       *         PortletEntityNotGeneratedException
       */
      public MutablePortletEntity generateEntityFromFragment( Fragment fragment, 
String principal )
              throws PortletEntityNotGeneratedException
      {
          PortletDefinition pd = 
registry.getPortletDefinitionByUniqueName(fragment.getName());
          ObjectID entityKey = generateEntityKey(fragment, principal);
  
          if (pd == null)
          {
              throw new PortletEntityNotGeneratedException("Failed to retrieve Portlet 
Definition for "
                      + fragment.getName());
          }
  
          MutablePortletEntity portletEntity = newPortletEntityInstance(pd);
          if (portletEntity == null)
          {
              throw new PortletEntityNotGeneratedException("Failed to create Portlet 
Entity for " + fragment.getName());
          }
          portletEntity.setId(entityKey.toString());
  
          return portletEntity;
      }
  
      /**
       * 
       * <p>
       * generateEntityFromFragment
       * </p>
       * 
       * @see 
org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent#generateEntityFromFragment(org.apache.jetspeed.om.page.Fragment)
       * @param fragment
       * @return @throws
       *         PortletEntityNotGeneratedException
       */
      public MutablePortletEntity generateEntityFromFragment( Fragment fragment )
              throws PortletEntityNotGeneratedException
      {
          return generateEntityFromFragment(fragment, null);
      }
  
      /**
       * 
       * <p>
       * generateEntityKey
       * </p>
       * 
       * @see 
org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent#generateEntityKey(org.apache.jetspeed.om.page.Fragment,
       *      java.lang.String)
       * @param fragment
       * @param principal
       * @return
       */
      public ObjectID generateEntityKey( Fragment fragment, String principal )
      {
          StringBuffer key = new StringBuffer();
          if (principal != null && principal.length() > 0)
          {
              key.append(principal);
              key.append("/");
          }
          key.append(fragment.getId());
          return JetspeedObjectID.createFromString(key.toString());
      }
  
      /**
       * 
       * <p>
       * getPortletEntities
       * </p>
       * 
       * @see 
org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent#getPortletEntities(org.apache.pluto.om.portlet.PortletDefinition)
       * @param portletDefinition
       * @return
       */
      public Collection getPortletEntities( PortletDefinition portletDefinition )
      {
          Criteria c = new Criteria();
          String appName = ((MutablePortletApplication) 
portletDefinition.getPortletApplicationDefinition()).getName();
          String portletName = portletDefinition.getName();
          c.addEqualTo("appName", appName);
          c.addEqualTo("portletName", portletName);
  
          return 
getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(PortletEntityImpl.class,
 c));
      }
  
      public MutablePortletEntity getPortletEntity( ObjectID id )
      {
          Criteria c = new Criteria();
          c.addEqualTo("id", id.toString());
          Query q = QueryFactory.newQuery(PortletEntityImpl.class, c);
          MutablePortletEntity portletEntity = (MutablePortletEntity) 
getPersistenceBrokerTemplate().getObjectByQuery(q);
          if (portletEntity == null)
          {
              return null;
          }
          else
          {
  
              String portletUniqueName = portletEntity.getPortletUniqueName();
              PortletDefinitionComposite parentPortletDef = 
registry.getPortletDefinitionByUniqueName(portletUniqueName);
              if(parentPortletDef != null)
              {
                  ((PortletEntityCtrl) 
portletEntity).setPortletDefinition(parentPortletDef);
                  return (MutablePortletEntity) portletEntity;
              }
              else
              {
                  logger.warn("No parent portlet definition could be located using 
unique name: "+portletUniqueName+
                              ".  Unless you plan on redploying this portlet 
definition, it is highly recommended "+
                              "that you delete the orphaned portlet entity with the 
id: "+portletEntity.getId());
                  return (MutablePortletEntity) portletEntity;
              }
                  
          }
      }
  
      public MutablePortletEntity getPortletEntity( String id )
      {
          ObjectID oid = JetspeedObjectID.createFromString(id);
          return getPortletEntity(oid);
      }
  
      public MutablePortletEntity getPortletEntityForFragment( Fragment fragment, 
String principal )
      {
          return getPortletEntity(generateEntityKey(fragment, principal));
      }
  
      public MutablePortletEntity getPortletEntityForFragment( Fragment fragment )
      {
          return getPortletEntity(generateEntityKey(fragment, null));
      }
  
      public MutablePortletEntity newPortletEntityInstance( PortletDefinition 
portletDefinition )
      {
          PortletEntityImpl portletEntity = new PortletEntityImpl();
  
          portletEntity.setPortletDefinition(portletDefinition);
  
          return (PortletEntityImpl) portletEntity;
      }
  
      public void removeFromCache( PortletEntity entity )
      {
          // TODO Auto-generated method stub
  
      }
  
      public void removePortletEntities( PortletDefinition portletDefinition ) throws 
PortletEntityNotDeletedException
      {
          Iterator entities = getPortletEntities(portletDefinition).iterator();
          while (entities.hasNext())
          {
              PortletEntity entity = (PortletEntity) entities.next();
              removePortletEntity(entity);
          }
  
      }
  
      public void removePortletEntity( PortletEntity portletEntity ) throws 
PortletEntityNotDeletedException
      {
          PrefsPreferenceSetImpl prefsSet  = (PrefsPreferenceSetImpl) 
portletEntity.getPreferenceSet();
          getPersistenceBrokerTemplate().delete(portletEntity);
          try
          {
              prefsSet.clear();
          }
          catch (BackingStoreException e)
          {
              throw new PortletEntityNotDeletedException("Failed to remove preferences 
for portlet entity "+portletEntity.getId()+".  "+e.getMessage(), e);
          }
      }
  
      public void storePortletEntity( PortletEntity portletEntity ) throws 
PortletEntityNotStoredException
      {
          try
          {
              ((PortletEntityCtrl) portletEntity).store();
          }
          catch (Exception e)
          {
              throw new PortletEntityNotStoredException(e.toString(), e);
          }
  
      }
  
      /**
       * <p>
       * storePreferenceSet
       * </p>
       * 
       * @see 
org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent#storePreferenceSet(org.apache.pluto.om.common.PreferenceSet)
       * @param prefSet
       * @throws IOException
       */
      public void storePreferenceSet( PreferenceSet prefSet, PortletEntity entity ) 
throws IOException
      {
          PrefsPreferenceSetImpl preferenceSet = (PrefsPreferenceSetImpl) prefSet;
          try
          {            
              getPersistenceBrokerTemplate().store(entity);
              if (preferenceSet != null)
              {
                  preferenceSet.flush();
              }            
  
          }
          catch (Exception e)
          {
              String msg = "Failed to store portlet entity:" + e.toString();
              IOException ioe = new IOException(msg);
              ioe.initCause(e);            
              throw ioe;
          }
  
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to