~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-dataset/src/main/java/org/hisp/dhis/dataset/action/dataentryform/SaveDataEntryFormAction.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.dataset.action.dataentryform;
 
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.regex.Matcher;
 
31
import java.util.regex.Pattern;
 
32
 
 
33
import org.hisp.dhis.dataset.DataEntryForm;
 
34
import org.hisp.dhis.dataset.DataEntryFormService;
 
35
import org.hisp.dhis.dataset.DataSet;
 
36
import org.hisp.dhis.dataset.DataSetService;
 
37
 
 
38
import com.opensymphony.xwork.Action;
 
39
 
 
40
/**
 
41
 * @author Bharath Kumar
 
42
 * @version $Id$
 
43
 */
 
44
public class SaveDataEntryFormAction
 
45
    implements Action
 
46
{
 
47
    // -------------------------------------------------------------------------
 
48
    // Dependencies
 
49
    // -------------------------------------------------------------------------
 
50
    
 
51
    private DataEntryFormService dataEntryFormService;
 
52
 
 
53
    public void setDataEntryFormService( DataEntryFormService dataEntryFormService )
 
54
    {
 
55
        this.dataEntryFormService = dataEntryFormService;
 
56
    }
 
57
 
 
58
    private DataSetService dataSetService;
 
59
 
 
60
    public void setDataSetService( DataSetService dataSetService )
 
61
    {
 
62
        this.dataSetService = dataSetService;
 
63
    }
 
64
 
 
65
    // -------------------------------------------------------------------------
 
66
    // Getters & Setters
 
67
    // -------------------------------------------------------------------------
 
68
    
 
69
    private int dataSetIdField;
 
70
 
 
71
    public void setDataSetIdField( int dataSetIdField )
 
72
    {
 
73
        this.dataSetIdField = dataSetIdField;
 
74
    }
 
75
 
 
76
    private String nameField;
 
77
 
 
78
    public void setNameField( String nameField )
 
79
    {
 
80
        this.nameField = nameField;
 
81
    }
 
82
 
 
83
    private String designTextarea;
 
84
 
 
85
    public void setDesignTextarea( String designTextarea )
 
86
    {
 
87
        this.designTextarea = designTextarea;
 
88
    }
 
89
 
 
90
    // -------------------------------------------------------------------------
 
91
    // Execute
 
92
    // -------------------------------------------------------------------------
 
93
    
 
94
    public String execute()
 
95
        throws Exception
 
96
    {
 
97
        DataSet dataSet = dataSetService.getDataSet( dataSetIdField );
 
98
 
 
99
        DataEntryForm dataEntryForm = dataEntryFormService.getDataEntryFormByDataSet( dataSet );
 
100
 
 
101
        if ( dataEntryForm == null )
 
102
        {
 
103
            dataEntryForm = new DataEntryForm( nameField, prepareDataEntryFormCode( designTextarea ), dataSet );
 
104
            dataEntryFormService.addDataEntryForm( dataEntryForm );
 
105
        }
 
106
        else
 
107
        {
 
108
            dataEntryForm.setName( nameField );
 
109
            dataEntryForm.setHtmlCode( prepareDataEntryFormCode( designTextarea ) );
 
110
            dataEntryFormService.updateDataEntryForm( dataEntryForm );
 
111
        }
 
112
 
 
113
        return SUCCESS;
 
114
    }
 
115
 
 
116
    private String prepareDataEntryFormCode( String dataEntryFormCode )
 
117
    {
 
118
        String preparedCode = dataEntryFormCode;
 
119
 
 
120
        preparedCode = prepareDataEntryFormInputs( preparedCode );
 
121
        
 
122
        return preparedCode;
 
123
    }
 
124
 
 
125
    private String prepareDataEntryFormInputs( String preparedCode )
 
126
    {
 
127
        // ---------------------------------------------------------------------
 
128
        // Buffer to contain the final result.
 
129
        // ---------------------------------------------------------------------
 
130
        
 
131
        StringBuffer sb = new StringBuffer();
 
132
        
 
133
        // ---------------------------------------------------------------------
 
134
        // Pattern to match data elements in the HTML code.
 
135
        // ---------------------------------------------------------------------
 
136
 
 
137
        Pattern patDataElement = Pattern.compile( "(<input.*?)[/]?>" );
 
138
        Matcher matDataElement = patDataElement.matcher( preparedCode );
 
139
 
 
140
        // ---------------------------------------------------------------------
 
141
        // Iterate through all matching data element fields.
 
142
        // ---------------------------------------------------------------------
 
143
        
 
144
        boolean result = matDataElement.find();
 
145
        
 
146
        while ( result )
 
147
        {
 
148
            // -----------------------------------------------------------------
 
149
            // Get input HTML code (HTML input field code).
 
150
            // -----------------------------------------------------------------
 
151
            
 
152
            String dataElementCode = matDataElement.group( 1 );
 
153
            
 
154
            // -----------------------------------------------------------------
 
155
            // Pattern to extract data element name from data element field
 
156
            // -----------------------------------------------------------------
 
157
            
 
158
            Pattern patDataElementName = Pattern.compile( "value=\"\\[ (.*) \\]\"" );
 
159
            Matcher matDataElementName = patDataElementName.matcher( dataElementCode );
 
160
 
 
161
            Pattern patTitle = Pattern.compile( "title=\"-- (.*) --\"" );
 
162
            Matcher matTitle = patTitle.matcher( dataElementCode );
 
163
       
 
164
            if ( matDataElementName.find() && matDataElementName.groupCount() > 0 )
 
165
            {
 
166
                String temp = "[ " + matDataElementName.group( 1 ) + " ]";
 
167
                dataElementCode = dataElementCode.replace( temp, "" );
 
168
 
 
169
                if ( matTitle.find() && matTitle.groupCount() > 0 )
 
170
                {
 
171
                    temp = "-- " + matTitle.group( 1 ) + " --";
 
172
                    dataElementCode = dataElementCode.replace( temp, "" );
 
173
                }
 
174
 
 
175
                // -------------------------------------------------------------
 
176
                // Appends dataElementCode
 
177
                // -------------------------------------------------------------
 
178
       
 
179
                String appendCode = dataElementCode;
 
180
                appendCode += "/>";
 
181
                matDataElement.appendReplacement( sb, appendCode );
 
182
            }
 
183
 
 
184
            // -----------------------------------------------------------------
 
185
            // Go to next data entry field
 
186
            // -----------------------------------------------------------------
 
187
   
 
188
            result = matDataElement.find();
 
189
        }
 
190
 
 
191
        // -----------------------------------------------------------------
 
192
        // Add remaining code (after the last match), and return formatted code.
 
193
        // -----------------------------------------------------------------
 
194
 
 
195
        matDataElement.appendTail( sb );
 
196
       
 
197
        return sb.toString();
 
198
    }    
 
199
}