1
package org.hisp.dhis.oust.action;
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.Collections;
33
import java.util.HashMap;
34
import java.util.HashSet;
35
import java.util.List;
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41
import org.hisp.dhis.organisationunit.OrganisationUnit;
42
import org.hisp.dhis.organisationunit.comparator.OrganisationUnitNameComparator;
43
import org.hisp.dhis.oust.manager.SelectionTreeManager;
45
import com.opensymphony.xwork.Action;
48
* @author Torgeir Lorange Ostby
49
* @version $Id: GetExpandedTreeAction.java 6260 2008-11-11 15:58:43Z larshelg $
51
public class GetExpandedTreeAction
54
private static final Log LOG = LogFactory.getLog( GetExpandedTreeAction.class );
56
// -------------------------------------------------------------------------
58
// -------------------------------------------------------------------------
60
private SelectionTreeManager selectionTreeManager;
62
public void setSelectionTreeManager( SelectionTreeManager selectionTreeManager )
64
this.selectionTreeManager = selectionTreeManager;
67
// -------------------------------------------------------------------------
69
// -------------------------------------------------------------------------
71
private List<OrganisationUnit> roots;
73
public List<OrganisationUnit> getRoots()
78
private List<OrganisationUnit> parents = new ArrayList<OrganisationUnit>();
80
public List<OrganisationUnit> getParents()
85
private Map<OrganisationUnit, List<OrganisationUnit>> childrenMap = new HashMap<OrganisationUnit, List<OrganisationUnit>>();
87
public Map<OrganisationUnit, List<OrganisationUnit>> getChildrenMap()
92
private Collection<OrganisationUnit> selected;
94
public Collection<OrganisationUnit> getSelected()
99
// -------------------------------------------------------------------------
100
// Action implementation
101
// -------------------------------------------------------------------------
103
public String execute()
106
roots = new ArrayList<OrganisationUnit>( selectionTreeManager.getRootOrganisationUnits() );
108
Collections.sort( roots, new OrganisationUnitNameComparator() );
110
// ---------------------------------------------------------------------
111
// Get the units that need to be expanded in order for the selected
112
// organisation units to be visible
113
// ---------------------------------------------------------------------
115
selected = selectionTreeManager.getSelectedOrganisationUnits();
117
Collection<OrganisationUnit> pathNodes = findPathNodes( roots, selected );
119
// ---------------------------------------------------------------------
120
// Get the children of the roots
121
// ---------------------------------------------------------------------
123
for ( OrganisationUnit root : roots )
125
boolean hasChildren = root.getChildren().size() > 0; // Dirty loading
127
LOG.debug( "OrganisationUnit " + root.getId() + " has children = " + hasChildren );
129
if ( pathNodes.contains( root ) )
131
addParentWithChildren( root, pathNodes );
138
private void addParentWithChildren( OrganisationUnit parent, Collection<OrganisationUnit> pathNodes )
141
List<OrganisationUnit> children = getChildren( parent );
143
parents.add( parent );
145
childrenMap.put( parent, children );
147
for ( OrganisationUnit child : children )
149
boolean hasChildren = child.getChildren().size() > 0; // Dirty loading
151
LOG.debug( "OrganisationUnit " + child.getId() + " has children = " + hasChildren );
153
if ( pathNodes.contains( child ) )
155
addParentWithChildren( child, pathNodes );
160
private final List<OrganisationUnit> getChildren( OrganisationUnit parent )
162
List<OrganisationUnit> children = new ArrayList<OrganisationUnit>( parent.getChildren() );
164
Collections.sort( children, new OrganisationUnitNameComparator() );
169
private final Collection<OrganisationUnit> findPathNodes( Collection<OrganisationUnit> roots,
170
Collection<OrganisationUnit> selected )
172
Set<OrganisationUnit> pathNodes = new HashSet<OrganisationUnit>();
174
for ( OrganisationUnit unit : selected )
176
if ( roots.contains( unit ) ) // Is parent => done
181
OrganisationUnit tmp = unit.getParent(); // Start with parent
183
while ( !roots.contains( tmp ) )
185
pathNodes.add( tmp ); // Add each parent
187
if ( tmp != null && tmp.getParent() != null )
189
tmp = tmp.getParent();
193
pathNodes.add( tmp ); // Add the root