1
package org.hisp.dhis.reportsheet.organisationunit.action;
4
* Copyright (c) 2004-2012, 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 java.util.Collection;
31
import java.util.Date;
32
import java.util.HashSet;
33
import java.util.List;
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;
49
import com.opensymphony.xwork2.Action;
52
* @author Dang Duy Hieu
55
public class AddMultiOrganisationUnitAction
58
private static final String SEPERATE = " - ";
60
// -------------------------------------------------------------------------
62
// -------------------------------------------------------------------------
64
private I18nFormat format;
66
public void setFormat( I18nFormat format )
72
private OrganisationUnitPrototypeService organisationUnitPrototypeService;
75
private OrganisationUnitService organisationUnitService;
78
private OrganisationUnitGroupService organisationUnitGroupService;
81
private OrganisationUnitSelectionManager selectionManager;
84
private DataSetService dataSetService;
87
private AttributeService attributeService;
89
// -------------------------------------------------------------------------
91
// -------------------------------------------------------------------------
93
private String openingDate;
95
public void setOpeningDate( String openingDate )
97
this.openingDate = openingDate;
100
private boolean active;
102
public void setActive( boolean active )
104
this.active = active;
107
private Collection<String> selectedList = new HashSet<String>();
109
public void setSelectedList( Collection<String> selectedList )
111
this.selectedList = selectedList;
114
private Collection<Integer> dataSets = new HashSet<Integer>();
116
public void setDataSets( Collection<Integer> dataSets )
118
this.dataSets = dataSets;
121
private Collection<Integer> selectedGroups = new HashSet<Integer>();
123
public void setSelectedGroups( Collection<Integer> selectedGroups )
125
this.selectedGroups = selectedGroups;
128
private Integer organisationUnitId;
130
public Integer getOrganisationUnitId()
132
return organisationUnitId;
135
private List<String> jsonAttributeValues;
137
public void setJsonAttributeValues( List<String> jsonAttributeValues )
139
this.jsonAttributeValues = jsonAttributeValues;
142
// -------------------------------------------------------------------------
143
// Action implementation
144
// -------------------------------------------------------------------------
146
public String execute()
149
Date date = format.parseDate( openingDate );
151
// ---------------------------------------------------------------------
153
// ---------------------------------------------------------------------
155
OrganisationUnit parent = selectionManager.getSelectedOrganisationUnit();
157
if ( parent == null )
159
// -----------------------------------------------------------------
160
// If no unit is selected, the parent is the parent of the roots
161
// -----------------------------------------------------------------
163
parent = selectionManager.getRootOrganisationUnitsParent();
166
Collection<DataSet> dataSetList = new HashSet<DataSet>( dataSetService.getDataSets( dataSets ) );
168
Collection<OrganisationUnitGroup> unitGroupList = new HashSet<OrganisationUnitGroup>(
169
organisationUnitGroupService.getOrganisationUnitGroups( selectedGroups ) );
171
// ---------------------------------------------------------------------
172
// Create organization unit
173
// ---------------------------------------------------------------------
175
String extraName = ( parent.getCode() == null ? String.valueOf( parent.getId() ) : parent.getCode() );
177
for ( String id : selectedList )
179
OrganisationUnitPrototype unitPrototype = organisationUnitPrototypeService
180
.getOrganisationUnitPrototype( Integer.parseInt( id ) );
182
if ( unitPrototype != null )
184
OrganisationUnit organisationUnit = new OrganisationUnit( unitPrototype.getDisplayName() + SEPERATE
185
+ extraName, unitPrototype.getDisplayShortName(), null, date, null, active, null );
187
organisationUnit.setParent( parent );
189
if ( parent != null )
191
parent.getChildren().add( organisationUnit );
194
if ( jsonAttributeValues != null )
196
AttributeUtils.updateAttributeValuesFromJson( organisationUnit.getAttributeValues(),
197
jsonAttributeValues, attributeService );
200
// ---------------------------------------------------------------------
201
// Must persist org-unit before adding data sets because
203
// updated on both sides (and this side is inverse)
204
// ---------------------------------------------------------------------
206
organisationUnitId = organisationUnitService.addOrganisationUnit( organisationUnit );
208
for ( DataSet ds : dataSetList )
210
organisationUnit.addDataSet( ds );
213
for ( OrganisationUnitGroup group : unitGroupList )
217
group.addOrganisationUnit( organisationUnit );
218
organisationUnitGroupService.updateOrganisationUnitGroup( group );
222
organisationUnitService.updateOrganisationUnit( organisationUnit );