~dhis2-devs-core/dhis2/datamart

« back to all changes in this revision

Viewing changes to local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/organisationunit/action/AddMultiOrganisationUnitAction.java

  • Committer: Hieu
  • Date: 2012-08-21 08:32:18 UTC
  • Revision ID: hieu.hispvietnam@gmail.com-20120821083218-8xxonsih0i9qwo5g
local vn - Implemented a new GUI for adding multiple of organisation unit based on the org-unit-prototypes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.reportsheet.organisationunit.action;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2012, 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
import java.util.Date;
 
32
import java.util.HashSet;
 
33
import java.util.List;
 
34
 
 
35
import org.hisp.dhis.attribute.AttributeService;
 
36
import org.hisp.dhis.dataset.DataSet;
 
37
import org.hisp.dhis.dataset.DataSetService;
 
38
import org.hisp.dhis.i18n.I18nFormat;
 
39
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
40
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 
41
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 
42
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
43
import org.hisp.dhis.organisationunitprototype.OrganisationUnitPrototype;
 
44
import org.hisp.dhis.organisationunitprototype.OrganisationUnitPrototypeService;
 
45
import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
 
46
import org.hisp.dhis.system.util.AttributeUtils;
 
47
import org.springframework.beans.factory.annotation.Autowired;
 
48
 
 
49
import com.opensymphony.xwork2.Action;
 
50
 
 
51
/**
 
52
 * @author Dang Duy Hieu
 
53
 * @version $Id$
 
54
 */
 
55
public class AddMultiOrganisationUnitAction
 
56
    implements Action
 
57
{
 
58
    private static final String SEPERATE = " - ";
 
59
 
 
60
    // -------------------------------------------------------------------------
 
61
    // Dependencies
 
62
    // -------------------------------------------------------------------------
 
63
 
 
64
    private I18nFormat format;
 
65
 
 
66
    public void setFormat( I18nFormat format )
 
67
    {
 
68
        this.format = format;
 
69
    }
 
70
 
 
71
    @Autowired
 
72
    private OrganisationUnitPrototypeService organisationUnitPrototypeService;
 
73
 
 
74
    @Autowired
 
75
    private OrganisationUnitService organisationUnitService;
 
76
 
 
77
    @Autowired
 
78
    private OrganisationUnitGroupService organisationUnitGroupService;
 
79
 
 
80
    @Autowired
 
81
    private OrganisationUnitSelectionManager selectionManager;
 
82
 
 
83
    @Autowired
 
84
    private DataSetService dataSetService;
 
85
 
 
86
    @Autowired
 
87
    private AttributeService attributeService;
 
88
 
 
89
    // -------------------------------------------------------------------------
 
90
    // Input & Output
 
91
    // -------------------------------------------------------------------------
 
92
 
 
93
    private String openingDate;
 
94
 
 
95
    public void setOpeningDate( String openingDate )
 
96
    {
 
97
        this.openingDate = openingDate;
 
98
    }
 
99
 
 
100
    private boolean active;
 
101
 
 
102
    public void setActive( boolean active )
 
103
    {
 
104
        this.active = active;
 
105
    }
 
106
 
 
107
    private Collection<String> selectedList = new HashSet<String>();
 
108
 
 
109
    public void setSelectedList( Collection<String> selectedList )
 
110
    {
 
111
        this.selectedList = selectedList;
 
112
    }
 
113
 
 
114
    private Collection<Integer> dataSets = new HashSet<Integer>();
 
115
 
 
116
    public void setDataSets( Collection<Integer> dataSets )
 
117
    {
 
118
        this.dataSets = dataSets;
 
119
    }
 
120
 
 
121
    private Collection<Integer> selectedGroups = new HashSet<Integer>();
 
122
 
 
123
    public void setSelectedGroups( Collection<Integer> selectedGroups )
 
124
    {
 
125
        this.selectedGroups = selectedGroups;
 
126
    }
 
127
 
 
128
    private Integer organisationUnitId;
 
129
 
 
130
    public Integer getOrganisationUnitId()
 
131
    {
 
132
        return organisationUnitId;
 
133
    }
 
134
 
 
135
    private List<String> jsonAttributeValues;
 
136
 
 
137
    public void setJsonAttributeValues( List<String> jsonAttributeValues )
 
138
    {
 
139
        this.jsonAttributeValues = jsonAttributeValues;
 
140
    }
 
141
 
 
142
    // -------------------------------------------------------------------------
 
143
    // Action implementation
 
144
    // -------------------------------------------------------------------------
 
145
 
 
146
    public String execute()
 
147
        throws Exception
 
148
    {
 
149
        Date date = format.parseDate( openingDate );
 
150
 
 
151
        // ---------------------------------------------------------------------
 
152
        // Get parent
 
153
        // ---------------------------------------------------------------------
 
154
 
 
155
        OrganisationUnit parent = selectionManager.getSelectedOrganisationUnit();
 
156
 
 
157
        if ( parent == null )
 
158
        {
 
159
            // -----------------------------------------------------------------
 
160
            // If no unit is selected, the parent is the parent of the roots
 
161
            // -----------------------------------------------------------------
 
162
 
 
163
            parent = selectionManager.getRootOrganisationUnitsParent();
 
164
        }
 
165
 
 
166
        Collection<DataSet> dataSetList = new HashSet<DataSet>( dataSetService.getDataSets( dataSets ) );
 
167
 
 
168
        Collection<OrganisationUnitGroup> unitGroupList = new HashSet<OrganisationUnitGroup>(
 
169
            organisationUnitGroupService.getOrganisationUnitGroups( selectedGroups ) );
 
170
 
 
171
        // ---------------------------------------------------------------------
 
172
        // Create organization unit
 
173
        // ---------------------------------------------------------------------
 
174
 
 
175
        String extraName = ( parent.getCode() == null ? String.valueOf( parent.getId() ) : parent.getCode() );
 
176
 
 
177
        for ( String id : selectedList )
 
178
        {
 
179
            OrganisationUnitPrototype unitPrototype = organisationUnitPrototypeService
 
180
                .getOrganisationUnitPrototype( Integer.parseInt( id ) );
 
181
 
 
182
            if ( unitPrototype != null )
 
183
            {
 
184
                OrganisationUnit organisationUnit = new OrganisationUnit( unitPrototype.getDisplayName() + SEPERATE
 
185
                    + extraName, unitPrototype.getDisplayShortName(), null, date, null, active, null );
 
186
 
 
187
                organisationUnit.setParent( parent );
 
188
 
 
189
                if ( parent != null )
 
190
                {
 
191
                    parent.getChildren().add( organisationUnit );
 
192
                }
 
193
 
 
194
                if ( jsonAttributeValues != null )
 
195
                {
 
196
                    AttributeUtils.updateAttributeValuesFromJson( organisationUnit.getAttributeValues(),
 
197
                        jsonAttributeValues, attributeService );
 
198
                }
 
199
 
 
200
                // ---------------------------------------------------------------------
 
201
                // Must persist org-unit before adding data sets because
 
202
                // association are
 
203
                // updated on both sides (and this side is inverse)
 
204
                // ---------------------------------------------------------------------
 
205
 
 
206
                organisationUnitId = organisationUnitService.addOrganisationUnit( organisationUnit );
 
207
 
 
208
                for ( DataSet ds : dataSetList )
 
209
                {
 
210
                    organisationUnit.addDataSet( ds );
 
211
                }
 
212
 
 
213
                for ( OrganisationUnitGroup group : unitGroupList )
 
214
                {
 
215
                    if ( group != null )
 
216
                    {
 
217
                        group.addOrganisationUnit( organisationUnit );
 
218
                        organisationUnitGroupService.updateOrganisationUnitGroup( group );
 
219
                    }
 
220
                }
 
221
 
 
222
                organisationUnitService.updateOrganisationUnit( organisationUnit );
 
223
            }
 
224
        }
 
225
 
 
226
        return SUCCESS;
 
227
    }
 
228
}