View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: AbstractMappable.java
13   * Created: 2007-10-23
14   * Author: apohllo
15   * $Id: AbstractMappable.java 2232 2009-01-04 22:59:53Z apohllo $
16   */
17  
18  package pl.edu.agh.cast.model.mapper;
19  
20  import java.io.Serializable;
21  import java.lang.reflect.Method;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import pl.edu.agh.cast.model.mapper.annotation.MapAttribute;
26  import pl.edu.agh.cast.model.mapper.annotation.Mapping;
27  import pl.edu.agh.cast.model.mapper.internal.Helper;
28  
29  /**
30   * Default implementation of the Mappable interface. The implementation should be exactly same in all implementing
31   * classes. Custom implementation is needed only if class implementing Mappable cannot extend AbstractMappable.
32   *
33   * @author AGH CAST Team
34   *
35   */
36  @Mapping()
37  public abstract class AbstractMappable implements Mappable, Serializable, Cloneable {
38  
39  	private static final long serialVersionUID = 8223254560632380336L;
40  
41  	/**
42  	 * The base model id.
43  	 */
44  	private long __id = 0;
45  
46  	/**
47  	 * The general id of the mappable - this value is viewable by users and is designed for the data identification.
48  	 * E.g. for phone number the id would be the value of the phone number (i.e. "123-456-789").
49  	 */
50  	protected String id = ""; //$NON-NLS-1$
51  
52  	/**
53  	 *
54  	 * {@inheritDoc}
55  	 *
56  	 * @see pl.edu.agh.cast.model.mapper.Mappable#getId()
57  	 */
58  	@MapAttribute(typeName = "mappableId")
59  	public String getId() {
60  		return id;
61  	}
62  
63  	/**
64  	 *
65  	 * {@inheritDoc}
66  	 *
67  	 * @see pl.edu.agh.cast.model.mapper.Mappable#setId(java.lang.String)
68  	 */
69  	public void setId(String id) {
70  		this.id = id;
71  	}
72  
73  	/**
74  	 *
75  	 * {@inheritDoc}
76  	 *
77  	 * @see pl.edu.agh.cast.model.mapper.Mappable#getMid()
78  	 */
79  	public final long getMid() {
80  		return __id;
81  	}
82  
83  	/**
84  	 *
85  	 * {@inheritDoc}
86  	 *
87  	 * @see pl.edu.agh.cast.model.mapper.Mappable#setMid(long)
88  	 */
89  	public final void setMid(long mid) {
90  		__id = mid;
91  	}
92  
93  	/**
94  	 * Get value of attribute with given name.
95  	 *
96  	 * @param name
97  	 *            the name of the attribute
98  	 * @return the value of the attribute
99  	 */
100 	public final Object getAttribute(String name) {
101 		Class klass = getClass();
102 		try {
103 			Method getter = klass.getMethod("get" + name, new Class[0]); //$NON-NLS-1$
104 			return getter.invoke(this, new Object[0]);
105 		} catch (Exception e) {
106 			// TODO warn to logger not err!
107 			System.err.println("Warning getAttribute: " + e); //$NON-NLS-1$
108 			// e.printStackTrace();
109 		}
110 		return null;
111 	}
112 
113 	/**
114 	 *
115 	 * {@inheritDoc}
116 	 *
117 	 * @see pl.edu.agh.cast.model.mapper.Mappable#setAttribute(java.lang.String, java.lang.Object)
118 	 */
119 	public final void setAttribute(String name, Object value) {
120 		Class klass = getClass();
121 		try {
122 			Method getter = klass.getMethod("set" + name, new Class[] { value //$NON-NLS-1$
123 			        .getClass() });
124 			getter.invoke(this, new Object[] { value });
125 		} catch (Exception e) {
126 			System.err.println("Warning setAttribute: " + e); //$NON-NLS-1$
127 			// TODO warn to logger
128 			throw new IllegalArgumentException("Problem occured while executing set" + //$NON-NLS-1$
129 			        name + " method", e); //$NON-NLS-1$
130 		}
131 	}
132 
133 	/**
134 	 *
135 	 * {@inheritDoc}
136 	 *
137 	 * @see pl.edu.agh.cast.model.mapper.Mappable#getAttributeType(java.lang.String)
138 	 */
139 	public final Class getAttributeType(String name) {
140 		// TODO implement get attribute class
141 		return null;
142 	}
143 
144 	/**
145 	 * Return all mapped attributes names.
146 	 *
147 	 * @return Array of attribute names
148 	 */
149 	public final String[] getAttributeNames() {
150 		Set<String> result = new HashSet<String>();
151 		getAttributeNames(result, getClass());
152 		return result.toArray(new String[0]);
153 	}
154 
155 	private void getAttributeNames(Set<String> result, Class klass) {
156 		for (Method method : Helper.mappedAttributeMethods(klass)) {
157 			result.add(method.getName().substring(3)); // remove get from the
158 			// method name
159 		}
160 		if (Helper.isSuperMapped(klass)) {
161 			getAttributeNames(result, klass.getSuperclass());
162 		}
163 	}
164 
165 	/**
166 	 *
167 	 * {@inheritDoc}
168 	 *
169 	 * @see pl.edu.agh.cast.model.mapper.Mappable#getType()
170 	 */
171 	public String getType() {
172 		return Helper.mappedTypeName(getClass());
173 	}
174 
175 	/**
176 	 *
177 	 * {@inheritDoc}
178 	 *
179 	 * @see java.lang.Object#toString()
180 	 */
181 	@Override
182 	public String toString() {
183 		return getType() + "[" + getId() + "]"; //$NON-NLS-1$//$NON-NLS-2$
184 	}
185 
186 	/**
187 	 * This method makes a shallow copy of the object. Subclasses may override, to break the referential integrity.
188 	 *
189 	 * @return copy of the mappable
190 	 */
191 	@Override
192 	public AbstractMappable clone() {
193 		try {
194 			return (AbstractMappable)super.clone();
195 		} catch (CloneNotSupportedException e) {
196 			// this cannot happen since AbstracMappable implements the cloneable interface
197 			e.printStackTrace();
198 			return null;
199 		}
200 	}
201 }