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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/RunValidationAction.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.validationrule.action;
 
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.ArrayList;
 
31
import java.util.Collection;
 
32
import java.util.Collections;
 
33
import java.util.HashSet;
 
34
import java.util.List;
 
35
 
 
36
import org.hisp.dhis.i18n.I18nFormat;
 
37
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
38
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
39
import org.hisp.dhis.oust.manager.SelectionTreeManager;
 
40
import org.hisp.dhis.source.Source;
 
41
import org.hisp.dhis.util.SessionUtils;
 
42
import org.hisp.dhis.validation.ValidationResult;
 
43
import org.hisp.dhis.validation.ValidationRuleGroup;
 
44
import org.hisp.dhis.validation.ValidationRuleService;
 
45
import org.hisp.dhis.validation.comparator.ValidationResultComparator;
 
46
 
 
47
import com.opensymphony.xwork.ActionSupport;
 
48
 
 
49
/**
 
50
 * @author Margrethe Store
 
51
 * @author Lars Helge Overland
 
52
 * @version $Id: RunValidationAction.java 6059 2008-10-28 15:15:34Z larshelg $
 
53
 */
 
54
public class RunValidationAction
 
55
    extends ActionSupport
 
56
{
 
57
    private static final String KEY_VALIDATIONRESULT = "validationResult";
 
58
    
 
59
    // -------------------------------------------------------------------------
 
60
    // Dependencies
 
61
    // -------------------------------------------------------------------------
 
62
 
 
63
    private ValidationRuleService validationRuleService;
 
64
 
 
65
    public void setValidationRuleService( ValidationRuleService validationRuleService )
 
66
    {
 
67
        this.validationRuleService = validationRuleService;
 
68
    }
 
69
 
 
70
    private I18nFormat format;
 
71
 
 
72
    public void setFormat( I18nFormat format )
 
73
    {
 
74
        this.format = format;
 
75
    }
 
76
 
 
77
    private SelectionTreeManager selectionTreeManager;
 
78
 
 
79
    public void setSelectionTreeManager( SelectionTreeManager selectionTreeManager )
 
80
    {
 
81
        this.selectionTreeManager = selectionTreeManager;
 
82
    }
 
83
 
 
84
    private OrganisationUnitService organisationUnitService;
 
85
 
 
86
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
87
    {
 
88
        this.organisationUnitService = organisationUnitService;
 
89
    }
 
90
 
 
91
    // -------------------------------------------------------------------------
 
92
    // Input/output
 
93
    // -------------------------------------------------------------------------
 
94
 
 
95
    private String startDate;
 
96
 
 
97
    public String getStartDate()
 
98
    {
 
99
        return startDate;
 
100
    }
 
101
 
 
102
    public void setStartDate( String startDate )
 
103
    {
 
104
        this.startDate = startDate;
 
105
    }
 
106
 
 
107
    private String endDate;
 
108
 
 
109
    public String getEndDate()
 
110
    {
 
111
        return endDate;
 
112
    }
 
113
 
 
114
    public void setEndDate( String endDate )
 
115
    {
 
116
        this.endDate = endDate;
 
117
    }
 
118
    
 
119
    private Integer validationRuleGroupId;
 
120
 
 
121
    public void setValidationRuleGroupId( Integer validationRuleGroupId )
 
122
    {
 
123
        this.validationRuleGroupId = validationRuleGroupId;
 
124
    }
 
125
    
 
126
    private Boolean includeChildren;
 
127
 
 
128
    public void setIncludeChildren( Boolean includeChildren )
 
129
    {
 
130
        this.includeChildren = includeChildren;
 
131
    }
 
132
 
 
133
    private List<ValidationResult> validationResults;
 
134
 
 
135
    public List<ValidationResult> getValidationResults()
 
136
    {
 
137
        return validationResults;
 
138
    }
 
139
 
 
140
    // -------------------------------------------------------------------------
 
141
    // Execute
 
142
    // -------------------------------------------------------------------------
 
143
 
 
144
    public String execute()
 
145
    {
 
146
        Collection<? extends Source> sources = selectionTreeManager.getSelectedOrganisationUnits();
 
147
 
 
148
        if ( includeChildren )
 
149
        {
 
150
            Collection<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>();
 
151
 
 
152
            for ( Source source : sources )
 
153
            {
 
154
                organisationUnits.addAll( organisationUnitService.getOrganisationUnitWithChildren( source.getId() ) );
 
155
            }
 
156
 
 
157
            sources = organisationUnits;
 
158
        }
 
159
 
 
160
        if ( validationRuleGroupId == -1 )
 
161
        {
 
162
            validationResults = new ArrayList<ValidationResult>( validationRuleService.validate( 
 
163
                format.parseDate( startDate ), format.parseDate( endDate ), sources  ) );
 
164
        }
 
165
        else
 
166
        {
 
167
            ValidationRuleGroup group = validationRuleService.getValidationRuleGroup( validationRuleGroupId );
 
168
            
 
169
            validationResults = new ArrayList<ValidationResult>( validationRuleService.validate( 
 
170
                format.parseDate( startDate ), format.parseDate( endDate ), sources, group ) );
 
171
        }
 
172
 
 
173
        Collections.sort( validationResults, new ValidationResultComparator() );
 
174
        
 
175
        SessionUtils.setSessionVar( KEY_VALIDATIONRESULT, validationResults );
 
176
        
 
177
        return SUCCESS;
 
178
    }
 
179
 
 
180
}