~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/organisationunitgroup/ValidateOrganisationUnitGroupAction.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.organisationunitgroup;
 
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.Collection;
 
31
 
 
32
import org.hisp.dhis.i18n.I18n;
 
33
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
34
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 
35
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 
36
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
 
37
import org.hisp.dhis.oust.manager.SelectionTreeManager;
 
38
 
 
39
import com.opensymphony.xwork.ActionSupport;
 
40
 
 
41
/**
 
42
 * @author Torgeir Lorange Ostby
 
43
 * @version $Id: ValidateOrganisationUnitGroupAction.java 1898 2006-09-22 12:06:56Z torgeilo $
 
44
 */
 
45
public class ValidateOrganisationUnitGroupAction
 
46
    extends ActionSupport
 
47
{
 
48
    // -------------------------------------------------------------------------
 
49
    // Dependencies
 
50
    // -------------------------------------------------------------------------
 
51
 
 
52
    private I18n i18n;
 
53
 
 
54
    public void setI18n( I18n i18n )
 
55
    {
 
56
        this.i18n = i18n;
 
57
    }
 
58
 
 
59
    private OrganisationUnitGroupService organisationUnitGroupService;
 
60
 
 
61
    public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
 
62
    {
 
63
        this.organisationUnitGroupService = organisationUnitGroupService;
 
64
    }
 
65
 
 
66
    private SelectionTreeManager selectionTreeManager;
 
67
 
 
68
    public void setSelectionTreeManager( SelectionTreeManager selectionTreeManager )
 
69
    {
 
70
        this.selectionTreeManager = selectionTreeManager;
 
71
    }
 
72
 
 
73
    // -------------------------------------------------------------------------
 
74
    // Input
 
75
    // -------------------------------------------------------------------------
 
76
 
 
77
    private Integer id;
 
78
 
 
79
    public void setId( Integer id )
 
80
    {
 
81
        this.id = id;
 
82
    }
 
83
 
 
84
    private String name;
 
85
 
 
86
    public void setName( String name )
 
87
    {
 
88
        this.name = name;
 
89
    }
 
90
 
 
91
    // -------------------------------------------------------------------------
 
92
    // Output
 
93
    // -------------------------------------------------------------------------
 
94
 
 
95
    private String message;
 
96
 
 
97
    public String getMessage()
 
98
    {
 
99
        return message;
 
100
    }
 
101
 
 
102
    // -------------------------------------------------------------------------
 
103
    // Action implementation
 
104
    // -------------------------------------------------------------------------
 
105
 
 
106
    public String execute()
 
107
    {
 
108
        // ---------------------------------------------------------------------
 
109
        // Validate values
 
110
        // ---------------------------------------------------------------------
 
111
 
 
112
        if ( name == null )
 
113
        {
 
114
            message = i18n.getString( "please_specify_a_name" );
 
115
 
 
116
            return INPUT;
 
117
        }
 
118
        else
 
119
        {
 
120
            name = name.trim();
 
121
 
 
122
            if ( name.length() == 0 )
 
123
            {
 
124
                message = i18n.getString( "please_specify_a_name" );
 
125
 
 
126
                return INPUT;
 
127
            }
 
128
 
 
129
            OrganisationUnitGroup match = organisationUnitGroupService.getOrganisationUnitGroupByName( name );
 
130
 
 
131
            if ( match != null && (id == null || match.getId() != id) )
 
132
            {
 
133
                message = i18n.getString( "name_is_already_in_use" );
 
134
 
 
135
                return INPUT;
 
136
            }
 
137
        }
 
138
 
 
139
        // ---------------------------------------------------------------------
 
140
        // When updating a group which is a member of an exclusive group set,
 
141
        // any selected units can not be a member of another group in the
 
142
        // group set.
 
143
        // ---------------------------------------------------------------------
 
144
 
 
145
        // No need to check this if it's a new organisations group.
 
146
        if ( id == null )
 
147
        {
 
148
            message = "Everything's ok";
 
149
 
 
150
            return SUCCESS;
 
151
        }
 
152
 
 
153
        OrganisationUnitGroup organisationUnitGroup = organisationUnitGroupService.getOrganisationUnitGroup( id
 
154
            .intValue() );
 
155
 
 
156
        Collection<OrganisationUnitGroupSet> exclusiveGroupSets = organisationUnitGroupService
 
157
            .getExclusiveOrganisationUnitGroupSetsContainingGroup( organisationUnitGroup );
 
158
 
 
159
        if ( exclusiveGroupSets != null && exclusiveGroupSets.size() > 0 )
 
160
        {
 
161
            Collection<OrganisationUnit> selectedUnits = selectionTreeManager.getSelectedOrganisationUnits();
 
162
 
 
163
            for ( OrganisationUnitGroupSet groupSet : exclusiveGroupSets )
 
164
            {
 
165
                for ( OrganisationUnitGroup group : groupSet.getOrganisationUnitGroups() )
 
166
                {
 
167
                    for ( OrganisationUnit unit : selectedUnits )
 
168
                    {
 
169
                        if ( group.getMembers().contains( unit ) && group.getId() != id )
 
170
                        {
 
171
                            message = unit.getShortName() + " "
 
172
                                + i18n.getString( "can_not_be_a_member_because_member_of" ) + " " + group.getName()
 
173
                                + " " + i18n.getString( "which_is_a_member_of_the_same_exclusive_group_set" ) + " "
 
174
                                + groupSet.getName() + " " + i18n.getString( "as_the_current_group" );
 
175
 
 
176
                            return INPUT;
 
177
                        }
 
178
                    }
 
179
                }
 
180
            }
 
181
        }
 
182
 
 
183
        message = "Everything's ok";
 
184
 
 
185
        return SUCCESS;
 
186
    }
 
187
}