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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/oust/manager/DefaultSelectionTreeManager.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.oust.manager;
 
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.util.ArrayList;
 
31
import java.util.Collection;
 
32
import java.util.HashSet;
 
33
import java.util.Map;
 
34
import java.util.Set;
 
35
 
 
36
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
37
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
38
 
 
39
import com.opensymphony.xwork.ActionContext;
 
40
 
 
41
/**
 
42
 * @author Torgeir Lorange Ostby
 
43
 * @version $Id: DefaultSelectionTreeManager.java 5652 2008-09-06 13:24:34Z larshelg $
 
44
 */
 
45
public class DefaultSelectionTreeManager
 
46
    implements SelectionTreeManager
 
47
{
 
48
    private static final String SESSION_KEY_SELECTED_ORG_UNITS = "dhis-oust-selected-org-units";
 
49
 
 
50
    private static final String SESSION_KEY_ROOT_ORG_UNITS = "dhis-oust-root-org-units";
 
51
 
 
52
    // -------------------------------------------------------------------------
 
53
    // Dependencies
 
54
    // -------------------------------------------------------------------------
 
55
 
 
56
    private OrganisationUnitService organisationUnitService;
 
57
 
 
58
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
59
    {
 
60
        this.organisationUnitService = organisationUnitService;
 
61
    }
 
62
 
 
63
    // -------------------------------------------------------------------------
 
64
    // SelectionTreeManager implementation
 
65
    // -------------------------------------------------------------------------
 
66
 
 
67
    public void setRootOrganisationUnits( Collection<OrganisationUnit> organisationUnits )
 
68
    {
 
69
        if ( organisationUnits == null )
 
70
        {
 
71
            throw new IllegalArgumentException( "Root OrganisationUnit cannot be null" );
 
72
        }
 
73
 
 
74
        Collection<OrganisationUnit> rootUnits = new HashSet<OrganisationUnit>();
 
75
        rootUnits.addAll( organisationUnits );
 
76
 
 
77
        saveToSession( SESSION_KEY_ROOT_ORG_UNITS, rootUnits );
 
78
 
 
79
        clearSelectedOrganisationUnits();
 
80
    }
 
81
 
 
82
    public void setRootOrganisationUnitsParent( OrganisationUnit rootUnitsParent )
 
83
    {
 
84
        if ( rootUnitsParent == null )
 
85
        {
 
86
            throw new IllegalArgumentException( "Root OrganisationUnits parent cannot be null" );
 
87
        }
 
88
 
 
89
        OrganisationUnit reloadedRootUnitsParent = reloadOrganisationUnit( rootUnitsParent );
 
90
 
 
91
        saveToSession( SESSION_KEY_ROOT_ORG_UNITS, reloadedRootUnitsParent.getChildren() );
 
92
 
 
93
        clearSelectedOrganisationUnits();
 
94
    }
 
95
 
 
96
    public Collection<OrganisationUnit> getRootOrganisationUnits()
 
97
    {
 
98
        Collection<OrganisationUnit> rootUnits = getCollectionFromSession( SESSION_KEY_ROOT_ORG_UNITS );
 
99
 
 
100
        if ( rootUnits == null )
 
101
        {
 
102
            return organisationUnitService.getRootOrganisationUnits();
 
103
        }
 
104
 
 
105
        return reloadOrganisationUnits( rootUnits );
 
106
    }
 
107
 
 
108
    public OrganisationUnit getRootOrganisationUnitsParent()
 
109
    {
 
110
        Collection<OrganisationUnit> rootUnits = getCollectionFromSession( SESSION_KEY_ROOT_ORG_UNITS );
 
111
 
 
112
        if ( rootUnits == null || rootUnits.isEmpty() )
 
113
        {
 
114
            return null;
 
115
        }
 
116
 
 
117
        OrganisationUnit randomRootUnit = rootUnits.iterator().next();
 
118
 
 
119
        OrganisationUnit reloadedRootUnit = reloadOrganisationUnit( randomRootUnit );
 
120
 
 
121
        return reloadedRootUnit.getParent();
 
122
    }
 
123
 
 
124
    public void resetRootOrganisationUnits()
 
125
    {
 
126
        removeFromSession( SESSION_KEY_ROOT_ORG_UNITS );
 
127
    }
 
128
 
 
129
    public void setSelectedOrganisationUnits( Collection<OrganisationUnit> selectedUnits )
 
130
    {
 
131
        if ( selectedUnits == null )
 
132
        {
 
133
            throw new IllegalArgumentException( "Selected OrganisationUnits cannot be null" );
 
134
        }
 
135
 
 
136
        // ---------------------------------------------------------------------
 
137
        // Reload the units to ensure it is loaded within the current
 
138
        // transaction
 
139
        // ---------------------------------------------------------------------
 
140
 
 
141
        Collection<OrganisationUnit> reloadedSelectedUnits = reloadOrganisationUnits( selectedUnits );
 
142
 
 
143
        // ---------------------------------------------------------------------
 
144
        // Remove all selected units that are not in the trees
 
145
        // ---------------------------------------------------------------------
 
146
 
 
147
        Collection<OrganisationUnit> rootUnits = getRootOrganisationUnits();
 
148
 
 
149
        if ( rootUnits != null )
 
150
        {
 
151
            reloadedSelectedUnits = getUnitsInTree( rootUnits, reloadedSelectedUnits );
 
152
 
 
153
            saveToSession( SESSION_KEY_SELECTED_ORG_UNITS, reloadedSelectedUnits );
 
154
        }
 
155
    }
 
156
    
 
157
    public Collection<OrganisationUnit> getSelectedOrganisationUnits()
 
158
    {
 
159
        Collection<OrganisationUnit> selectedUnits = getCollectionFromSession( SESSION_KEY_SELECTED_ORG_UNITS );
 
160
 
 
161
        if ( selectedUnits == null )
 
162
        {
 
163
            return new HashSet<OrganisationUnit>();
 
164
        }
 
165
 
 
166
        return reloadOrganisationUnits( selectedUnits );
 
167
    }
 
168
 
 
169
    public void clearSelectedOrganisationUnits()
 
170
    {
 
171
        removeFromSession( SESSION_KEY_SELECTED_ORG_UNITS );
 
172
    }
 
173
 
 
174
    public OrganisationUnit getSelectedOrganisationUnit()
 
175
    {
 
176
        Collection<OrganisationUnit> selectedUnits = getSelectedOrganisationUnits();
 
177
 
 
178
        if ( selectedUnits.isEmpty() )
 
179
        {
 
180
            return null;
 
181
        }
 
182
 
 
183
        return selectedUnits.iterator().next();
 
184
    }
 
185
 
 
186
    public void setSelectedOrganisationUnit( OrganisationUnit selectedUnit )
 
187
    {
 
188
        if ( selectedUnit == null )
 
189
        {
 
190
            throw new IllegalArgumentException( "Selected OrganisationUnit cannot be null" );
 
191
        }
 
192
 
 
193
        Set<OrganisationUnit> set = new HashSet<OrganisationUnit>( 1 );
 
194
        set.add( selectedUnit );
 
195
        setSelectedOrganisationUnits( set );
 
196
    }
 
197
 
 
198
    // -------------------------------------------------------------------------
 
199
    // Session methods
 
200
    // -------------------------------------------------------------------------
 
201
 
 
202
    @SuppressWarnings( "unchecked" )
 
203
    protected Map getSession()
 
204
    {
 
205
        return ActionContext.getContext().getSession();
 
206
    }
 
207
 
 
208
    @SuppressWarnings( "unchecked" )
 
209
    private final void saveToSession( String key, Object object )
 
210
    {
 
211
        getSession().put( key, object );
 
212
    }
 
213
 
 
214
    @SuppressWarnings( "unchecked" )
 
215
    private final Collection<OrganisationUnit> getCollectionFromSession( String key )
 
216
    {
 
217
        return (Collection<OrganisationUnit>) getSession().get( key );
 
218
    }
 
219
 
 
220
    private final void removeFromSession( String key )
 
221
    {
 
222
        getSession().remove( key );
 
223
    }
 
224
 
 
225
    // -------------------------------------------------------------------------
 
226
    // Reload methods
 
227
    // -------------------------------------------------------------------------
 
228
 
 
229
    private OrganisationUnit reloadOrganisationUnit( OrganisationUnit unit )
 
230
    {
 
231
        return organisationUnitService.getOrganisationUnit( unit.getId() );
 
232
    }
 
233
 
 
234
    private Collection<OrganisationUnit> reloadOrganisationUnits( Collection<OrganisationUnit> units )
 
235
    {
 
236
        Set<OrganisationUnit> reloadedUnits = new HashSet<OrganisationUnit>();
 
237
 
 
238
        for ( OrganisationUnit unit : units )
 
239
        {
 
240
            OrganisationUnit reloadedUnit = reloadOrganisationUnit( unit );
 
241
 
 
242
            if ( reloadedUnit != null )
 
243
            {
 
244
                reloadedUnits.add( reloadedUnit );
 
245
            }
 
246
        }
 
247
 
 
248
        return reloadedUnits;
 
249
    }
 
250
 
 
251
    // -------------------------------------------------------------------------
 
252
    // Supportive methods
 
253
    // -------------------------------------------------------------------------
 
254
 
 
255
    private Collection<OrganisationUnit> getUnitsInTree( Collection<OrganisationUnit> rootUnits, Collection<OrganisationUnit> selectedUnits )
 
256
    {
 
257
        Collection<OrganisationUnit> unitsInTree = new ArrayList<OrganisationUnit>();
 
258
        
 
259
        for ( OrganisationUnit selectedUnit : selectedUnits )
 
260
        {
 
261
            if  ( rootUnits.contains( selectedUnit ) )
 
262
            {
 
263
                unitsInTree.add( selectedUnit );
 
264
            }
 
265
            
 
266
            OrganisationUnit parent = selectedUnit.getParent();
 
267
            
 
268
            while ( parent != null )
 
269
            {
 
270
                if ( rootUnits.contains( parent ) )
 
271
                {
 
272
                    unitsInTree.add( selectedUnit );
 
273
                }
 
274
                
 
275
                parent = parent.getParent();
 
276
            }
 
277
        }
 
278
        
 
279
        return unitsInTree;
 
280
    }
 
281
}