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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/hierarchy/ValidateNewParentOrganisationUnitAction.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.oum.action.hierarchy;
 
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 org.hisp.dhis.i18n.I18n;
 
31
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
32
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
33
import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
 
34
 
 
35
import com.opensymphony.xwork.ActionSupport;
 
36
 
 
37
/**
 
38
 * @author Torgeir Lorange Ostby
 
39
 * @version $Id: ValidateNewParentOrganisationUnitAction.java 1232 2006-01-30 06:29:30Z torgeilo $
 
40
 */
 
41
public class ValidateNewParentOrganisationUnitAction
 
42
    extends ActionSupport
 
43
{
 
44
    // -------------------------------------------------------------------------
 
45
    // Dependencies
 
46
    // -------------------------------------------------------------------------
 
47
 
 
48
    private I18n i18n;
 
49
 
 
50
    public void setI18n( I18n i18n )
 
51
    {
 
52
        this.i18n = i18n;
 
53
    }
 
54
 
 
55
    private OrganisationUnitService organisationUnitService;
 
56
 
 
57
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
58
    {
 
59
        this.organisationUnitService = organisationUnitService;
 
60
    }
 
61
 
 
62
    private OrganisationUnitSelectionManager selectionManager;
 
63
 
 
64
    public void setSelectionManager( OrganisationUnitSelectionManager selectionManager )
 
65
    {
 
66
        this.selectionManager = selectionManager;
 
67
    }
 
68
 
 
69
    // -------------------------------------------------------------------------
 
70
    // Input/output
 
71
    // -------------------------------------------------------------------------
 
72
 
 
73
    private Integer organisationUnitToMoveId;
 
74
 
 
75
    public void setOrganisationUnitToMoveId( Integer organisationUnitToMoveId )
 
76
    {
 
77
        this.organisationUnitToMoveId = organisationUnitToMoveId;
 
78
    }
 
79
 
 
80
    private Integer newParentOrganisationUnitId;
 
81
 
 
82
    public void setNewParentOrganisationUnitId( Integer newParentOrganisationUnitId )
 
83
    {
 
84
        this.newParentOrganisationUnitId = newParentOrganisationUnitId;
 
85
    }
 
86
 
 
87
    private String message;
 
88
 
 
89
    public String getMessage()
 
90
    {
 
91
        return message;
 
92
    }
 
93
 
 
94
    // -------------------------------------------------------------------------
 
95
    // Action implementation
 
96
    // -------------------------------------------------------------------------
 
97
 
 
98
    public String execute()
 
99
    {
 
100
        // ---------------------------------------------------------------------
 
101
        // Get organisation unit to move
 
102
        // ---------------------------------------------------------------------
 
103
 
 
104
        OrganisationUnit organisationUnitToMove = organisationUnitService.getOrganisationUnit( organisationUnitToMoveId
 
105
            .intValue() );
 
106
 
 
107
        // ---------------------------------------------------------------------
 
108
        // New parent is null => move to root
 
109
        // ---------------------------------------------------------------------
 
110
 
 
111
        if ( newParentOrganisationUnitId == null )
 
112
        {
 
113
            // -----------------------------------------------------------------
 
114
            // Get tree root parent
 
115
            // -----------------------------------------------------------------
 
116
 
 
117
            OrganisationUnit treeRootParent = selectionManager.getRootOrganisationUnitsParent();
 
118
 
 
119
            // -----------------------------------------------------------------
 
120
            // Check root
 
121
            // -----------------------------------------------------------------
 
122
 
 
123
            if ( organisationUnitToMove.getParent() == null )
 
124
            {
 
125
                message = i18n.getString( "the_selected_organisation_unit_is_already_a_root" );
 
126
 
 
127
                return INPUT;
 
128
            }
 
129
            else if ( treeRootParent != null && organisationUnitToMove.getParent().getId() == treeRootParent.getId() )
 
130
            {
 
131
                message = i18n.getString( "the_selected_organisation_unit_is_already_a_root" );
 
132
 
 
133
                return INPUT;
 
134
            }
 
135
            else
 
136
            {
 
137
                message = i18n.getString( "the_selected_organisation_unit_will_be_moved_to_root" );
 
138
 
 
139
                return SUCCESS;
 
140
            }
 
141
        }
 
142
 
 
143
        // ---------------------------------------------------------------------
 
144
        // Get new parent organisation unit
 
145
        // ---------------------------------------------------------------------
 
146
 
 
147
        OrganisationUnit newParentOrganisationUnit = organisationUnitService
 
148
            .getOrganisationUnit( newParentOrganisationUnitId.intValue() );
 
149
 
 
150
        // ---------------------------------------------------------------------
 
151
        // Check if equal
 
152
        // ---------------------------------------------------------------------
 
153
 
 
154
        if ( organisationUnitToMove.getId() == newParentOrganisationUnit.getId() )
 
155
        {
 
156
            message = i18n.getString( "an_organisation_unit_cannot_be_moved_to_itself" );
 
157
 
 
158
            return INPUT;
 
159
        }
 
160
 
 
161
        // ---------------------------------------------------------------------
 
162
        // Check if path in wrong direction
 
163
        // ---------------------------------------------------------------------
 
164
 
 
165
        if ( pathFromParentToChild( organisationUnitToMove, newParentOrganisationUnit ) )
 
166
        {
 
167
            message = i18n.getString( "an_org_unit_cannot_be_moved_to_be_its_own_child" );
 
168
 
 
169
            return INPUT;
 
170
        }
 
171
 
 
172
        message = i18n.getString( "the_new_parent_org_unit_is_approved" );
 
173
 
 
174
        return SUCCESS;
 
175
    }
 
176
 
 
177
    // -------------------------------------------------------------------------
 
178
    // Support methods
 
179
    // -------------------------------------------------------------------------
 
180
 
 
181
    private boolean pathFromParentToChild( OrganisationUnit parent, OrganisationUnit child )
 
182
    {
 
183
        OrganisationUnit tmp = child.getParent();
 
184
 
 
185
        while ( tmp != null )
 
186
        {
 
187
            if ( tmp.getId() == parent.getId() )
 
188
            {
 
189
                return true;
 
190
            }
 
191
 
 
192
            tmp = tmp.getParent();
 
193
        }
 
194
 
 
195
        return false;
 
196
    }
 
197
}