DomainObject.java :  » Project-Management » XPlanner-0.7b7 » com » technoetic » xplanner » domain » Java Open Source

Java Open Source » Project Management » XPlanner 0.7b7 
XPlanner 0.7b7 » com » technoetic » xplanner » domain » DomainObject.java
package com.technoetic.xplanner.domain;

import java.util.*;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;

public abstract class DomainObject implements Identifiable, Nameable
{
   private Date lastUpdateTime;
   private int id;
   private Map attributes;

   public Date getLastUpdateTime()
   {
      return lastUpdateTime;
   }

   public void setLastUpdateTime(Date lastUpdateTime)
   {
      this.lastUpdateTime = lastUpdateTime;
   }

   public int getId()
   {
      return id;
   }

   public void setId(int id)
   {
      this.id = id;
   }

   public boolean equals(Object o)
   {
      if (this == o) return true;
      if (!(o instanceof DomainObject)) return false;

      final DomainObject object = (DomainObject) o;

      if (id == 0) return this == o;
      if (id != object.id) return false;

      return true;
   }

   public int hashCode()
   {
      return id;
   }

   public String getAttribute(String name)
   {
      if (attributes != null)
      {
         return (String) attributes.get(name);
      }
      else
      {
         attributes = getAttributes();
         if (attributes != null)
         {
            return (String) attributes.get(name);
         }
         else
         {
            return null;
         }
      }
   }

   public Map getAttributes()
   {
      return attributes != null ? Collections.unmodifiableMap(attributes) : null;
   }

   protected void setAttributes(Map attributes)
   {
      this.attributes = attributes;
   }

   protected static String getValidProperty(Class beanClass, String property) {
      BeanInfo beanInfo;
      try {
         beanInfo = Introspector.getBeanInfo(beanClass);
      } catch (IntrospectionException e) {
         throw new RuntimeException("could not introspect " + beanClass, e);
      }
      PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
      boolean found = false;
      for (int i = 0; i < properties.length; i++) {
         if (properties[i].getName().equals(property)) {
            found = true;
            break;
         }
      }
      if (!found) {
         throw new RuntimeException("Could not find property " + property + " in " + beanClass);
      }

      return property;

   }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.