1
package org.hisp.dhis.oum.action.hierarchy;
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 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;
35
import com.opensymphony.xwork.ActionSupport;
38
* @author Torgeir Lorange Ostby
39
* @version $Id: ValidateNewParentOrganisationUnitAction.java 1232 2006-01-30 06:29:30Z torgeilo $
41
public class ValidateNewParentOrganisationUnitAction
44
// -------------------------------------------------------------------------
46
// -------------------------------------------------------------------------
50
public void setI18n( I18n i18n )
55
private OrganisationUnitService organisationUnitService;
57
public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
59
this.organisationUnitService = organisationUnitService;
62
private OrganisationUnitSelectionManager selectionManager;
64
public void setSelectionManager( OrganisationUnitSelectionManager selectionManager )
66
this.selectionManager = selectionManager;
69
// -------------------------------------------------------------------------
71
// -------------------------------------------------------------------------
73
private Integer organisationUnitToMoveId;
75
public void setOrganisationUnitToMoveId( Integer organisationUnitToMoveId )
77
this.organisationUnitToMoveId = organisationUnitToMoveId;
80
private Integer newParentOrganisationUnitId;
82
public void setNewParentOrganisationUnitId( Integer newParentOrganisationUnitId )
84
this.newParentOrganisationUnitId = newParentOrganisationUnitId;
87
private String message;
89
public String getMessage()
94
// -------------------------------------------------------------------------
95
// Action implementation
96
// -------------------------------------------------------------------------
98
public String execute()
100
// ---------------------------------------------------------------------
101
// Get organisation unit to move
102
// ---------------------------------------------------------------------
104
OrganisationUnit organisationUnitToMove = organisationUnitService.getOrganisationUnit( organisationUnitToMoveId
107
// ---------------------------------------------------------------------
108
// New parent is null => move to root
109
// ---------------------------------------------------------------------
111
if ( newParentOrganisationUnitId == null )
113
// -----------------------------------------------------------------
114
// Get tree root parent
115
// -----------------------------------------------------------------
117
OrganisationUnit treeRootParent = selectionManager.getRootOrganisationUnitsParent();
119
// -----------------------------------------------------------------
121
// -----------------------------------------------------------------
123
if ( organisationUnitToMove.getParent() == null )
125
message = i18n.getString( "the_selected_organisation_unit_is_already_a_root" );
129
else if ( treeRootParent != null && organisationUnitToMove.getParent().getId() == treeRootParent.getId() )
131
message = i18n.getString( "the_selected_organisation_unit_is_already_a_root" );
137
message = i18n.getString( "the_selected_organisation_unit_will_be_moved_to_root" );
143
// ---------------------------------------------------------------------
144
// Get new parent organisation unit
145
// ---------------------------------------------------------------------
147
OrganisationUnit newParentOrganisationUnit = organisationUnitService
148
.getOrganisationUnit( newParentOrganisationUnitId.intValue() );
150
// ---------------------------------------------------------------------
152
// ---------------------------------------------------------------------
154
if ( organisationUnitToMove.getId() == newParentOrganisationUnit.getId() )
156
message = i18n.getString( "an_organisation_unit_cannot_be_moved_to_itself" );
161
// ---------------------------------------------------------------------
162
// Check if path in wrong direction
163
// ---------------------------------------------------------------------
165
if ( pathFromParentToChild( organisationUnitToMove, newParentOrganisationUnit ) )
167
message = i18n.getString( "an_org_unit_cannot_be_moved_to_be_its_own_child" );
172
message = i18n.getString( "the_new_parent_org_unit_is_approved" );
177
// -------------------------------------------------------------------------
179
// -------------------------------------------------------------------------
181
private boolean pathFromParentToChild( OrganisationUnit parent, OrganisationUnit child )
183
OrganisationUnit tmp = child.getParent();
185
while ( tmp != null )
187
if ( tmp.getId() == parent.getId() )
192
tmp = tmp.getParent();