~registry/dhis2-academy/trunk

« back to all changes in this revision

Viewing changes to dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java

  • Committer: Abyot Asalefew Gizaw
  • Date: 2012-10-12 00:08:27 UTC
  • Revision ID: abyota@gmail.com-20121012000827-qqmmfllx0s5vtkqi
Updating the acadmy trunk with the main DHIS2 trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.common;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2012, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
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;
 
39
 
 
40
import java.util.Collection;
 
41
import java.util.Date;
 
42
import java.util.HashMap;
 
43
import java.util.Map;
 
44
 
 
45
/**
 
46
 * @author Bob Jolliffe
 
47
 */
 
48
@JacksonXmlRootElement( localName = "identifiableObject", namespace = Dxf2Namespace.NAMESPACE )
 
49
public class BaseIdentifiableObject
 
50
    extends BaseLinkableObject
 
51
    implements IdentifiableObject
 
52
{
 
53
    /**
 
54
     * Determines if a de-serialized file is compatible with this class.
 
55
     */
 
56
    private static final long serialVersionUID = 5532508099213570673L;
 
57
 
 
58
    /**
 
59
     * The database internal identifier for this Object.
 
60
     */
 
61
    protected int id;
 
62
 
 
63
    /**
 
64
     * The Unique Identifier for this Object.
 
65
     */
 
66
    protected String uid;
 
67
 
 
68
    /**
 
69
     * The unique code for this Object.
 
70
     */
 
71
    protected String code;
 
72
 
 
73
    /**
 
74
     * The name of this Object. Required and unique.
 
75
     */
 
76
    protected String name;
 
77
 
 
78
    /**
 
79
     * The date this object was last updated.
 
80
     */
 
81
    protected Date lastUpdated;
 
82
 
 
83
    /**
 
84
     * The i18n variant of the name. Should not be persisted.
 
85
     */
 
86
    protected transient String displayName;
 
87
 
 
88
    // -------------------------------------------------------------------------
 
89
    // Constructors
 
90
    // -------------------------------------------------------------------------
 
91
 
 
92
    public BaseIdentifiableObject()
 
93
    {
 
94
    }
 
95
 
 
96
    public BaseIdentifiableObject( int id, String uid, String name )
 
97
    {
 
98
        this.id = id;
 
99
        this.uid = uid;
 
100
        this.name = name;
 
101
    }
 
102
 
 
103
    public BaseIdentifiableObject( IdentifiableObject identifiableObject )
 
104
    {
 
105
        this.id = identifiableObject.getId();
 
106
        this.uid = identifiableObject.getUid();
 
107
        this.name = identifiableObject.getName();
 
108
        this.lastUpdated = identifiableObject.getLastUpdated();
 
109
    }
 
110
 
 
111
    // -------------------------------------------------------------------------
 
112
    // Comparable implementation
 
113
    // -------------------------------------------------------------------------
 
114
 
 
115
    @Override
 
116
    public int compareTo( IdentifiableObject object )
 
117
    {
 
118
        return name == null ? (object.getName() == null ? 0 : -1) : name.compareTo( object.getName() );
 
119
    }
 
120
 
 
121
    // -------------------------------------------------------------------------
 
122
    // Setters and getters
 
123
    // -------------------------------------------------------------------------
 
124
 
 
125
    @JsonIgnore
 
126
    public int getId()
 
127
    {
 
128
        return id;
 
129
    }
 
130
 
 
131
    public void setId( int id )
 
132
    {
 
133
        this.id = id;
 
134
    }
 
135
 
 
136
    @JsonProperty( value = "id" )
 
137
    @JacksonXmlProperty( isAttribute = true )
 
138
    public String getUid()
 
139
    {
 
140
        return uid;
 
141
    }
 
142
 
 
143
    public void setUid( String uid )
 
144
    {
 
145
        this.uid = uid;
 
146
    }
 
147
 
 
148
    @JsonProperty
 
149
    @JsonView( {DetailedView.class, BasicView.class, ExportView.class} )
 
150
    @JacksonXmlProperty( isAttribute = true )
 
151
    public String getCode()
 
152
    {
 
153
        return code;
 
154
    }
 
155
 
 
156
    public void setCode( String code )
 
157
    {
 
158
        this.code = code;
 
159
    }
 
160
 
 
161
    @JsonProperty
 
162
    @JsonView( {DetailedView.class, BasicView.class, ExportView.class} )
 
163
    @JacksonXmlProperty( isAttribute = true )
 
164
    public String getName()
 
165
    {
 
166
        return name;
 
167
    }
 
168
 
 
169
    public void setName( String name )
 
170
    {
 
171
        this.name = name;
 
172
    }
 
173
 
 
174
    @JsonProperty
 
175
    @JsonView( {DetailedView.class, BasicView.class, ExportView.class} )
 
176
    @JacksonXmlProperty( isAttribute = true )
 
177
    public Date getLastUpdated()
 
178
    {
 
179
        return lastUpdated;
 
180
    }
 
181
 
 
182
    public void setLastUpdated( Date lastUpdated )
 
183
    {
 
184
        this.lastUpdated = lastUpdated;
 
185
    }
 
186
 
 
187
    public String getDisplayName()
 
188
    {
 
189
        return displayName != null && !displayName.trim().isEmpty() ? displayName : getName();
 
190
    }
 
191
 
 
192
    public void setDisplayName( String displayName )
 
193
    {
 
194
        this.displayName = displayName;
 
195
    }
 
196
 
 
197
    @Override
 
198
    public boolean equals( Object o )
 
199
    {
 
200
        if ( this == o ) return true;
 
201
        if ( o == null || getClass() != o.getClass() ) return false;
 
202
 
 
203
        BaseIdentifiableObject that = (BaseIdentifiableObject) o;
 
204
 
 
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;
 
208
 
 
209
        return true;
 
210
    }
 
211
 
 
212
    @Override
 
213
    public int hashCode()
 
214
    {
 
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);
 
219
 
 
220
        return result;
 
221
    }
 
222
 
 
223
    // -------------------------------------------------------------------------
 
224
    // Logic
 
225
    // -------------------------------------------------------------------------
 
226
 
 
227
    /**
 
228
     * Set auto-generated fields on save or update
 
229
     */
 
230
    public void setAutoFields()
 
231
    {
 
232
        if ( uid == null || uid.length() == 0 )
 
233
        {
 
234
            setUid( CodeGenerator.generateCode() );
 
235
        }
 
236
 
 
237
        setLastUpdated( new Date() );
 
238
    }
 
239
 
 
240
    /**
 
241
     * Get a map of uids to internal identifiers
 
242
     *
 
243
     * @param objects the IdentifiableObjects to put in the map
 
244
     * @return the map
 
245
     */
 
246
    public static Map<String, Integer> getUIDMap( Collection<? extends BaseIdentifiableObject> objects )
 
247
    {
 
248
        Map<String, Integer> map = new HashMap<String, Integer>();
 
249
 
 
250
        for ( IdentifiableObject object : objects )
 
251
        {
 
252
            String uid = object.getUid();
 
253
            int internalId = object.getId();
 
254
 
 
255
            map.put( uid, internalId );
 
256
        }
 
257
 
 
258
        return map;
 
259
    }
 
260
 
 
261
    /**
 
262
     * Get a map of codes to internal identifiers
 
263
     *
 
264
     * @param objects the NameableObjects to put in the map
 
265
     * @return the map
 
266
     */
 
267
    public static Map<String, Integer> getCodeMap( Collection<? extends BaseIdentifiableObject> objects )
 
268
    {
 
269
        Map<String, Integer> map = new HashMap<String, Integer>();
 
270
        for ( BaseIdentifiableObject object : objects )
 
271
        {
 
272
            String code = object.getCode();
 
273
            int internalId = object.getId();
 
274
 
 
275
            map.put( code, internalId );
 
276
        }
 
277
        return map;
 
278
    }
 
279
 
 
280
    /**
 
281
     * Get a map of names to internal identifiers
 
282
     *
 
283
     * @param objects the NameableObjects to put in the map
 
284
     * @return the map
 
285
     */
 
286
    public static Map<String, Integer> getNameMap( Collection<? extends BaseIdentifiableObject> objects )
 
287
    {
 
288
        Map<String, Integer> map = new HashMap<String, Integer>();
 
289
        for ( BaseIdentifiableObject object : objects )
 
290
        {
 
291
            String name = object.getName();
 
292
            int internalId = object.getId();
 
293
 
 
294
            map.put( name, internalId );
 
295
        }
 
296
        return map;
 
297
    }
 
298
 
 
299
    @Override
 
300
    public String toString()
 
301
    {
 
302
        return "{" + "id=" + id + ", uid='" + uid + '\'' + ", code='" +
 
303
            code + '\'' + ", name='" + name + '\'' + ", lastUpdated=" + lastUpdated + "}";
 
304
    }
 
305
 
 
306
    @Override
 
307
    public void mergeWith( IdentifiableObject other )
 
308
    {
 
309
        Validate.notNull( other );
 
310
 
 
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();
 
315
    }
 
316
}