1
package org.hisp.dhis.dataelement;
4
* Copyright (c) 2004-2007, 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 java.io.Serializable;
31
import java.util.Collection;
32
import java.util.HashSet;
33
import java.util.Iterator;
36
import org.hisp.dhis.common.MetaObject;
39
* @author Abyot Aselefew
42
public class DataElementCategoryOptionCombo
43
implements Serializable, MetaObject
45
public static final String DEFAULT_NAME = "default";
48
* The database internal identifier.
55
private DataElementCategoryCombo categoryCombo;
58
* The category options.
60
private Set<DataElementCategoryOption> categoryOptions = new HashSet<DataElementCategoryOption>();
62
// -------------------------------------------------------------------------
64
// -------------------------------------------------------------------------
66
public DataElementCategoryOptionCombo()
70
// -------------------------------------------------------------------------
71
// hashCode and equals
72
// -------------------------------------------------------------------------
81
result = prime * result + ( ( categoryCombo == null ) ? 0 : categoryCombo.hashCode() );
83
result = prime * result + ( ( categoryOptions == null ) ? 0 : categoryOptions.hashCode() );
89
public boolean equals( Object object )
101
if ( getClass() != object.getClass() )
106
final DataElementCategoryOptionCombo other = (DataElementCategoryOptionCombo) object;
108
if ( categoryCombo == null )
110
if ( other.categoryCombo != null )
115
else if ( !categoryCombo.equals( other.categoryCombo ) )
120
if ( categoryOptions == null )
122
if ( other.categoryOptions != null )
127
else if ( !categoryOptions.equals( other.categoryOptions ) )
136
public String toString()
138
StringBuffer buffer = new StringBuffer( "[" + categoryCombo + ", [" );
140
Iterator<DataElementCategoryOption> iterator = categoryOptions.iterator();
142
while ( iterator.hasNext() )
144
buffer.append( iterator.next().toString() );
146
if ( iterator.hasNext() )
148
buffer.append( ", " );
152
return buffer.append( "]]" ).toString();
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.
161
* @param object the object to test for equality.
162
* @return true if objects are equal, false otherwise.
164
public boolean equalsOnName( DataElementCategoryOptionCombo object )
166
if ( this == object )
171
if ( object == null )
176
if ( object.getCategoryCombo() == null )
181
if ( object.getCategoryOptions() == null )
186
if ( !categoryCombo.getName().equals( object.getCategoryCombo().getName() ) )
191
if ( categoryOptions.size() != object.getCategoryOptions().size() )
196
Set<String> names1 = new HashSet<String>();
197
Set<String> names2 = new HashSet<String>();
199
for ( DataElementCategoryOption option : categoryOptions )
201
names1.add( option.getName() );
204
for ( DataElementCategoryOption option : object.getCategoryOptions() )
206
names2.add( option.getName() );
209
return names1.equals( names2 );
213
* Tests if this object equals to an object in the given Collection on a name basis.
215
* @param categoryOptionCombos the Collection.
216
* @return true if the Collection contains this object, false otherwise.
218
public DataElementCategoryOptionCombo get( Collection<DataElementCategoryOptionCombo> categoryOptionCombos )
220
for ( DataElementCategoryOptionCombo combo : categoryOptionCombos )
222
if ( combo.equalsOnName( this ) )
231
// -------------------------------------------------------------------------
232
// MetaObject implementation
233
// -------------------------------------------------------------------------
240
public void setId( int id )
245
public String getName()
247
StringBuffer name = new StringBuffer();
249
if ( categoryOptions != null && categoryOptions.size() > 0 )
251
Iterator<DataElementCategoryOption> iterator = categoryOptions.iterator();
253
name.append( "(" ).append( iterator.next().getName() );
255
while ( iterator.hasNext() )
257
name.append( ", " ).append( iterator.next().getName() );
263
return name.toString();
266
public void setName( String name )
268
throw new UnsupportedOperationException( "Cannot set name on DataElementCategoryOptionCombo: " + name );
271
public String getShortName()
273
StringBuffer name = new StringBuffer();
275
if ( categoryOptions != null && categoryOptions.size() > 0 )
277
Iterator<DataElementCategoryOption> iterator = categoryOptions.iterator();
279
name.append( "(" ).append( iterator.next().getShortName() );
281
while ( iterator.hasNext() )
283
name.append( ", " ).append( iterator.next().getShortName() );
289
return name.toString();
292
public void setShortName( String shortName )
294
throw new UnsupportedOperationException( "Cannot set shortName on DataElementCategoryOptionCombo: " + shortName );
297
public String getCode()
299
return getShortName();
302
public void setCode( String code )
304
throw new UnsupportedOperationException( "Cannot set code on DataElementCategoryOptionCombo: " + code );
307
public String getAlternativeName()
309
return getShortName();
312
public void setAlternativeName( String alternativeName )
314
throw new UnsupportedOperationException( "Cannot set alternativename on DataElementCategoryOptionCombo: " + alternativeName );
317
// -------------------------------------------------------------------------
318
// Getters and setters
319
// -------------------------------------------------------------------------
321
public DataElementCategoryCombo getCategoryCombo()
323
return categoryCombo;
326
public void setCategoryCombo ( DataElementCategoryCombo categoryCombo )
328
this.categoryCombo = categoryCombo;
331
public Set<DataElementCategoryOption> getCategoryOptions()
333
return categoryOptions;
336
public void setCategoryOptions( Set<DataElementCategoryOption> categoryOptions )
338
this.categoryOptions = categoryOptions;