0 Replies Latest reply on Mar 3, 2005 12:36 PM by czernay

    ValueObject creation problem

    czernay Newbie

      I have set up two CMP beans AuthorBean and ImageBean that have a 1:1 relation via AuthorBean.getImage() that returns an ImageBeanLocal object. That works perfectly well.

      Now I want to integrate this into a Struts application. Just a simple HTML-form, reading name, description and the image as a byte[]. With Struts I could use a simple ValueObject, with fields for String name, String description and byte[] image, to transfer the data to the AuthorBean. The obvious problem is, that AuthorBean's image field is an ImageBeanLocal not byte[].

      So I tried two solutions:

      1) I set up AuthorBean.setImageContent(byte[]) and byte[] AuthorBean.getImageContent() methods with actual implementations for fetching the related ImageBean and returning/setting it's data. That might work, but if I try to include those methods into the value-object, I get "getImageContent()/setImageContent is undefined" errors in the XDoclet-created AuthorBeanData class.

      2) I created a value object "by hand", that is derived from as simpler generated value object, that only held the name and description fields. That looks like an ugly solution to me.

      Is there any "right" way to handle this situation? Do I have a completly wrong approach? Any help?

      Thanks a lot for your help,
      Alexander

      AuthorBean.java
      ===============
      /*
      * Created on 09.08.2004
      *
      */
      package de.sbow.MSys.Authors.ejb;

      //import java.rmi.RemoteException;
      import java.util.Collection;

      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;
      import javax.ejb.RemoveException;
      import javax.naming.NamingException;

      import de.sbow.Exceptions.ApplicationException;
      import de.sbow.MSys.Authors.interfaces.AuthorBeanUtil;
      import de.sbow.MSys.Authors.value.AuthorBeanSpecialFullValue;
      import de.sbow.MSys.Authors.value.AuthorBeanSpecialValue;
      import de.sbow.MSys.Authors.value.AuthorBeanValue;
      import de.sbow.MSys.MediaArchive.interfaces.ImageBeanLocal;
      import de.sbow.MSys.MediaArchive.interfaces.MediaArchiveFacadeBeanLocal;
      import de.sbow.MSys.MediaArchive.interfaces.MediaArchiveFacadeBeanLocalHome;
      import de.sbow.MSys.MediaArchive.interfaces.MediaArchiveFacadeBeanUtil;


      /**
      * @author Alexander
      *
      * @ejb.bean name = "AuthorBean"
      * type = "CMP"
      * cmp-version = "2.x"
      * display-name = "AuthorBean"
      * description = "AuthorBean EJB"
      * view-type = "local"
      * jndi-name = "ejb/AuthorHome"
      * local-jndi-name = "ejb/AuthorLocalHome"
      * primkey-field = "id"
      * schema = "Authors"
      *
      * @ejb.persistence table-name = "Authors"
      * @jboss.persistence table-name = "Authors"
      *
      *
      * @ejb.finder signature = "java.util.Collection findAll()"
      * query = "select Object(l) from Authors as l"
      *
      * @ejb.finder signature = "java.util.Collection findByName(java.lang.String name)"
      * query = "select Object(l) from Authors as l where l.name = ?1"
      *
      * @ejb.finder signature = "de.sbow.MSys.Authors.interfaces.AuthorBeanLocal findById(java.lang.String id)"
      * query = "select Object(l) from Authors as l where l.id = ?1"
      *
      *
      * @ejb.util
      * generate="physical"
      *
      * @ejb.value-object
      * name = "AuthorBeanSpecial"
      * match = "special"
      *
      * @ejb.value-object
      * match = "*"
      */
      public abstract class AuthorBean implements EntityBean {

      /** The EntityContext */
      private EntityContext context;

      /**
      * @throws CreateException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      *
      * @ejb.create-method
      */
      public String ejbCreate() throws CreateException {
      this.setId(AuthorBeanUtil.generateGUID(this));
      return null;
      }

      /**
      * @ejb.create-method
      * @param name
      * @param description
      * @param image a byte-array containing the author's image
      * @return
      */
      public String ejbCreate(String name, String description, byte[] image) throws CreateException {
      this.setName(name);
      this.setDescription(description);
      this.setId(AuthorBeanUtil.generateGUID(this));
      return null;
      }

      public void ejbPostCreate(String name, String description, byte[] image) throws CreateException {
      this.setImageContent(image);
      }

      /**
      * @ejb.create-method
      * @param name
      * @param description
      * @return
      */
      public String ejbCreate(String name, String description) throws CreateException {
      this.setName(name);
      this.setDescription(description);
      this.setId(AuthorBeanUtil.generateGUID(this));
      return null;
      }

      public void ejbPostCreate(String name, String description) throws CreateException {
      }

      /**
      * TODO check if this works!
      * @ejb.create-method
      * @param data
      * @return
      * @throws CreateException
      */
      public String ejbCreate(AuthorBeanValue data) throws CreateException {
      this.setId(AuthorBeanUtil.generateGUID(this));
      //data.setId(this.getId());
      this.setAuthorBeanValue(data);
      return null;
      }

      public void ejbPostCreate(AuthorBeanValue data) throws CreateException {
      }

      /**
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbActivate() throws EJBException {
      }

      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbPassivate() throws EJBException {
      }

      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbLoad() throws EJBException {
      }
      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbStore() throws EJBException {
      }
      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      *
      * @throws RemoveException Thrown if the enterprise bean does not allow destruction of the object.
      */
      public void ejbRemove() throws EJBException, RemoveException {
      }

      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void setEntityContext(EntityContext newContext) throws EJBException {
      context = newContext;
      }

      /**
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void unsetEntityContext() throws EJBException {
      context = null;
      }



      /**
      * @ejb.interface-method view-type = "local"
      * @ejb.persistence column-name = "author_id"
      * @ejb.pk-field
      * @ejb.value-object match = "special"
      * @return
      */
      public abstract String getId();

      /**
      * @ejb.interface-method view-type = "local"
      * @param id
      */
      public abstract void setId(String id);

      /**
      * @ejb.interface-method view-type = "local"
      * @ejb.persistence column-name = "name"
      * @ejb.value-object match = "special"
      * @return
      */
      public abstract String getName();

      /**
      * @ejb.interface-method view-type = "local"
      *
      * @param name
      */
      public abstract void setName(String name);

      /**
      * @ejb.interface-method view-type = "local"
      * @ejb.persistence column-name = "description"
      * @ejb.value-object match = "special"
      * @return
      */
      public abstract String getDescription();

      /**
      * @ejb.interface-method view-type = "local"
      *
      * @param description
      */
      public abstract void setDescription(String description);


      /**
      * Get this author's image.
      *
      * @ejb.relation name = "Author-OwnImage"
      * role-name = "Author-has-OwnImage"
      * cascade-delete = "yes"
      * target-ejb = "ImageBean"
      * target-role-name = "OwnImage-has-Author"
      * target-multiple = "no"
      *
      * @jboss.relation
      * related-pk-field="id"
      * fk-column="image_fk"
      *
      *
      * @ejb.interface-method view-type = "local"
      *
      * @ejb.value-object
      * compose="de.sbow.MSys.MediaArchive.value.ImageBeanValue"
      * compose-name="ImageBeanValue"
      * members="de.sbow.MSys.MediaArchive.interfaces.ImageBeanLocal"
      * members-name="Image"
      * relation="external"
      *
      * @return
      */
      public abstract ImageBeanLocal getImage();

      /**
      * Set this author's image.
      *
      * @ejb.interface-method view-type="local"
      * @ejb.value-object
      *
      * @param image
      */
      public abstract void setImage(ImageBeanLocal image);

      /**
      * @ejb.interface-method view-type="local"
      * @ejb.value-object
      * @return
      */
      public byte[] getImageContent() {
      if (getImage() == null)
      return null;
      else {
      try {
      MediaArchiveFacadeBeanLocalHome mediaHome = MediaArchiveFacadeBeanUtil.getLocalHome();
      MediaArchiveFacadeBeanLocal media = mediaHome.create();

      return media.getImage(getImage());
      }
      catch (CreateException e) {
      throw new ApplicationException("Create Exception: ", e);
      }
      catch (NamingException e) {
      throw new ApplicationException("Naming Exception: ", e);
      }

      }
      }

      /**
      *
      * @ejb.interface-method view-type="local"
      *
      * @param content
      */
      public void setImageContent(byte[] content) {
      try {
      MediaArchiveFacadeBeanLocalHome mediaHome = MediaArchiveFacadeBeanUtil.getLocalHome();
      MediaArchiveFacadeBeanLocal media = mediaHome.create();

      // check if we already have an attached image
      if (this.getImage() != null) {
      // we have an attached image, just alter it's content
      media.alterImage(getImage().getId(), content);
      } else {
      // we have to create a new image
      setImage(media.createLocalImage(getName(), content));
      }
      }
      catch (CreateException e) {
      throw new ApplicationException("Create Exception: ", e);
      }
      catch (NamingException e) {
      throw new ApplicationException("Naming Exception: ", e);
      }
      }

      /**
      * Get images owned by this author
      * (this is not the author's image -> see getImage())
      *
      * @ejb.relation name = "Images-Authors"
      * role-name = "Author-has-Images"
      *
      * @jboss.relation related-pk-field = "id"
      * fk-column = "image_id"
      *
      * @return
      */
      public abstract Collection getImages();


      /**
      * Set images owned by this author
      * (this is not the author's image -> see setImage())
      *
      * @ejb.interface-method
      * @param images
      *
      */
      public abstract void setImages(Collection images);


      /**
      * @ejb.interface-method view-type = "local"
      *
      * @return
      */
      public abstract AuthorBeanValue getAuthorBeanValue();

      /**
      * @ejb.interface-method view-type = "local"
      * @param data
      */
      public abstract void setAuthorBeanValue(AuthorBeanValue data);

      /**
      * @ejb.interface-method view-type = "local"
      *
      * @return
      */
      public abstract AuthorBeanSpecialValue getAuthorBeanSpecialValue();

      /**
      * @ejb.interface-method view-type = "local"
      * @param data
      */
      public abstract void setAuthorBeanSpecialValue(AuthorBeanSpecialValue data);

      /**
      * @ejb.interface-method view-type = "local"
      *
      * @return
      */
      public abstract AuthorBeanSpecialFullValue getAuthorBeanSpecialFullValue();

      /**
      * @ejb.interface-method view-type = "local"
      * @param data
      */
      public abstract void setAuthorBeanSpecialFullValue(AuthorBeanSpecialFullValue data);

      }

      ImageBean.java
      ==============

      /*
      * Created on 06.08.2004
      *
      */
      package de.sbow.MSys.MediaArchive.ejb;


      //import java.rmi.RemoteException;
      import java.util.Collection;

      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;
      import javax.ejb.RemoveException;

      import de.sbow.MSys.Authors.interfaces.AuthorBeanLocal;
      import de.sbow.MSys.Categories.interfaces.CategoryBeanLocal;
      import de.sbow.MSys.MediaArchive.interfaces.ImageBeanUtil;
      import de.sbow.MSys.MediaArchive.interfaces.ImageVariantBeanLocal;
      import de.sbow.MSys.MediaArchive.value.ImageBeanValue;

      /**
      * @author Alexander
      *
      */

      /**
      *
      * @ejb.bean name = "ImageBean"
      * type = "CMP"
      * cmp-version = "2.x"
      * display-name = "ImageBean"
      * description = "ImageBean EJB"
      * view-type = "local"
      * jndi-name = "ejb/ImageBeanHome"
      * local-jndi-name = "ejb/ImageBeanLocalHome"
      * primkey-field = "id"
      * schema = "Images"
      *
      * @ejb.persistence table-name = "Images"
      * @jboss.persistence table-name = "Images"
      * post-table-create = "ALTER TABLE Images ADD type=InnoDB"
      *
      * @ejb.finder signature="java.util.Collection findAll()"
      * query="select object(i) from Images i"
      *
      * @ejb.select
      * query="select distinct i.id from Images i order by i.name"
      *
      * @ejb.util
      * generate="physical"
      *
      * @ejb.value-object
      * mask="*"
      *
      * @ejb.value-object
      * name = "ImageBeanLight"
      * mask = "light"
      *
      */
      public abstract class ImageBean implements EntityBean {

      /** The EntityContext */
      private EntityContext context;

      /**
      * @throws CreateException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      *
      * @ejb.create-method
      */
      public String ejbCreate(String name, ImageVariantBeanLocal subObject) throws CreateException {
      this.setId(ImageBeanUtil.generateGUID(this));
      this.setName(name);
      return null;
      }

      /**
      * @throws CreateException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbPostCreate(String name, ImageVariantBeanLocal subObject) throws CreateException {
      this.addVariant(subObject);
      }

      /**
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbActivate() throws EJBException {
      }

      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbPassivate() throws EJBException {
      }

      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbLoad() throws EJBException {
      }
      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void ejbStore() throws EJBException {
      }
      /**
      *
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      *
      * @throws RemoveException Thrown if the enterprise bean does not allow destruction of the object.
      */
      public void ejbRemove() throws EJBException, RemoveException {
      }

      /**
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void setEntityContext(EntityContext newContext) throws EJBException {
      context = newContext;
      }

      /**
      * @throws EJBException Thrown if the instance could not perform
      * the function requested by the container because of an system-level error.
      */
      public void unsetEntityContext() throws EJBException {
      context = null;
      }

      /**
      * @ejb.interface-method view-type = "local"
      * @ejb.persistence column-name = "image_id"
      * @ejb.pk-field
      * @ejb.value-object mask="light"
      * @return
      */
      public abstract String getId();

      /**
      * @ejb.interface-method view-type = "local"
      * @param id
      */
      public abstract void setId(String id);

      /**
      * @ejb.interface-method view-type = "local"
      * @ejb.persistence column-name = "name"
      * @ejb.value-object mask="light"
      * @return
      */
      public abstract String getName();

      /**
      * @ejb.interface-method view-type = "local"
      *
      * @param name
      */
      public abstract void setName(String name);


      /**
      * @ejb.interface-method
      *
      * @ejb.relation name = "Images-Categories"
      * role-name = "Image-has-Categories"
      * cascade-delete = "false"
      * target-ejb = "CategoryBean"
      * target-role = "Category-has-Images"
      * target-multiple = "yes"
      *
      * @jboss.relation-mapping style = "relation-table"
      *
      * @jboss.relation-table table-name = "Image2Category"
      *
      * @jboss.relation related-pk-field = "id"
      * fk-column = "image_id"
      *
      * @return
      */
      public abstract Collection getCategories();

      /**
      * @ejb.interface-method
      * @param categories
      *
      */
      public abstract void setCategories(Collection categories);

      /**
      * Add a new category to the media object
      *
      * @ejb.interface-method
      * @param newCategory
      */
      public void addCategory(CategoryBeanLocal newCategory) {
      Collection categories = getCategories();
      categories.add(newCategory);
      }

      /**
      * @ejb.interface-method
      *
      * @ejb.relation name = "Images-Authors"
      * role-name = "Image-has-Authors"
      *
      * @jboss.relation-table table-name = "Image2Author"
      *
      * @jboss.relation related-pk-field = "id"
      * fk-column = "author_id"
      * fk-constraint = "no"
      *
      * @return
      */
      public abstract Collection getAuthors();

      /**
      * @ejb.interface-method
      * @param authors
      *
      */
      public abstract void setAuthors(Collection authors);

      /**
      * Add a new author to the media object
      *
      * @ejb.interface-method
      * @param newAuthor
      */
      public void addAuthor(AuthorBeanLocal newAuthor) {
      Collection authors = getAuthors();
      authors.add(newAuthor);
      }

      /**
      * @ejb.interface-method
      *
      * @ejb.relation name = "ImageBean-Variants"
      * role-name = "Image-has-ImageVariants"
      * cascade-delete = "no"
      * target-ejb = "ImageVariantBean"
      * target-role-name = "ImageVariant-has-ImageBean"
      * target-multiple = "no"
      * target-cascade-delete = "yes"
      *
      * @jboss.target-relation related-pk-field = "id"
      * fk-column = "image_id"
      *
      * @ejb.value-object
      * compose = "de.sbow.MSys.MediaArchive.value.ImageVariantBeanValue"
      * compose-name = "Variant"
      * members = "de.sbow.MSys.MediaArchive.interfaces.ImageVariantBeanLocal"
      * members-name = "Variants"
      * relation = "external"
      * type = "java.util.Collection"
      *
      * @return
      */
      public abstract Collection getVariants();

      /**
      * @ejb.interface-method
      * @param variants
      *
      */
      public abstract void setVariants(Collection variants);

      /**
      * Add a new variant to the media object
      *
      * @ejb.interface-method
      *
      * @param newVariant
      */
      public void addVariant(ImageVariantBeanLocal newVariant) {
      Collection variants = getVariants();
      variants.add(newVariant);
      setVariants(variants);
      }

      /**
      * Remove a variant from the image object
      *
      * @ejb.interface-method
      *
      * @param variant
      */
      public void removeVariant(ImageVariantBeanLocal variant) {
      Collection variants = getVariants();
      variants.remove(variant);
      setVariants(variants);
      }

      /**
      * @ejb.interface-method
      *
      * @return
      */
      public abstract ImageBeanValue getImageBeanValue();

      /**
      * @ejb.interface-method
      * @param value
      */
      public abstract void setImageBeanValue(ImageBeanValue value);
      }