Commits

Anonymous committed 80bdc75

This commit was manufactured by cvs2svn to create branch 'os'.

git-svn-id: http://svn.opensymphony.com/svn/propertyset/branches/os@46bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

  • Participants
  • Parent commits e024c34
  • Branches os

Comments (0)

Files changed (7)

src/java/com/opensymphony/ejb/AbstractEntityAdapter.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.ejb;
+
+import com.opensymphony.module.propertyset.PropertySet;
+import com.opensymphony.module.propertyset.PropertySetManager;
+import com.opensymphony.module.sequence.SequenceGeneratorHome;
+
+import com.opensymphony.util.EJBUtils;
+import com.opensymphony.util.GUID;
+
+import java.rmi.RemoteException;
+
+import java.util.HashMap;
+
+import javax.ejb.CreateException;
+import javax.ejb.EntityContext;
+
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+
+
+/**
+ * DOCUMENT ME!
+ *
+ * @author $author$
+ * @version $Revision$
+ */
+public class AbstractEntityAdapter {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    /**
+     * Reference to EntityContext.
+     */
+    protected EntityContext context;
+
+    /**
+     * Name of the auto-generating sequence.
+     */
+    protected String sequenceName;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    /**
+     * Sets the context - should be used only by direct subclasses
+     * Also, attempts to determine the <code>sequenceName</code>.
+     */
+    protected void setContext(EntityContext context) {
+        this.context = context;
+
+        try {
+            sequenceName = (String) EJBUtils.lookup("sequenceName", String.class);
+        } catch (Exception e) {
+            sequenceName = "";
+        }
+    }
+
+    /**
+     * Generate random GUID.
+     *
+     * @see com.opensymphony.util.GUID
+     */
+    protected String generateGUID() {
+        return GUID.generateGUID();
+    }
+
+    /**
+     * Locate PropertySet using PropertyStore for this sequenceName/sequenceId mapping.
+     */
+    protected PropertySet locatePropertySet(long id) throws RemoteException {
+        HashMap args = new HashMap(2);
+        args.put("entityId", new Long(id));
+        args.put("entityName", sequenceName);
+
+        return PropertySetManager.getInstance("ejb", args);
+    }
+
+    /**
+     * @deprecated Use {@link #nextInt()} or {@link #nextLong()} instead.
+     */
+    protected int nextId() throws CreateException, RemoteException {
+        return nextInt();
+    }
+
+    /**
+     * Return int unique id key from a unique instance key generator.
+     */
+    protected int nextInt() throws CreateException, RemoteException {
+        try {
+            return (int) nextLong();
+        } catch (ClassCastException e) {
+            throw new CreateException("Cannot generate id: Sequence cannot be downcasted to long.");
+        } catch (NullPointerException e) {
+            throw new CreateException("Cannot generate id: Sequence returning null.");
+        }
+    }
+
+    /**
+     * Return long unique id key from a unique instance key generator.
+     */
+    protected long nextLong() throws CreateException, RemoteException {
+        try {
+            SequenceGeneratorHome sgHome;
+
+            try {
+                if (sequenceName == null) {
+                    sequenceName = (String) EJBUtils.lookup("sequenceName", String.class);
+                }
+
+                sgHome = (SequenceGeneratorHome) EJBUtils.lookup("ejb/SequenceGenerator", SequenceGeneratorHome.class);
+            } catch (NameNotFoundException e) {
+                sgHome = (SequenceGeneratorHome) EJBUtils.lookup("SequenceGenerator", SequenceGeneratorHome.class);
+            }
+
+            return sgHome.create().getCount(sequenceName);
+        } catch (NamingException e) {
+            throw new CreateException("Cannot generate id: " + e.toString());
+        }
+    }
+}

src/java/com/opensymphony/module/propertyset/ejb/types/DataEntityEJB.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.ejb.types;
+
+
+/* ====================================================================
+ * The OpenSymphony Software License, Version 1.1
+ *
+ * (this license is derived and fully compatible with the Apache Software
+ * License - see http://www.apache.org/LICENSE.txt)
+ *
+ * Copyright (c) 2001 The OpenSymphony Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by the
+ *        OpenSymphony Group (http://www.opensymphony.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "OpenSymphony" and "The OpenSymphony Group"
+ *    must not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written
+ *    permission, please contact license@opensymphony.com .
+ *
+ * 5. Products derived from this software may not be called "OpenSymphony"
+ *    or "OSCore", nor may "OpenSymphony" or "OSCore" appear in their
+ *    name, without prior written permission of the OpenSymphony Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ */
+import com.opensymphony.module.propertyset.*;
+
+import com.opensymphony.util.Data;
+import com.opensymphony.util.XMLUtils;
+
+import org.w3c.dom.Document;
+
+import java.io.*;
+
+import java.util.Properties;
+
+import javax.ejb.*;
+
+
+/**
+ * AbstractValueEntityEJB concrete implementation optimized for storing binary data.
+ * This can be used to store TEXT, OBJECT, DATA, XML, PROPERTIES or anything else
+ * that cannot be stored elsewhere.
+ *
+ * @ejb.bean
+ *  type="CMP"
+ *  name="PropertyData"
+ *  view-type="local"
+ *  reentrant="False"
+ *  primkey-field="id"
+ *
+ * @ejb.pk class="java.lang.Long" extends="java.lang.Object"
+ *
+ * @ejb.persistence table-name="OS_PROPERTYDATA"
+ * @ejb.permission unchecked="true"
+ * @ejb.transaction type="Supports"
+ *
+ * @author <a href="mailto:hani@formicary.net">Hani Suleiman</a>
+ * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
+ * @version $Revision$
+ *
+ */
+public abstract class DataEntityEJB implements EntityBean {
+    //~ Static fields/initializers /////////////////////////////////////////////
+
+    /**
+     * Flag to signify no value in a property. Some db's can't store nulls or 0 length fields in LOBs properly.
+     */
+    private static final byte[] NULL_DATA = "hello world".getBytes();
+
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private EntityContext context;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public abstract void setBytes(byte[] data);
+
+    /**
+     * @ejb.persistence column-name="value"
+     */
+    public abstract byte[] getBytes();
+
+    public abstract void setId(Long id);
+
+    /**
+     * @ejb.pk-field
+     * @ejb.interface-method
+     * @ejb.persistence column-name="id"
+     */
+    public abstract Long getId();
+
+    public void setEntityContext(EntityContext context) {
+        this.context = context;
+    }
+
+    /**
+     * Set the value. Depending on the type, different casting serializing will occur.
+     *
+     * <p>For TEXT, value must be java.lang.String.<br>
+     * For OBJECT, value must be Serializable object.<br>
+     * For XML, value must be org.w3c.dom.Document.<br>
+     * For DATA, value must be com.opensymphony.util.Data<br>
+     * For PROPERTIES, value must be java.util.Properties<br>
+     *
+     * @ejb.interface-method
+     */
+    public void setValue(int type, Serializable value) {
+        if (value == null) {
+            setBytes(NULL_DATA);
+
+            return;
+        }
+
+        try {
+            switch (type) {
+            case PropertySet.TEXT:
+                setBytes(((String) value).getBytes());
+
+                break;
+
+            case PropertySet.OBJECT:
+
+                if (!(value instanceof Serializable)) {
+                    throw new IllegalPropertyException("Object not serializable.");
+                }
+
+                setBytes(writeObject(value));
+
+                break;
+
+            case PropertySet.XML:
+                setBytes(writeXML((Document) value));
+
+                break;
+
+            case PropertySet.DATA:
+                setBytes(((Data) value).getBytes());
+
+                break;
+
+            case PropertySet.PROPERTIES:
+                setBytes(writeProperties((Properties) value));
+
+                break;
+
+            default:
+
+                // this should never happen.
+                throw new PropertyImplementationException("Cannot store this type of property.");
+            }
+
+            if (getBytes().length == 0) {
+                setBytes(NULL_DATA); // some db's have problems storing nothing
+            }
+        } catch (ClassCastException ce) {
+            throw new IllegalPropertyException("Cannot cast value to appropriate type for persistence.");
+        }
+    }
+
+    /**
+     * Create appropriate wrapper object around value (String, Object, Document, Data, Properties).
+     *
+     * @ejb.interface-method
+     */
+    public Serializable getValue(int type) {
+        byte[] value = getBytes();
+
+        switch (type) {
+        case PropertySet.TEXT:
+            return new String(value);
+
+        case PropertySet.OBJECT:
+            return (Serializable) readObject(value);
+
+        case PropertySet.XML:
+            return (Serializable) readXML(value);
+
+        case PropertySet.DATA:
+            return new Data(value);
+
+        case PropertySet.PROPERTIES:
+            return readProperties(value);
+
+        default:
+
+            // this should never happen.
+            throw new PropertyImplementationException("Cannot retrieve this type of property.");
+        }
+    }
+
+    public void ejbActivate() {
+    }
+
+    /**
+     * @ejb.create-method
+     */
+    public Long ejbCreate(int type, long id) throws CreateException {
+        setId(new Long(id));
+
+        // the concrete setValue() method shall set value to default (not null).
+        setValue(type, null);
+
+        return null;
+    }
+
+    public void ejbLoad() {
+    }
+
+    public void ejbPassivate() {
+    }
+
+    public void ejbPostCreate(int type, long id) throws CreateException {
+    }
+
+    public void ejbRemove() throws RemoveException {
+    }
+
+    public void ejbStore() {
+    }
+
+    public void unsetEntityContext() {
+        context = null;
+    }
+
+    /**
+     * Returns { PropertySet.TEXT, PropertySet.OBJECT, PropertySet.XML, PropertySet.DATA, PropertySet.PROPERTIES }
+     */
+    protected int[] allowedTypes() {
+        return new int[] {
+            PropertySet.TEXT, PropertySet.OBJECT, PropertySet.XML,
+            PropertySet.DATA, PropertySet.PROPERTIES
+        };
+    }
+
+    /**
+     * DeSerialize an Object from byte array.
+     */
+    private Object readObject(byte[] data) {
+        try {
+            ByteArrayInputStream bytes = new ByteArrayInputStream(data);
+            ObjectInputStream stream = new ObjectInputStream(bytes);
+            Object result = stream.readObject();
+            stream.close();
+
+            return result;
+        } catch (IOException e) {
+            throw new PropertyImplementationException("Cannot deserialize Object", e);
+        } catch (ClassNotFoundException e) {
+            throw new PropertyImplementationException("Class not found for Object", e);
+        }
+    }
+
+    /**
+     * Load java.util.Properties from byte array.
+     */
+    private Properties readProperties(byte[] data) {
+        try {
+            ByteArrayInputStream bytes = new ByteArrayInputStream(data);
+            Properties result = new Properties();
+            result.load(bytes);
+
+            return result;
+        } catch (Exception e) {
+            throw new PropertyImplementationException("Cannot load Properties.", e);
+        }
+    }
+
+    /**
+     * Parse XML document from String in byte array.
+     */
+    private Document readXML(byte[] data) {
+        try {
+            ByteArrayInputStream bytes = new ByteArrayInputStream(data);
+
+            return XMLUtils.parse(bytes);
+        } catch (Exception e) {
+            throw new PropertyImplementationException("Cannot parse XML data.", e);
+        }
+    }
+
+    /**
+     * Serialize an Object to byte array.
+     */
+    private byte[] writeObject(Object o) {
+        try {
+            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+            ObjectOutputStream stream = new ObjectOutputStream(bytes);
+            stream.writeObject(o);
+            stream.close();
+            bytes.flush();
+
+            return bytes.toByteArray();
+        } catch (IOException e) {
+            throw new PropertyImplementationException("Cannot serialize Object", e);
+        }
+    }
+
+    /**
+     * Store java.util.Properties to byte array.
+     */
+    private byte[] writeProperties(Properties p) {
+        try {
+            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+            p.store(bytes, null);
+            bytes.flush();
+
+            return bytes.toByteArray();
+        } catch (IOException e) {
+            throw new PropertyImplementationException("Cannot store Properties.", e);
+        }
+    }
+
+    /**
+     * Serialize (print) XML document to byte array (as String).
+     */
+    private byte[] writeXML(Document doc) {
+        try {
+            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+            XMLUtils.print(doc, bytes);
+            bytes.flush();
+
+            return bytes.toByteArray();
+        } catch (IOException e) {
+            throw new PropertyImplementationException("Cannot serialize XML", e);
+        }
+    }
+}

src/java/com/opensymphony/module/propertyset/ejb/types/DateEntityEJB.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.ejb.types;
+
+
+/* ====================================================================
+ * The OpenSymphony Software License, Version 1.1
+ *
+ * (this license is derived and fully compatible with the Apache Software
+ * License - see http://www.apache.org/LICENSE.txt)
+ *
+ * Copyright (c) 2001 The OpenSymphony Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by the
+ *        OpenSymphony Group (http://www.opensymphony.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "OpenSymphony" and "The OpenSymphony Group"
+ *    must not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written
+ *    permission, please contact license@opensymphony.com .
+ *
+ * 5. Products derived from this software may not be called "OpenSymphony"
+ *    or "OSCore", nor may "OpenSymphony" or "OSCore" appear in their
+ *    name, without prior written permission of the OpenSymphony Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ */
+import com.opensymphony.module.propertyset.PropertyImplementationException;
+import com.opensymphony.module.propertyset.PropertySet;
+
+import java.sql.Timestamp;
+
+import javax.ejb.*;
+
+
+/**
+ * AbstractValueEntityEJB concrete implementation optimized for storing Dates.
+ *
+ * @ejb.bean
+ *  type="CMP"
+ *  view-type="local"
+ *  name="PropertyDate"
+ *  schema="PropertyDate"
+ *  reentrant="False"
+ *  primkey-field="id"
+ *
+ * @ejb.pk class="java.lang.Long" extends="java.lang.Object"
+ *
+ * @ejb.persistence table-name="OS_PROPERTYDATE"
+ *
+ * @ejb.permission unchecked="true"
+ * @ejb.transaction type="Supports"
+ *
+ * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
+ * @version $Revision$
+ *
+ * @see com.opensymphony.module.propertyset.ejb.PropertyStoreEJB
+ */
+public abstract class DateEntityEJB implements EntityBean {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private EntityContext context;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public abstract void setDate(Timestamp date);
+
+    /**
+     * @ejb.persistence column-name="value"
+     */
+    public abstract Timestamp getDate();
+
+    public abstract void setId(Long id);
+
+    /**
+     * @ejb.pk-field
+     * @ejb.interface-method
+     * @ejb.persistence column-name="id"
+     */
+    public abstract Long getId();
+
+    public void setEntityContext(EntityContext context) {
+        this.context = context;
+    }
+
+    /**
+     * Set the value. Only java.util.Timestamp can be supplied.
+     *
+     * @ejb.interface-method
+     */
+    public void setValue(int type, Timestamp value) {
+        if (value == null) {
+            setDate(new Timestamp(0L));
+
+            return;
+        }
+
+        if (type == PropertySet.DATE) {
+            setDate(value);
+
+            return;
+        } else {
+            throw new PropertyImplementationException("Cannot store this type of property.");
+        }
+    }
+
+    /**
+     * Return Date
+     *
+     * @ejb.interface-method
+     */
+    public Timestamp getValue(int type) {
+        switch (type) {
+        case PropertySet.DATE:
+            return getDate();
+
+        default:
+
+            // this should never happen.
+            throw new PropertyImplementationException("Cannot retrieve this type of property.");
+        }
+    }
+
+    public void ejbActivate() {
+    }
+
+    /**
+     * @ejb.create-method
+     */
+    public Long ejbCreate(int type, long id) throws CreateException {
+        setId(new Long(id));
+
+        // the concrete setValue() method shall set value to default (not null).
+        setValue(type, null);
+
+        return null;
+    }
+
+    public void ejbLoad() {
+    }
+
+    public void ejbPassivate() {
+    }
+
+    public void ejbPostCreate(int type, long id) throws CreateException {
+    }
+
+    public void ejbRemove() throws RemoveException {
+    }
+
+    public void ejbStore() {
+    }
+
+    public void unsetEntityContext() {
+        context = null;
+    }
+
+    /**
+     * Returns { PropertySet.DATE }
+     */
+    protected int[] allowedTypes() {
+        return new int[] {PropertySet.DATE};
+    }
+}

src/java/com/opensymphony/module/propertyset/ejb/types/DecimalEntityEJB.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.ejb.types;
+
+
+/* ====================================================================
+ * The OpenSymphony Software License, Version 1.1
+ *
+ * (this license is derived and fully compatible with the Apache Software
+ * License - see http://www.apache.org/LICENSE.txt)
+ *
+ * Copyright (c) 2001 The OpenSymphony Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by the
+ *        OpenSymphony Group (http://www.opensymphony.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "OpenSymphony" and "The OpenSymphony Group"
+ *    must not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written
+ *    permission, please contact license@opensymphony.com .
+ *
+ * 5. Products derived from this software may not be called "OpenSymphony"
+ *    or "OSCore", nor may "OpenSymphony" or "OSCore" appear in their
+ *    name, without prior written permission of the OpenSymphony Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ */
+import com.opensymphony.module.propertyset.PropertyImplementationException;
+import com.opensymphony.module.propertyset.PropertySet;
+
+import javax.ejb.*;
+
+
+/**
+ * AbstractValueEntityEJB concrete implementation optimized for storing decimal numbers.
+ * This can be used to store DOUBLE.
+ *
+ * @ejb.bean
+ *  type="CMP"
+ *  name="PropertyDecimal"
+ *  schema="PropertyDecimal"
+ *  view-type="local"
+ *  reentrant="False"
+ *  primkey-field="id"
+ *
+ * @ejb.pk class="java.lang.Long" extends="java.lang.Object"
+ *
+ * @ejb.persistence table-name="OS_PROPERTYDECIMAL"
+ * @ejb.permission unchecked="true"
+ * @ejb.transaction type="Supports"
+ *
+ * @author <a href="mailto:hani@formicary.net">Hani Suleiman</a>
+ * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
+ * @version $Revision$
+ *
+ * @see com.opensymphony.module.propertyset.ejb.PropertyStoreEJB
+ */
+public abstract class DecimalEntityEJB implements EntityBean {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private EntityContext context;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public abstract void setDecimal(double date);
+
+    /**
+     * @ejb.persistence column-name="value"
+     */
+    public abstract double getDecimal();
+
+    public abstract void setId(Long id);
+
+    /**
+     * @ejb.pk-field
+     * @ejb.interface-method
+     * @ejb.persistence column-name="id"
+     */
+    public abstract Long getId();
+
+    public void setEntityContext(EntityContext context) {
+        this.context = context;
+    }
+
+    /**
+     * Set the value. Only java.lang.Number can be supplied.
+     *
+     * @ejb.interface-method
+     */
+    public void setValue(int type, Double value) {
+        if (value == null) {
+            setDecimal(0.0);
+
+            return;
+        }
+
+        if (type == PropertySet.DOUBLE) {
+            setDecimal(value.doubleValue());
+        } else {
+            throw new PropertyImplementationException("Cannot store this type of property.");
+        }
+    }
+
+    /**
+     * Create appropriate wrapper object around value (Double).
+     *
+     * @ejb.interface-method
+     */
+    public Double getValue(int type) {
+        if (type == PropertySet.DOUBLE) {
+            return new Double(getDecimal());
+        } else {
+            throw new PropertyImplementationException("Cannot retrieve this type of property.");
+        }
+    }
+
+    public void ejbActivate() {
+    }
+
+    /**
+     * @ejb.create-method
+     */
+    public Long ejbCreate(int type, long id) throws CreateException {
+        setId(new Long(id));
+
+        // the concrete setValue() method shall set value to default (not null).
+        setValue(type, null);
+
+        return null;
+    }
+
+    public void ejbLoad() {
+    }
+
+    public void ejbPassivate() {
+    }
+
+    public void ejbPostCreate(int type, long id) throws CreateException {
+    }
+
+    public void ejbRemove() throws RemoveException {
+    }
+
+    public void ejbStore() {
+    }
+
+    public void unsetEntityContext() {
+        context = null;
+    }
+
+    /**
+     * Returns { PropertySet.DOUBLE }
+     */
+    protected int[] allowedTypes() {
+        return new int[] {PropertySet.DOUBLE};
+    }
+}

src/java/com/opensymphony/module/propertyset/ejb/types/NumberEntityEJB.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.ejb.types;
+
+
+/* ====================================================================
+ * The OpenSymphony Software License, Version 1.1
+ *
+ * (this license is derived and fully compatible with the Apache Software
+ * License - see http://www.apache.org/LICENSE.txt)
+ *
+ * Copyright (c) 2001 The OpenSymphony Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by the
+ *        OpenSymphony Group (http://www.opensymphony.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "OpenSymphony" and "The OpenSymphony Group"
+ *    must not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written
+ *    permission, please contact license@opensymphony.com .
+ *
+ * 5. Products derived from this software may not be called "OpenSymphony"
+ *    or "OSCore", nor may "OpenSymphony" or "OSCore" appear in their
+ *    name, without prior written permission of the OpenSymphony Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ */
+import com.opensymphony.module.propertyset.*;
+
+import java.io.Serializable;
+
+import javax.ejb.*;
+
+
+/**
+ * AbstractValueEntityEJB concrete implementation optimized for storing whole numbers
+ * (i.e. integers). This can be used to store BOOLEAN, INT and LONG.
+ *
+ * @ejb.bean
+ *  type="CMP"
+ *  name="PropertyNumber"
+ *  schema="PropertyNumber"
+ *  view-type="local"
+ *  reentrant="False"
+ *  primkey-field="id"
+ *
+ * @ejb.pk class="java.lang.Long" extends="java.lang.Object"
+ *
+ * @ejb.persistence table-name="OS_PROPERTYNUMBER"
+ * @ejb.permission unchecked="true"
+ * @ejb.transaction type="Supports"
+ *
+ * @author <a href="mailto:hani@formicary.net">Hani Suleiman</a>
+ * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
+ * @version $Revision$
+ *
+ * @see com.opensymphony.module.propertyset.ejb.PropertyStoreEJB
+ */
+public abstract class NumberEntityEJB implements EntityBean {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private EntityContext context;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public abstract void setId(Long id);
+
+    /**
+     * @ejb.pk-field
+     * @ejb.interface-method
+     * @ejb.persistence column-name="id"
+     */
+    public abstract Long getId();
+
+    public abstract void setNumber(long date);
+
+    /**
+     * @ejb.persistence column-name="value"
+     */
+    public abstract long getNumber();
+
+    public void setEntityContext(EntityContext context) {
+        this.context = context;
+    }
+
+    /**
+     * Set the value. Only java.lang.Number, java.lang.Boolean or java.util.Date can be supplied.
+     *
+     * @ejb.interface-method
+     */
+    public void setValue(int type, Serializable value) {
+        if (value == null) {
+            setNumber(0L);
+
+            return;
+        }
+
+        try {
+            switch (type) {
+            case PropertySet.BOOLEAN:
+                setNumber(((Boolean) value).booleanValue() ? 1L : 0L);
+
+                return;
+
+            case PropertySet.INT:
+            case PropertySet.LONG:
+                setNumber(((Number) value).longValue());
+
+                return;
+
+            default:
+
+                // this should never happen.
+                throw new PropertyImplementationException("Cannot store this type of property.");
+            }
+        } catch (ClassCastException ce) {
+            throw new IllegalPropertyException("Cannot cast value to appropriate type for persistence.");
+        }
+    }
+
+    /**
+     * Create appropriate wrapper object around value (Boolean, Integer, Long).
+     *
+     * @ejb.interface-method
+     */
+    public Serializable getValue(int type) {
+        long value = getNumber();
+
+        switch (type) {
+        case PropertySet.BOOLEAN:
+            return (value == 0L) ? Boolean.FALSE : Boolean.TRUE;
+
+        case PropertySet.INT:
+            return new Integer((int) value);
+
+        case PropertySet.LONG:
+            return new Long(value);
+
+        default:
+
+            // this should never happen.
+            throw new PropertyImplementationException("Cannot retrieve this type of property.");
+        }
+    }
+
+    public void ejbActivate() {
+    }
+
+    /**
+     * @ejb.create-method
+     */
+    public Long ejbCreate(int type, long id) throws CreateException {
+        setId(new Long(id));
+
+        // the concrete setValue() method shall set value to default (not null).
+        setValue(type, null);
+
+        return null;
+    }
+
+    public void ejbLoad() {
+    }
+
+    public void ejbPassivate() {
+    }
+
+    public void ejbPostCreate(int type, long id) throws CreateException {
+    }
+
+    public void ejbRemove() throws RemoveException {
+    }
+
+    public void ejbStore() {
+    }
+
+    public void unsetEntityContext() {
+        context = null;
+    }
+
+    /**
+     * Returns { PropertySet.BOOLEAN, PropertySet.INT, PropertySet.LONG }
+     */
+    protected int[] allowedTypes() {
+        return new int[] {PropertySet.BOOLEAN, PropertySet.INT, PropertySet.LONG};
+    }
+}

src/java/com/opensymphony/module/propertyset/ejb/types/PropertyEntryEJB.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.ejb.types;
+
+
+/* ====================================================================
+ * The OpenSymphony Software License, Version 1.1
+ *
+ * (this license is derived and fully compatible with the Apache Software
+ * License - see http://www.apache.org/LICENSE.txt)
+ *
+ * Copyright (c) 2001 The OpenSymphony Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by the
+ *        OpenSymphony Group (http://www.opensymphony.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "OpenSymphony" and "The OpenSymphony Group"
+ *    must not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written
+ *    permission, please contact license@opensymphony.com .
+ *
+ * 5. Products derived from this software may not be called "OpenSymphony"
+ *    or "OSCore", nor may "OpenSymphony" or "OSCore" appear in their
+ *    name, without prior written permission of the OpenSymphony Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ */
+import com.opensymphony.module.propertyset.PropertySet;
+import com.opensymphony.module.sequence.SequenceGenerator;
+import com.opensymphony.module.sequence.SequenceGeneratorHome;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.Serializable;
+
+import java.sql.Timestamp;
+
+import javax.ejb.*;
+
+import javax.naming.InitialContext;
+
+import javax.rmi.PortableRemoteObject;
+
+
+/**
+ * @ejb.bean
+ *  type="CMP"
+ *  view-type="local"
+ *  name="PropertyEntry"
+ *  reentrant="False"
+ *  schema="PropertyEntry"
+ *  primkey-field="id"
+ *
+ * @ejb.pk class="java.lang.Long" extends="java.lang.Object"
+ *
+ * @ejb.persistence table-name="OS_PROPERTYENTRY"
+ *
+ * @ejb.ejb-ref
+ *  ejb-name="PropertyNumber"
+ *  view-type="local"
+ * @ejb.ejb-ref
+ *  ejb-name="PropertyDate"
+ *  view-type="local"
+ * @ejb.ejb-ref
+ *  ejb-name="PropertyData"
+ *  view-type="local"
+ * @ejb.ejb-ref
+ *  ejb-name="PropertyString"
+ *  view-type="local"
+ * @ejb.ejb-ref
+ *  ejb-name="PropertyDecimal"
+ *  view-type="local"
+ *
+ * @ejb.ejb-external-ref
+ *  ref-name="ejb/SequenceGenerator"
+ *  type="Session"
+ *  view-type="remote"
+ *  link="SequenceGenerator"
+ *  home="com.opensymphony.module.sequence.SequenceGeneratorHome"
+ *  business="com.opensymphony.module.sequence.SequenceGenerator"
+ *
+ * @ejb.finder
+ *  signature="java.util.Collection findByNameAndId(java.lang.String entityName, long entityId)"
+ *  query="SELECT DISTINCT OBJECT(o) FROM PropertyEntry o WHERE o.entityName = ?1 AND o.entityId = ?2"
+ *
+ * @ejb.finder
+ *  signature="com.opensymphony.module.propertyset.ejb.types.PropertyEntry findByEntity(java.lang.String entityName, long entityId, java.lang.String key)"
+ *  query="SELECT DISTINCT OBJECT(o) FROM PropertyEntry o WHERE o.entityName = ?1 AND o.entityId = ?2 AND o.key = ?3"
+ *
+ * @ejb.permission unchecked="true"
+ * @ejb.transaction type="Supports"
+ */
+public abstract class PropertyEntryEJB implements EntityBean {
+    //~ Static fields/initializers /////////////////////////////////////////////
+
+    private static final Log logger = LogFactory.getLog(PropertyEntryEJB.class);
+
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private EntityContext context;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public abstract void setEntityId(long entityId);
+
+    /**
+     * @ejb.interface-method
+     * @ejb.persistence column-name="entityid"
+     */
+    public abstract long getEntityId();
+
+    public abstract void setEntityName(String entityName);
+
+    /**
+     * @ejb.interface-method
+     * @ejb.persistence column-name="entityname"
+     */
+    public abstract String getEntityName();
+
+    public abstract void setId(Long id);
+
+    /**
+     * @ejb.pk-field
+     * @ejb.interface-method
+     * @ejb.persistence column-name="id"
+     */
+    public abstract Long getId();
+
+    public abstract void setKey(String key);
+
+    /**
+     * @ejb.interface-method
+     * @ejb.persistence column-name="keyvalue"
+     */
+    public abstract String getKey();
+
+    public abstract void setType(int type);
+
+    /**
+     * @ejb.interface-method
+     * @ejb.persistence column-name="type"
+     */
+    public abstract int getType();
+
+    public void setEntityContext(EntityContext context) {
+        this.context = context;
+    }
+
+    /**
+     * @ejb.interface-method
+     */
+    public void setValue(Serializable value) {
+        int type = getType();
+        Long id = getId();
+
+        try {
+            if ((type == PropertySet.BOOLEAN) || (type == PropertySet.INT) || (type == PropertySet.LONG)) {
+                PropertyNumberLocalHome home = PropertyNumberHomeFactory.getLocalHome();
+                home.findByPrimaryKey(id).setValue(type, value);
+            } else if (type == PropertySet.DATE) {
+                PropertyDateLocalHome home = PropertyDateHomeFactory.getLocalHome();
+                home.findByPrimaryKey(id).setValue(type, (Timestamp) value);
+            } else if (type == PropertySet.DOUBLE) {
+                PropertyDecimalLocalHome home = PropertyDecimalHomeFactory.getLocalHome();
+                home.findByPrimaryKey(id).setValue(type, (Double) value);
+            } else if (type == PropertySet.STRING) {
+                PropertyStringLocalHome home = PropertyStringHomeFactory.getLocalHome();
+                home.findByPrimaryKey(id).setValue(type, (String) value);
+            } else {
+                PropertyDataLocalHome home = PropertyDataHomeFactory.getLocalHome();
+                home.findByPrimaryKey(id).setValue(type, value);
+            }
+        } catch (Exception e) {
+            logger.error("Error setting value in PropertySet", e);
+        }
+    }
+
+    /**
+     * @ejb.interface-method
+     */
+    public Serializable getValue() {
+        int type = getType();
+        Long id = getId();
+
+        try {
+            if ((type == PropertySet.BOOLEAN) || (type == PropertySet.INT) || (type == PropertySet.LONG)) {
+                PropertyNumberLocalHome home = PropertyNumberHomeFactory.getLocalHome();
+
+                return home.findByPrimaryKey(id).getValue(type);
+            } else if (type == PropertySet.DATE) {
+                PropertyDateLocalHome home = PropertyDateHomeFactory.getLocalHome();
+
+                return home.findByPrimaryKey(id).getValue(type);
+            } else if (type == PropertySet.DOUBLE) {
+                PropertyDecimalLocalHome home = PropertyDecimalHomeFactory.getLocalHome();
+
+                return home.findByPrimaryKey(id).getValue(type);
+            } else if (type == PropertySet.STRING) {
+                PropertyStringLocalHome home = PropertyStringHomeFactory.getLocalHome();
+
+                return home.findByPrimaryKey(id).getValue(type);
+            } else {
+                PropertyDataLocalHome home = PropertyDataHomeFactory.getLocalHome();
+
+                return home.findByPrimaryKey(id).getValue(type);
+            }
+        } catch (Exception e) {
+            logger.warn("Error getting value from PropertySet", e);
+
+            return null;
+        }
+    }
+
+    public void ejbActivate() {
+    }
+
+    /**
+     * @ejb.create-method
+     */
+    public Long ejbCreate(String entityName, long entityId, int type, String key) throws CreateException {
+        Long id = null;
+
+        try {
+            InitialContext ctx = new InitialContext();
+
+            SequenceGeneratorHome genHome = (SequenceGeneratorHome) PortableRemoteObject.narrow(ctx.lookup("java:comp/env/ejb/SequenceGenerator"), SequenceGeneratorHome.class);
+            SequenceGenerator gen = genHome.create();
+            id = new Long(gen.getCount("os.PropertyEntry"));
+
+            setId(id);
+            setEntityName(entityName);
+            setEntityId(entityId);
+            setType(type);
+            setKey(key);
+
+            if ((type == PropertySet.BOOLEAN) || (type == PropertySet.INT) || (type == PropertySet.LONG)) {
+                PropertyNumberLocalHome home = PropertyNumberHomeFactory.getLocalHome();
+                home.create(type, id.longValue());
+            } else if (type == PropertySet.DATE) {
+                PropertyDateLocalHome home = PropertyDateHomeFactory.getLocalHome();
+                home.create(type, id.longValue());
+            } else if (type == PropertySet.DOUBLE) {
+                PropertyDecimalLocalHome home = PropertyDecimalHomeFactory.getLocalHome();
+                home.create(type, id.longValue());
+            } else if (type == PropertySet.STRING) {
+                PropertyStringLocalHome home = PropertyStringHomeFactory.getLocalHome();
+                home.create(type, id.longValue());
+            } else {
+                PropertyDataLocalHome home = PropertyDataHomeFactory.getLocalHome();
+                home.create(type, id.longValue());
+            }
+        } catch (Exception e) {
+            logger.error("Error creating new PropertyEntry", e);
+            throw new CreateException(e.toString());
+        }
+
+        return id;
+    }
+
+    public void ejbLoad() {
+    }
+
+    public void ejbPassivate() {
+    }
+
+    public void ejbPostCreate(String entityName, long entityId, int type, String key) throws CreateException {
+    }
+
+    public void ejbRemove() throws RemoveException {
+        int type = getType();
+        Long id = getId();
+
+        try {
+            InitialContext ctx = new InitialContext();
+
+            if ((type == PropertySet.BOOLEAN) || (type == PropertySet.INT) || (type == PropertySet.LONG)) {
+                PropertyNumberLocalHome home = (PropertyNumberLocalHome) PortableRemoteObject.narrow(ctx.lookup(PropertyNumberLocalHome.COMP_NAME), PropertyNumberLocalHome.class);
+                home.findByPrimaryKey(id).remove();
+            } else if (type == PropertySet.DATE) {
+                PropertyDateLocalHome home = (PropertyDateLocalHome) PortableRemoteObject.narrow(ctx.lookup(PropertyDateLocalHome.COMP_NAME), PropertyDateLocalHome.class);
+                home.findByPrimaryKey(id).remove();
+            } else if (type == PropertySet.DOUBLE) {
+                PropertyDecimalLocalHome home = (PropertyDecimalLocalHome) PortableRemoteObject.narrow(ctx.lookup(PropertyDecimalLocalHome.COMP_NAME), PropertyDecimalLocalHome.class);
+                home.findByPrimaryKey(id).remove();
+            } else if (type == PropertySet.STRING) {
+                PropertyStringLocalHome home = (PropertyStringLocalHome) PortableRemoteObject.narrow(ctx.lookup(PropertyStringLocalHome.COMP_NAME), PropertyStringLocalHome.class);
+                home.findByPrimaryKey(id).remove();
+            } else {
+                PropertyDataLocalHome home = (PropertyDataLocalHome) PortableRemoteObject.narrow(ctx.lookup(PropertyDataLocalHome.COMP_NAME), PropertyDataLocalHome.class);
+                home.findByPrimaryKey(id).remove();
+            }
+        } catch (Exception e) {
+            logger.error("Error removing PropertySet", e);
+        }
+    }
+
+    public void ejbStore() {
+    }
+
+    public void unsetEntityContext() {
+        context = null;
+    }
+}

src/java/com/opensymphony/module/propertyset/ejb/types/StringEntityEJB.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.ejb.types;
+
+
+/* ====================================================================
+ * The OpenSymphony Software License, Version 1.1
+ *
+ * (this license is derived and fully compatible with the Apache Software
+ * License - see http://www.apache.org/LICENSE.txt)
+ *
+ * Copyright (c) 2001 The OpenSymphony Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by the
+ *        OpenSymphony Group (http://www.opensymphony.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "OpenSymphony" and "The OpenSymphony Group"
+ *    must not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written
+ *    permission, please contact license@opensymphony.com .
+ *
+ * 5. Products derived from this software may not be called "OpenSymphony"
+ *    or "OSCore", nor may "OpenSymphony" or "OSCore" appear in their
+ *    name, without prior written permission of the OpenSymphony Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ */
+import com.opensymphony.module.propertyset.PropertyImplementationException;
+import com.opensymphony.module.propertyset.PropertySet;
+
+import javax.ejb.*;
+
+
+/**
+ * AbstractValueEntityEJB concrete implementation optimized for storing short Strings.
+ * This can be used to store STRING.
+ *
+ * @ejb.bean
+ *  type="CMP"
+ *  name="PropertyString"
+ *  schema="PropertyString"
+ *  view-type="local"
+ *  reentrant="False"
+ *  primkey-field="id"
+ *
+ * @ejb.pk class="java.lang.Long" extends="java.lang.Object"
+ *
+ * @ejb.persistence table-name="OS_PROPERTYSTRING"
+ * @ejb.permission unchecked="true"
+ * @ejb.transaction type="Supports"
+ *
+ * @author <a href="mailto:hani@formicary.net">Hani Suleiman</a>
+ * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
+ * @version $Revision$
+ *
+ * @see com.opensymphony.module.propertyset.ejb.PropertyStoreEJB
+ */
+public abstract class StringEntityEJB implements EntityBean {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private EntityContext context;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public abstract void setId(Long id);
+
+    /**
+     * @ejb.pk-field
+     * @ejb.interface-method
+     * @ejb.persistence column-name="id"
+     */
+    public abstract Long getId();
+
+    public abstract void setString(String string);
+
+    /**
+     * @ejb.persistence column-name="value"
+     */
+    public abstract String getString();
+
+    public void setEntityContext(EntityContext context) {
+        this.context = context;
+    }
+
+    /**
+     * Set the value. Only java.lang.String can be supplied.
+     *
+     * @ejb.interface-method
+     */
+    public void setValue(int type, String value) {
+        if (value == null) {
+            setString("");
+
+            return;
+        }
+
+        if (type == PropertySet.STRING) {
+            setString(value);
+        } else {
+            throw new PropertyImplementationException("Cannot store this type of property.");
+        }
+    }
+
+    /**
+     * Return String based value.
+     *
+     * @ejb.interface-method
+     */
+    public String getValue(int type) {
+        if (type == PropertySet.STRING) {
+            return getString();
+        } else {
+            throw new PropertyImplementationException("Cannot retrieve this type of property.");
+        }
+    }
+
+    public void ejbActivate() {
+    }
+
+    /**
+     * @ejb.create-method
+     */
+    public Long ejbCreate(int type, long id) throws CreateException {
+        setId(new Long(id));
+
+        // the concrete setValue() method shall set value to default (not null).
+        setValue(type, null);
+
+        return null;
+    }
+
+    public void ejbLoad() {
+    }
+
+    public void ejbPassivate() {
+    }
+
+    public void ejbPostCreate(int type, long id) throws CreateException {
+    }
+
+    public void ejbRemove() throws RemoveException {
+    }
+
+    public void ejbStore() {
+    }
+
+    public void unsetEntityContext() {
+        context = null;
+    }
+
+    /**
+     * Returns { PropertySet.STRING }
+     */
+    protected int[] allowedTypes() {
+        return new int[] {PropertySet.STRING};
+    }
+}