View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.persistence.hibernate.dao.extent;
18  
19  import java.io.Serializable;
20  import java.lang.reflect.Method;
21  
22  import ch.elca.el4j.util.codingsupport.BeanPropertyUtils;
23  
24  /**
25   * An ExtentPart is the abstract super Class of Extent parts like ExtentEntity or ExtentCollection.
26   *
27   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/hibernate/src/main/java/ch/elca/el4j/services/persistence/hibernate/dao/extent/AbstractExtentPart.java $
28   *
29   * @author Andreas Rueedlinger (ARR)
30   */
31  public abstract class AbstractExtentPart implements Serializable, Comparable<AbstractExtentPart> {
32  	/** The name of the extent-part. */
33  	protected String m_name;
34  	
35  	/** The parent of the entity, null if root. */
36  	protected AbstractExtentPart m_parent;
37  	
38  	/**
39  	 * Name of the extent-part.
40  	 * @return the name of the extent-part.
41  	 */
42  	public String getName() {
43  		return m_name;
44  	}
45  	
46  	/**
47  	 * Return the parent entity of the current extent part,
48  	 * null if root or contained in a collection.
49  	 * @return the parent.
50  	 */
51  	public AbstractExtentPart getParent() {
52  		return m_parent;
53  	}
54  	
55  	/**
56  	 * @return the id of the extent part.
57  	 */
58  	public abstract String getId();
59  	
60  	/**
61  	 * Updates the id of the extent part.
62  	 * Should be used be children to inform its parent.
63  	 */
64  	protected abstract void updateId();
65  	
66  	/** {@inheritDoc} */
67  	public int compareTo(AbstractExtentPart other) {
68  		if (m_name != null) {
69  			return m_name.compareTo(other.m_name);
70  		} else if (other.m_name == null) {
71  			return 0;
72  		} else {
73  			return -1;
74  		}
75  	}
76  	
77  	/**
78  	 * Sets the parent of the extent-part.
79  	 * @param parent	the parent to set.
80  	 */
81  	protected void setParent(AbstractExtentPart parent) {
82  		m_parent = parent;
83  	}
84  	/**
85  	 * Method to get the extent-part, null if root entity,
86  	 * otherwise set latest when added as child.
87  	 * @return the method to get the extent-part.
88  	 * @throws NoSuchMethodException 
89  	 */
90  	public Method getMethod() throws SecurityException, NoSuchMethodException {
91  		if (m_parent instanceof ExtentCollection) {
92  			return null;
93  		} else {
94  			return BeanPropertyUtils.getReadMethod(((ExtentEntity) m_parent).getEntityClass(), getName());
95  		}
96  	}
97  	
98  
99  	/** {@inheritDoc} */
100 	@Override
101 	public int hashCode() {
102 		return getId().hashCode();
103 	}
104 	
105 	/** {@inheritDoc} */
106 	@Override
107 	public boolean equals(Object object) {
108 		if (super.equals(object)) {
109 			return true;
110 		} else if (object instanceof AbstractExtentPart) {
111 			return getId().equals(((AbstractExtentPart) object).getId());
112 		} else {
113 			return false;
114 		}
115 	}
116 
117 	/** {@inheritDoc} */
118 	@Override
119 	public String toString() {
120 		return getId();
121 	}
122 	
123 	/**
124 	 * @return the old native implementation of the toString method.
125 	 */
126 	protected String nativeToString() {
127 		return super.toString();
128 	}
129 	
130 	/* Helper Functions */
131 	
132 	/**
133 	 * Helper function to convert a string from Getter-Method-name
134 	 * to field name.
135 	 * @param m		the method to get the field name of.
136 	 * @return field name
137 	 */
138 	protected String toFieldName(Method m) {
139 		String str = m.getName();
140 		if (m.getReturnType().equals(boolean.class)) {
141 			return firstCharLower(str.substring(2));
142 		} else {
143 			return firstCharLower(str.substring(3));
144 		}
145 		
146 	}
147 	
148 	/**
149 	 * Helper function to set first Character lower case.
150 	 * @param str	string to be changed
151 	 * @return changed string
152 	 */
153 	protected String firstCharLower(String str) {
154 		return str.substring(0, 1).toLowerCase() + str.substring(1);
155 	}
156 	
157 	/**
158 	 * Helper function to set first Character upper case.
159 	 * @param str	string to be changed
160 	 * @return changed string
161 	 */
162 	protected String firstCharUpper(String str) {
163 		return str.substring(0, 1).toUpperCase() + str.substring(1);
164 	}
165 }