~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/action/GetExpandedTreeAction.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.action;
 
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.Collections;
 
33
import java.util.HashMap;
 
34
import java.util.HashSet;
 
35
import java.util.List;
 
36
import java.util.Map;
 
37
import java.util.Set;
 
38
 
 
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;
 
44
 
 
45
import com.opensymphony.xwork.Action;
 
46
 
 
47
/**
 
48
 * @author Torgeir Lorange Ostby
 
49
 * @version $Id: GetExpandedTreeAction.java 6260 2008-11-11 15:58:43Z larshelg $
 
50
 */
 
51
public class GetExpandedTreeAction
 
52
    implements Action
 
53
{
 
54
    private static final Log LOG = LogFactory.getLog( GetExpandedTreeAction.class );
 
55
 
 
56
    // -------------------------------------------------------------------------
 
57
    // Dependencies
 
58
    // -------------------------------------------------------------------------
 
59
 
 
60
    private SelectionTreeManager selectionTreeManager;
 
61
 
 
62
    public void setSelectionTreeManager( SelectionTreeManager selectionTreeManager )
 
63
    {
 
64
        this.selectionTreeManager = selectionTreeManager;
 
65
    }
 
66
 
 
67
    // -------------------------------------------------------------------------
 
68
    // Output
 
69
    // -------------------------------------------------------------------------
 
70
 
 
71
    private List<OrganisationUnit> roots;
 
72
 
 
73
    public List<OrganisationUnit> getRoots()
 
74
    {
 
75
        return roots;
 
76
    }
 
77
 
 
78
    private List<OrganisationUnit> parents = new ArrayList<OrganisationUnit>();
 
79
 
 
80
    public List<OrganisationUnit> getParents()
 
81
    {
 
82
        return parents;
 
83
    }
 
84
 
 
85
    private Map<OrganisationUnit, List<OrganisationUnit>> childrenMap = new HashMap<OrganisationUnit, List<OrganisationUnit>>();
 
86
 
 
87
    public Map<OrganisationUnit, List<OrganisationUnit>> getChildrenMap()
 
88
    {
 
89
        return childrenMap;
 
90
    }
 
91
 
 
92
    private Collection<OrganisationUnit> selected;
 
93
 
 
94
    public Collection<OrganisationUnit> getSelected()
 
95
    {
 
96
        return selected;
 
97
    }
 
98
 
 
99
    // -------------------------------------------------------------------------
 
100
    // Action implementation
 
101
    // -------------------------------------------------------------------------
 
102
 
 
103
    public String execute()
 
104
        throws Exception
 
105
    {
 
106
        roots = new ArrayList<OrganisationUnit>( selectionTreeManager.getRootOrganisationUnits() );
 
107
 
 
108
        Collections.sort( roots, new OrganisationUnitNameComparator() );
 
109
 
 
110
        // ---------------------------------------------------------------------
 
111
        // Get the units that need to be expanded in order for the selected
 
112
        // organisation units to be visible
 
113
        // ---------------------------------------------------------------------
 
114
 
 
115
        selected = selectionTreeManager.getSelectedOrganisationUnits();
 
116
 
 
117
        Collection<OrganisationUnit> pathNodes = findPathNodes( roots, selected );
 
118
 
 
119
        // ---------------------------------------------------------------------
 
120
        // Get the children of the roots
 
121
        // ---------------------------------------------------------------------
 
122
 
 
123
        for ( OrganisationUnit root : roots )
 
124
        {
 
125
            boolean hasChildren = root.getChildren().size() > 0; // Dirty loading
 
126
 
 
127
            LOG.debug( "OrganisationUnit " + root.getId() + " has children = " + hasChildren );
 
128
 
 
129
            if ( pathNodes.contains( root ) )
 
130
            {
 
131
                addParentWithChildren( root, pathNodes );
 
132
            }
 
133
        }
 
134
 
 
135
        return SUCCESS;
 
136
    }
 
137
 
 
138
    private void addParentWithChildren( OrganisationUnit parent, Collection<OrganisationUnit> pathNodes )
 
139
        throws Exception
 
140
    {
 
141
        List<OrganisationUnit> children = getChildren( parent );
 
142
 
 
143
        parents.add( parent );
 
144
 
 
145
        childrenMap.put( parent, children );
 
146
 
 
147
        for ( OrganisationUnit child : children )
 
148
        {
 
149
            boolean hasChildren = child.getChildren().size() > 0; // Dirty loading
 
150
 
 
151
            LOG.debug( "OrganisationUnit " + child.getId() + " has children = " + hasChildren );
 
152
 
 
153
            if ( pathNodes.contains( child ) )
 
154
            {
 
155
                addParentWithChildren( child, pathNodes );
 
156
            }
 
157
        }
 
158
    }
 
159
 
 
160
    private final List<OrganisationUnit> getChildren( OrganisationUnit parent )
 
161
    {
 
162
        List<OrganisationUnit> children = new ArrayList<OrganisationUnit>( parent.getChildren() );
 
163
 
 
164
        Collections.sort( children, new OrganisationUnitNameComparator() );
 
165
 
 
166
        return children;
 
167
    }
 
168
 
 
169
    private final Collection<OrganisationUnit> findPathNodes( Collection<OrganisationUnit> roots,
 
170
        Collection<OrganisationUnit> selected )
 
171
    {
 
172
        Set<OrganisationUnit> pathNodes = new HashSet<OrganisationUnit>();
 
173
 
 
174
        for ( OrganisationUnit unit : selected )
 
175
        {
 
176
            if ( roots.contains( unit ) ) // Is parent => done
 
177
            {
 
178
                continue;
 
179
            }
 
180
 
 
181
            OrganisationUnit tmp = unit.getParent(); // Start with parent
 
182
 
 
183
            while ( !roots.contains( tmp ) )
 
184
            {
 
185
                pathNodes.add( tmp ); // Add each parent
 
186
                
 
187
                if ( tmp != null && tmp.getParent() != null )
 
188
                {
 
189
                    tmp = tmp.getParent();
 
190
                }                
 
191
            }
 
192
 
 
193
            pathNodes.add( tmp ); // Add the root
 
194
        }
 
195
 
 
196
        return pathNodes;
 
197
    }
 
198
}