~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryOptionCombo.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.dataelement;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, 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 java.io.Serializable;
 
31
import java.util.Collection;
 
32
import java.util.HashSet;
 
33
import java.util.Iterator;
 
34
import java.util.Set;
 
35
 
 
36
import org.hisp.dhis.common.MetaObject;
 
37
 
 
38
/**
 
39
 * @author Abyot Aselefew
 
40
 * @version $Id$
 
41
 */
 
42
public class DataElementCategoryOptionCombo
 
43
    implements Serializable, MetaObject
 
44
{
 
45
    public static final String DEFAULT_NAME = "default";
 
46
    
 
47
    /**
 
48
     * The database internal identifier.
 
49
     */
 
50
    private int id;  
 
51
    
 
52
    /**
 
53
     * The category combo.
 
54
     */
 
55
    private DataElementCategoryCombo categoryCombo;
 
56
    
 
57
    /**
 
58
     * The category options.
 
59
     */
 
60
    private Set<DataElementCategoryOption> categoryOptions = new HashSet<DataElementCategoryOption>();
 
61
    
 
62
    // -------------------------------------------------------------------------
 
63
    // Constructors
 
64
    // -------------------------------------------------------------------------
 
65
 
 
66
    public DataElementCategoryOptionCombo()
 
67
    {
 
68
    }
 
69
    
 
70
    // -------------------------------------------------------------------------
 
71
    // hashCode and equals
 
72
    // -------------------------------------------------------------------------
 
73
 
 
74
    @Override
 
75
    public int hashCode()
 
76
    {
 
77
        final int prime = 31;
 
78
        
 
79
        int result = 1;
 
80
        
 
81
        result = prime * result + ( ( categoryCombo == null ) ? 0 : categoryCombo.hashCode() );
 
82
        
 
83
        result = prime * result + ( ( categoryOptions == null ) ? 0 : categoryOptions.hashCode() );
 
84
        
 
85
        return result;
 
86
    }
 
87
 
 
88
    @Override
 
89
    public boolean equals( Object object )
 
90
    {
 
91
        if ( this == object )
 
92
        {
 
93
            return true;
 
94
        }
 
95
        
 
96
        if ( object == null )
 
97
        {
 
98
            return false;
 
99
        }
 
100
        
 
101
        if ( getClass() != object.getClass() )
 
102
        {
 
103
            return false;
 
104
        }
 
105
        
 
106
        final DataElementCategoryOptionCombo other = (DataElementCategoryOptionCombo) object;
 
107
        
 
108
        if ( categoryCombo == null )
 
109
        {
 
110
            if ( other.categoryCombo != null )
 
111
            {
 
112
                return false;
 
113
            }
 
114
        }
 
115
        else if ( !categoryCombo.equals( other.categoryCombo ) )
 
116
        {
 
117
            return false;
 
118
        }
 
119
        
 
120
        if ( categoryOptions == null )
 
121
        {
 
122
            if ( other.categoryOptions != null )
 
123
            {
 
124
                return false;
 
125
            }
 
126
        }
 
127
        else if ( !categoryOptions.equals( other.categoryOptions ) )
 
128
        {
 
129
            return false;
 
130
        }
 
131
        
 
132
        return true;
 
133
    }
 
134
    
 
135
    @Override
 
136
    public String toString()
 
137
    {
 
138
        StringBuffer buffer = new StringBuffer( "[" + categoryCombo + ", [" );
 
139
        
 
140
        Iterator<DataElementCategoryOption> iterator = categoryOptions.iterator();
 
141
        
 
142
        while ( iterator.hasNext() )
 
143
        {
 
144
            buffer.append( iterator.next().toString() );
 
145
            
 
146
            if ( iterator.hasNext() )
 
147
            {
 
148
                buffer.append( ", " );
 
149
            }
 
150
        }
 
151
        
 
152
        return buffer.append( "]]" ).toString();
 
153
    }
 
154
 
 
155
    /**
 
156
     * Tests whether two objects compare on a name basis. The default equals method
 
157
     * becomes unusable in conjunction with persistence frameworks that put proxys
 
158
     * on associated objects and collections, since it tests the class type which
 
159
     * will differ between the proxy and the raw type.
 
160
     * 
 
161
     * @param object the object to test for equality.
 
162
     * @return true if objects are equal, false otherwise.
 
163
     */
 
164
    public boolean equalsOnName( DataElementCategoryOptionCombo object )
 
165
    {
 
166
        if ( this == object )
 
167
        {
 
168
            return true;
 
169
        }
 
170
        
 
171
        if ( object == null )
 
172
        {
 
173
            return false;
 
174
        }
 
175
        
 
176
        if ( object.getCategoryCombo() == null )
 
177
        {
 
178
            return false;
 
179
        }
 
180
        
 
181
        if ( object.getCategoryOptions() == null )
 
182
        {
 
183
            return false;
 
184
        }
 
185
        
 
186
        if ( !categoryCombo.getName().equals( object.getCategoryCombo().getName() ) )
 
187
        {
 
188
            return false;
 
189
        }
 
190
        
 
191
        if ( categoryOptions.size() != object.getCategoryOptions().size() )
 
192
        {
 
193
            return false;
 
194
        }
 
195
        
 
196
        Set<String> names1 = new HashSet<String>();
 
197
        Set<String> names2 = new HashSet<String>();
 
198
        
 
199
        for ( DataElementCategoryOption option : categoryOptions )
 
200
        {
 
201
            names1.add( option.getName() );
 
202
        }
 
203
        
 
204
        for ( DataElementCategoryOption option : object.getCategoryOptions() )
 
205
        {
 
206
            names2.add( option.getName() );
 
207
        }
 
208
        
 
209
        return names1.equals( names2 );
 
210
    }
 
211
    
 
212
    /**
 
213
     * Tests if this object equals to an object in the given Collection on a name basis.
 
214
     * 
 
215
     * @param categoryOptionCombos the Collection.
 
216
     * @return true if the Collection contains this object, false otherwise.
 
217
     */
 
218
    public DataElementCategoryOptionCombo get( Collection<DataElementCategoryOptionCombo> categoryOptionCombos )
 
219
    {
 
220
        for ( DataElementCategoryOptionCombo combo : categoryOptionCombos )
 
221
        {
 
222
            if ( combo.equalsOnName( this ) )
 
223
            {
 
224
                return combo;
 
225
            }
 
226
        }
 
227
        
 
228
        return null;
 
229
    }
 
230
 
 
231
    // -------------------------------------------------------------------------
 
232
    // MetaObject implementation
 
233
    // -------------------------------------------------------------------------
 
234
 
 
235
    public int getId()
 
236
    {
 
237
        return id;
 
238
    }
 
239
    
 
240
    public void setId( int id )
 
241
    {
 
242
        this.id = id;
 
243
    }
 
244
    
 
245
    public String getName()
 
246
    {
 
247
        StringBuffer name = new StringBuffer();
 
248
        
 
249
        if ( categoryOptions != null && categoryOptions.size() > 0 )
 
250
        {
 
251
            Iterator<DataElementCategoryOption> iterator = categoryOptions.iterator();
 
252
            
 
253
            name.append( "(" ).append( iterator.next().getName() );
 
254
                                    
 
255
            while ( iterator.hasNext() )
 
256
            {
 
257
                name.append( ", " ).append( iterator.next().getName() );
 
258
            }
 
259
            
 
260
            name.append( ")" );
 
261
        }
 
262
        
 
263
        return name.toString();
 
264
    }
 
265
    
 
266
    public void setName( String name )
 
267
    {
 
268
        throw new UnsupportedOperationException( "Cannot set name on DataElementCategoryOptionCombo: " + name );
 
269
    }
 
270
    
 
271
    public String getShortName()
 
272
    {
 
273
        StringBuffer name = new StringBuffer();
 
274
        
 
275
        if ( categoryOptions != null && categoryOptions.size() > 0 )
 
276
        {
 
277
            Iterator<DataElementCategoryOption> iterator = categoryOptions.iterator();
 
278
            
 
279
            name.append( "(" ).append( iterator.next().getShortName() );
 
280
                                    
 
281
            while ( iterator.hasNext() )
 
282
            {
 
283
                name.append( ", " ).append( iterator.next().getShortName() );
 
284
            }
 
285
            
 
286
            name.append( ")" );
 
287
        }
 
288
        
 
289
        return name.toString();
 
290
    }
 
291
    
 
292
    public void setShortName( String shortName )
 
293
    {
 
294
        throw new UnsupportedOperationException( "Cannot set shortName on DataElementCategoryOptionCombo: " + shortName );
 
295
    }
 
296
    
 
297
    public String getCode()
 
298
    {
 
299
        return getShortName();
 
300
    }
 
301
    
 
302
    public void setCode( String code )
 
303
    {
 
304
        throw new UnsupportedOperationException( "Cannot set code on DataElementCategoryOptionCombo: " + code );
 
305
    }
 
306
    
 
307
    public String getAlternativeName()
 
308
    {
 
309
        return getShortName();
 
310
    }
 
311
    
 
312
    public void setAlternativeName( String alternativeName )
 
313
    {
 
314
        throw new UnsupportedOperationException( "Cannot set alternativename on DataElementCategoryOptionCombo: " + alternativeName );
 
315
    }
 
316
    
 
317
    // -------------------------------------------------------------------------
 
318
    // Getters and setters
 
319
    // -------------------------------------------------------------------------
 
320
 
 
321
    public DataElementCategoryCombo getCategoryCombo()
 
322
    {
 
323
        return categoryCombo;
 
324
    }
 
325
    
 
326
    public void setCategoryCombo ( DataElementCategoryCombo categoryCombo )
 
327
    {
 
328
        this.categoryCombo = categoryCombo;
 
329
    }
 
330
    
 
331
    public Set<DataElementCategoryOption> getCategoryOptions()
 
332
    {
 
333
        return categoryOptions;
 
334
    }
 
335
    
 
336
    public void setCategoryOptions( Set<DataElementCategoryOption> categoryOptions )
 
337
    {
 
338
        this.categoryOptions = categoryOptions;
 
339
    }
 
340
}