1
package org.hisp.dhis.oust.manager;
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.util.ArrayList;
31
import java.util.Collection;
32
import java.util.HashSet;
36
import org.hisp.dhis.organisationunit.OrganisationUnit;
37
import org.hisp.dhis.organisationunit.OrganisationUnitService;
39
import com.opensymphony.xwork.ActionContext;
42
* @author Torgeir Lorange Ostby
43
* @version $Id: DefaultSelectionTreeManager.java 5652 2008-09-06 13:24:34Z larshelg $
45
public class DefaultSelectionTreeManager
46
implements SelectionTreeManager
48
private static final String SESSION_KEY_SELECTED_ORG_UNITS = "dhis-oust-selected-org-units";
50
private static final String SESSION_KEY_ROOT_ORG_UNITS = "dhis-oust-root-org-units";
52
// -------------------------------------------------------------------------
54
// -------------------------------------------------------------------------
56
private OrganisationUnitService organisationUnitService;
58
public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
60
this.organisationUnitService = organisationUnitService;
63
// -------------------------------------------------------------------------
64
// SelectionTreeManager implementation
65
// -------------------------------------------------------------------------
67
public void setRootOrganisationUnits( Collection<OrganisationUnit> organisationUnits )
69
if ( organisationUnits == null )
71
throw new IllegalArgumentException( "Root OrganisationUnit cannot be null" );
74
Collection<OrganisationUnit> rootUnits = new HashSet<OrganisationUnit>();
75
rootUnits.addAll( organisationUnits );
77
saveToSession( SESSION_KEY_ROOT_ORG_UNITS, rootUnits );
79
clearSelectedOrganisationUnits();
82
public void setRootOrganisationUnitsParent( OrganisationUnit rootUnitsParent )
84
if ( rootUnitsParent == null )
86
throw new IllegalArgumentException( "Root OrganisationUnits parent cannot be null" );
89
OrganisationUnit reloadedRootUnitsParent = reloadOrganisationUnit( rootUnitsParent );
91
saveToSession( SESSION_KEY_ROOT_ORG_UNITS, reloadedRootUnitsParent.getChildren() );
93
clearSelectedOrganisationUnits();
96
public Collection<OrganisationUnit> getRootOrganisationUnits()
98
Collection<OrganisationUnit> rootUnits = getCollectionFromSession( SESSION_KEY_ROOT_ORG_UNITS );
100
if ( rootUnits == null )
102
return organisationUnitService.getRootOrganisationUnits();
105
return reloadOrganisationUnits( rootUnits );
108
public OrganisationUnit getRootOrganisationUnitsParent()
110
Collection<OrganisationUnit> rootUnits = getCollectionFromSession( SESSION_KEY_ROOT_ORG_UNITS );
112
if ( rootUnits == null || rootUnits.isEmpty() )
117
OrganisationUnit randomRootUnit = rootUnits.iterator().next();
119
OrganisationUnit reloadedRootUnit = reloadOrganisationUnit( randomRootUnit );
121
return reloadedRootUnit.getParent();
124
public void resetRootOrganisationUnits()
126
removeFromSession( SESSION_KEY_ROOT_ORG_UNITS );
129
public void setSelectedOrganisationUnits( Collection<OrganisationUnit> selectedUnits )
131
if ( selectedUnits == null )
133
throw new IllegalArgumentException( "Selected OrganisationUnits cannot be null" );
136
// ---------------------------------------------------------------------
137
// Reload the units to ensure it is loaded within the current
139
// ---------------------------------------------------------------------
141
Collection<OrganisationUnit> reloadedSelectedUnits = reloadOrganisationUnits( selectedUnits );
143
// ---------------------------------------------------------------------
144
// Remove all selected units that are not in the trees
145
// ---------------------------------------------------------------------
147
Collection<OrganisationUnit> rootUnits = getRootOrganisationUnits();
149
if ( rootUnits != null )
151
reloadedSelectedUnits = getUnitsInTree( rootUnits, reloadedSelectedUnits );
153
saveToSession( SESSION_KEY_SELECTED_ORG_UNITS, reloadedSelectedUnits );
157
public Collection<OrganisationUnit> getSelectedOrganisationUnits()
159
Collection<OrganisationUnit> selectedUnits = getCollectionFromSession( SESSION_KEY_SELECTED_ORG_UNITS );
161
if ( selectedUnits == null )
163
return new HashSet<OrganisationUnit>();
166
return reloadOrganisationUnits( selectedUnits );
169
public void clearSelectedOrganisationUnits()
171
removeFromSession( SESSION_KEY_SELECTED_ORG_UNITS );
174
public OrganisationUnit getSelectedOrganisationUnit()
176
Collection<OrganisationUnit> selectedUnits = getSelectedOrganisationUnits();
178
if ( selectedUnits.isEmpty() )
183
return selectedUnits.iterator().next();
186
public void setSelectedOrganisationUnit( OrganisationUnit selectedUnit )
188
if ( selectedUnit == null )
190
throw new IllegalArgumentException( "Selected OrganisationUnit cannot be null" );
193
Set<OrganisationUnit> set = new HashSet<OrganisationUnit>( 1 );
194
set.add( selectedUnit );
195
setSelectedOrganisationUnits( set );
198
// -------------------------------------------------------------------------
200
// -------------------------------------------------------------------------
202
@SuppressWarnings( "unchecked" )
203
protected Map getSession()
205
return ActionContext.getContext().getSession();
208
@SuppressWarnings( "unchecked" )
209
private final void saveToSession( String key, Object object )
211
getSession().put( key, object );
214
@SuppressWarnings( "unchecked" )
215
private final Collection<OrganisationUnit> getCollectionFromSession( String key )
217
return (Collection<OrganisationUnit>) getSession().get( key );
220
private final void removeFromSession( String key )
222
getSession().remove( key );
225
// -------------------------------------------------------------------------
227
// -------------------------------------------------------------------------
229
private OrganisationUnit reloadOrganisationUnit( OrganisationUnit unit )
231
return organisationUnitService.getOrganisationUnit( unit.getId() );
234
private Collection<OrganisationUnit> reloadOrganisationUnits( Collection<OrganisationUnit> units )
236
Set<OrganisationUnit> reloadedUnits = new HashSet<OrganisationUnit>();
238
for ( OrganisationUnit unit : units )
240
OrganisationUnit reloadedUnit = reloadOrganisationUnit( unit );
242
if ( reloadedUnit != null )
244
reloadedUnits.add( reloadedUnit );
248
return reloadedUnits;
251
// -------------------------------------------------------------------------
252
// Supportive methods
253
// -------------------------------------------------------------------------
255
private Collection<OrganisationUnit> getUnitsInTree( Collection<OrganisationUnit> rootUnits, Collection<OrganisationUnit> selectedUnits )
257
Collection<OrganisationUnit> unitsInTree = new ArrayList<OrganisationUnit>();
259
for ( OrganisationUnit selectedUnit : selectedUnits )
261
if ( rootUnits.contains( selectedUnit ) )
263
unitsInTree.add( selectedUnit );
266
OrganisationUnit parent = selectedUnit.getParent();
268
while ( parent != null )
270
if ( rootUnits.contains( parent ) )
272
unitsInTree.add( selectedUnit );
275
parent = parent.getParent();