1
package org.hisp.dhis.common;
4
* Copyright (c) 2004-2012, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import com.fasterxml.jackson.annotation.JsonIgnore;
31
import com.fasterxml.jackson.annotation.JsonProperty;
32
import com.fasterxml.jackson.annotation.JsonView;
33
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
34
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
35
import org.apache.commons.lang.Validate;
36
import org.hisp.dhis.common.view.BasicView;
37
import org.hisp.dhis.common.view.DetailedView;
38
import org.hisp.dhis.common.view.ExportView;
40
import java.util.Collection;
41
import java.util.Date;
42
import java.util.HashMap;
46
* @author Bob Jolliffe
48
@JacksonXmlRootElement( localName = "identifiableObject", namespace = Dxf2Namespace.NAMESPACE )
49
public class BaseIdentifiableObject
50
extends BaseLinkableObject
51
implements IdentifiableObject
54
* Determines if a de-serialized file is compatible with this class.
56
private static final long serialVersionUID = 5532508099213570673L;
59
* The database internal identifier for this Object.
64
* The Unique Identifier for this Object.
69
* The unique code for this Object.
71
protected String code;
74
* The name of this Object. Required and unique.
76
protected String name;
79
* The date this object was last updated.
81
protected Date lastUpdated;
84
* The i18n variant of the name. Should not be persisted.
86
protected transient String displayName;
88
// -------------------------------------------------------------------------
90
// -------------------------------------------------------------------------
92
public BaseIdentifiableObject()
96
public BaseIdentifiableObject( int id, String uid, String name )
103
public BaseIdentifiableObject( IdentifiableObject identifiableObject )
105
this.id = identifiableObject.getId();
106
this.uid = identifiableObject.getUid();
107
this.name = identifiableObject.getName();
108
this.lastUpdated = identifiableObject.getLastUpdated();
111
// -------------------------------------------------------------------------
112
// Comparable implementation
113
// -------------------------------------------------------------------------
116
public int compareTo( IdentifiableObject object )
118
return name == null ? (object.getName() == null ? 0 : -1) : name.compareTo( object.getName() );
121
// -------------------------------------------------------------------------
122
// Setters and getters
123
// -------------------------------------------------------------------------
131
public void setId( int id )
136
@JsonProperty( value = "id" )
137
@JacksonXmlProperty( isAttribute = true )
138
public String getUid()
143
public void setUid( String uid )
149
@JsonView( {DetailedView.class, BasicView.class, ExportView.class} )
150
@JacksonXmlProperty( isAttribute = true )
151
public String getCode()
156
public void setCode( String code )
162
@JsonView( {DetailedView.class, BasicView.class, ExportView.class} )
163
@JacksonXmlProperty( isAttribute = true )
164
public String getName()
169
public void setName( String name )
175
@JsonView( {DetailedView.class, BasicView.class, ExportView.class} )
176
@JacksonXmlProperty( isAttribute = true )
177
public Date getLastUpdated()
182
public void setLastUpdated( Date lastUpdated )
184
this.lastUpdated = lastUpdated;
187
public String getDisplayName()
189
return displayName != null && !displayName.trim().isEmpty() ? displayName : getName();
192
public void setDisplayName( String displayName )
194
this.displayName = displayName;
198
public boolean equals( Object o )
200
if ( this == o ) return true;
201
if ( o == null || getClass() != o.getClass() ) return false;
203
BaseIdentifiableObject that = (BaseIdentifiableObject) o;
205
if ( code != null ? !code.equals( that.code ) : that.code != null ) return false;
206
if ( name != null ? !name.equals( that.name ) : that.name != null ) return false;
207
if ( uid != null ? !uid.equals( that.uid ) : that.uid != null ) return false;
213
public int hashCode()
215
int result = uid != null ? uid.hashCode() : 0;
216
result = 31 * result + (code != null ? code.hashCode() : 0);
217
result = 31 * result + (name != null ? name.hashCode() : 0);
218
result = 31 * result + (lastUpdated != null ? lastUpdated.hashCode() : 0);
223
// -------------------------------------------------------------------------
225
// -------------------------------------------------------------------------
228
* Set auto-generated fields on save or update
230
public void setAutoFields()
232
if ( uid == null || uid.length() == 0 )
234
setUid( CodeGenerator.generateCode() );
237
setLastUpdated( new Date() );
241
* Get a map of uids to internal identifiers
243
* @param objects the IdentifiableObjects to put in the map
246
public static Map<String, Integer> getUIDMap( Collection<? extends BaseIdentifiableObject> objects )
248
Map<String, Integer> map = new HashMap<String, Integer>();
250
for ( IdentifiableObject object : objects )
252
String uid = object.getUid();
253
int internalId = object.getId();
255
map.put( uid, internalId );
262
* Get a map of codes to internal identifiers
264
* @param objects the NameableObjects to put in the map
267
public static Map<String, Integer> getCodeMap( Collection<? extends BaseIdentifiableObject> objects )
269
Map<String, Integer> map = new HashMap<String, Integer>();
270
for ( BaseIdentifiableObject object : objects )
272
String code = object.getCode();
273
int internalId = object.getId();
275
map.put( code, internalId );
281
* Get a map of names to internal identifiers
283
* @param objects the NameableObjects to put in the map
286
public static Map<String, Integer> getNameMap( Collection<? extends BaseIdentifiableObject> objects )
288
Map<String, Integer> map = new HashMap<String, Integer>();
289
for ( BaseIdentifiableObject object : objects )
291
String name = object.getName();
292
int internalId = object.getId();
294
map.put( name, internalId );
300
public String toString()
302
return "{" + "id=" + id + ", uid='" + uid + '\'' + ", code='" +
303
code + '\'' + ", name='" + name + '\'' + ", lastUpdated=" + lastUpdated + "}";
307
public void mergeWith( IdentifiableObject other )
309
Validate.notNull( other );
311
this.uid = other.getUid() == null ? this.uid : other.getUid();
312
this.name = other.getName() == null ? this.name : other.getName();
313
this.code = other.getCode() == null ? this.code : other.getCode();
314
this.lastUpdated = other.getLastUpdated() == null ? this.lastUpdated : other.getLastUpdated();