~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/ouwt/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.ouwt.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.List;
 
35
import java.util.Map;
 
36
 
 
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;
 
43
 
 
44
import com.opensymphony.xwork.Action;
 
45
 
 
46
/**
 
47
 * @author Torgeir Lorange Ostby
 
48
 * @version $Id: GetExpandedTreeAction.java 5282 2008-05-28 10:41:06Z larshelg $
 
49
 */
 
50
public class GetExpandedTreeAction
 
51
    implements Action
 
52
{
 
53
    private static final Log LOG = LogFactory.getLog( GetExpandedTreeAction.class );
 
54
 
 
55
    // -------------------------------------------------------------------------
 
56
    // Dependencies
 
57
    // -------------------------------------------------------------------------
 
58
 
 
59
    private OrganisationUnitSelectionManager selectionManager;
 
60
 
 
61
    public void setSelectionManager( OrganisationUnitSelectionManager selectionManger )
 
62
    {
 
63
        this.selectionManager = selectionManger;
 
64
    }
 
65
 
 
66
    private TreeStateManager treeStateManager;
 
67
 
 
68
    public void setTreeStateManager( TreeStateManager treeStateManager )
 
69
    {
 
70
        this.treeStateManager = treeStateManager;
 
71
    }
 
72
 
 
73
    // -------------------------------------------------------------------------
 
74
    // Output
 
75
    // -------------------------------------------------------------------------
 
76
 
 
77
    private List<OrganisationUnit> roots;
 
78
 
 
79
    public List<OrganisationUnit> getRoots()
 
80
    {
 
81
        return roots;
 
82
    }
 
83
 
 
84
    private List<OrganisationUnit> parents = new ArrayList<OrganisationUnit>();
 
85
 
 
86
    public List<OrganisationUnit> getParents()
 
87
    {
 
88
        return parents;
 
89
    }
 
90
 
 
91
    private Map<OrganisationUnit, List<OrganisationUnit>> childrenMap = new HashMap<OrganisationUnit, List<OrganisationUnit>>();
 
92
 
 
93
    public Map<OrganisationUnit, List<OrganisationUnit>> getChildrenMap()
 
94
    {
 
95
        return childrenMap;
 
96
    }
 
97
 
 
98
    private Collection<OrganisationUnit> selected;
 
99
 
 
100
    public Collection<OrganisationUnit> getSelected()
 
101
    {
 
102
        return selected;
 
103
    }
 
104
 
 
105
    // -------------------------------------------------------------------------
 
106
    // Action implementation
 
107
    // -------------------------------------------------------------------------
 
108
 
 
109
    public String execute()
 
110
        throws Exception
 
111
    {
 
112
        // ---------------------------------------------------------------------
 
113
        // Get the roots
 
114
        // ---------------------------------------------------------------------
 
115
 
 
116
        roots = new ArrayList<OrganisationUnit>( selectionManager.getRootOrganisationUnits() );
 
117
 
 
118
        Collections.sort( roots, new OrganisationUnitNameComparator() );
 
119
 
 
120
        // ---------------------------------------------------------------------
 
121
        // Get the children of the roots
 
122
        // ---------------------------------------------------------------------
 
123
 
 
124
        for ( OrganisationUnit root : roots )
 
125
        {
 
126
            boolean hasChildren = root.getChildren().size() > 0; // Dirty loading
 
127
 
 
128
            LOG.debug( "OrganisationUnit " + root.getId() + " has children = " + hasChildren );
 
129
 
 
130
            if ( treeStateManager.isSubtreeExpanded( root ) )
 
131
            {
 
132
                addParentWithChildren( root );
 
133
            }
 
134
        }
 
135
 
 
136
        // ---------------------------------------------------------------------
 
137
        // Get the selected units
 
138
        // ---------------------------------------------------------------------
 
139
 
 
140
        selected = selectionManager.getSelectedOrganisationUnits();
 
141
 
 
142
        return SUCCESS;
 
143
    }
 
144
 
 
145
    private void addParentWithChildren( OrganisationUnit parent )
 
146
        throws Exception
 
147
    {
 
148
        List<OrganisationUnit> children = getChildren( parent );
 
149
 
 
150
        parents.add( parent );
 
151
 
 
152
        childrenMap.put( parent, children );
 
153
 
 
154
        for ( OrganisationUnit child : children )
 
155
        {
 
156
            boolean hasChildren = child.getChildren().size() > 0; // Dirty loading
 
157
 
 
158
            LOG.debug( "OrganisationUnit " + child.getId() + " has children = " + hasChildren );
 
159
 
 
160
            if ( treeStateManager.isSubtreeExpanded( child ) )
 
161
            {
 
162
                addParentWithChildren( child );
 
163
            }
 
164
        }
 
165
    }
 
166
 
 
167
    private final List<OrganisationUnit> getChildren( OrganisationUnit parent )
 
168
    {
 
169
        List<OrganisationUnit> children = new ArrayList<OrganisationUnit>( parent.getChildren() );
 
170
 
 
171
        Collections.sort( children, new OrganisationUnitNameComparator() );
 
172
 
 
173
        return children;
 
174
    }
 
175
}