1
package org.hisp.dhis.ouwt.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.List;
37
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
39
import org.hisp.dhis.organisationunit.OrganisationUnit;
40
import org.hisp.dhis.organisationunit.comparator.OrganisationUnitNameComparator;
41
import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
42
import org.hisp.dhis.ouwt.manager.TreeStateManager;
44
import com.opensymphony.xwork.Action;
47
* @author Torgeir Lorange Ostby
48
* @version $Id: GetExpandedTreeAction.java 5282 2008-05-28 10:41:06Z larshelg $
50
public class GetExpandedTreeAction
53
private static final Log LOG = LogFactory.getLog( GetExpandedTreeAction.class );
55
// -------------------------------------------------------------------------
57
// -------------------------------------------------------------------------
59
private OrganisationUnitSelectionManager selectionManager;
61
public void setSelectionManager( OrganisationUnitSelectionManager selectionManger )
63
this.selectionManager = selectionManger;
66
private TreeStateManager treeStateManager;
68
public void setTreeStateManager( TreeStateManager treeStateManager )
70
this.treeStateManager = treeStateManager;
73
// -------------------------------------------------------------------------
75
// -------------------------------------------------------------------------
77
private List<OrganisationUnit> roots;
79
public List<OrganisationUnit> getRoots()
84
private List<OrganisationUnit> parents = new ArrayList<OrganisationUnit>();
86
public List<OrganisationUnit> getParents()
91
private Map<OrganisationUnit, List<OrganisationUnit>> childrenMap = new HashMap<OrganisationUnit, List<OrganisationUnit>>();
93
public Map<OrganisationUnit, List<OrganisationUnit>> getChildrenMap()
98
private Collection<OrganisationUnit> selected;
100
public Collection<OrganisationUnit> getSelected()
105
// -------------------------------------------------------------------------
106
// Action implementation
107
// -------------------------------------------------------------------------
109
public String execute()
112
// ---------------------------------------------------------------------
114
// ---------------------------------------------------------------------
116
roots = new ArrayList<OrganisationUnit>( selectionManager.getRootOrganisationUnits() );
118
Collections.sort( roots, new OrganisationUnitNameComparator() );
120
// ---------------------------------------------------------------------
121
// Get the children of the roots
122
// ---------------------------------------------------------------------
124
for ( OrganisationUnit root : roots )
126
boolean hasChildren = root.getChildren().size() > 0; // Dirty loading
128
LOG.debug( "OrganisationUnit " + root.getId() + " has children = " + hasChildren );
130
if ( treeStateManager.isSubtreeExpanded( root ) )
132
addParentWithChildren( root );
136
// ---------------------------------------------------------------------
137
// Get the selected units
138
// ---------------------------------------------------------------------
140
selected = selectionManager.getSelectedOrganisationUnits();
145
private void addParentWithChildren( OrganisationUnit parent )
148
List<OrganisationUnit> children = getChildren( parent );
150
parents.add( parent );
152
childrenMap.put( parent, children );
154
for ( OrganisationUnit child : children )
156
boolean hasChildren = child.getChildren().size() > 0; // Dirty loading
158
LOG.debug( "OrganisationUnit " + child.getId() + " has children = " + hasChildren );
160
if ( treeStateManager.isSubtreeExpanded( child ) )
162
addParentWithChildren( child );
167
private final List<OrganisationUnit> getChildren( OrganisationUnit parent )
169
List<OrganisationUnit> children = new ArrayList<OrganisationUnit>( parent.getChildren() );
171
Collections.sort( children, new OrganisationUnitNameComparator() );