1
package org.hisp.dhis.dataset.action.dataentryform;
4
* Copyright (c) 2004-2007, 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.regex.Matcher;
31
import java.util.regex.Pattern;
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;
38
import com.opensymphony.xwork.Action;
41
* @author Bharath Kumar
44
public class SaveDataEntryFormAction
47
// -------------------------------------------------------------------------
49
// -------------------------------------------------------------------------
51
private DataEntryFormService dataEntryFormService;
53
public void setDataEntryFormService( DataEntryFormService dataEntryFormService )
55
this.dataEntryFormService = dataEntryFormService;
58
private DataSetService dataSetService;
60
public void setDataSetService( DataSetService dataSetService )
62
this.dataSetService = dataSetService;
65
// -------------------------------------------------------------------------
67
// -------------------------------------------------------------------------
69
private int dataSetIdField;
71
public void setDataSetIdField( int dataSetIdField )
73
this.dataSetIdField = dataSetIdField;
76
private String nameField;
78
public void setNameField( String nameField )
80
this.nameField = nameField;
83
private String designTextarea;
85
public void setDesignTextarea( String designTextarea )
87
this.designTextarea = designTextarea;
90
// -------------------------------------------------------------------------
92
// -------------------------------------------------------------------------
94
public String execute()
97
DataSet dataSet = dataSetService.getDataSet( dataSetIdField );
99
DataEntryForm dataEntryForm = dataEntryFormService.getDataEntryFormByDataSet( dataSet );
101
if ( dataEntryForm == null )
103
dataEntryForm = new DataEntryForm( nameField, prepareDataEntryFormCode( designTextarea ), dataSet );
104
dataEntryFormService.addDataEntryForm( dataEntryForm );
108
dataEntryForm.setName( nameField );
109
dataEntryForm.setHtmlCode( prepareDataEntryFormCode( designTextarea ) );
110
dataEntryFormService.updateDataEntryForm( dataEntryForm );
116
private String prepareDataEntryFormCode( String dataEntryFormCode )
118
String preparedCode = dataEntryFormCode;
120
preparedCode = prepareDataEntryFormInputs( preparedCode );
125
private String prepareDataEntryFormInputs( String preparedCode )
127
// ---------------------------------------------------------------------
128
// Buffer to contain the final result.
129
// ---------------------------------------------------------------------
131
StringBuffer sb = new StringBuffer();
133
// ---------------------------------------------------------------------
134
// Pattern to match data elements in the HTML code.
135
// ---------------------------------------------------------------------
137
Pattern patDataElement = Pattern.compile( "(<input.*?)[/]?>" );
138
Matcher matDataElement = patDataElement.matcher( preparedCode );
140
// ---------------------------------------------------------------------
141
// Iterate through all matching data element fields.
142
// ---------------------------------------------------------------------
144
boolean result = matDataElement.find();
148
// -----------------------------------------------------------------
149
// Get input HTML code (HTML input field code).
150
// -----------------------------------------------------------------
152
String dataElementCode = matDataElement.group( 1 );
154
// -----------------------------------------------------------------
155
// Pattern to extract data element name from data element field
156
// -----------------------------------------------------------------
158
Pattern patDataElementName = Pattern.compile( "value=\"\\[ (.*) \\]\"" );
159
Matcher matDataElementName = patDataElementName.matcher( dataElementCode );
161
Pattern patTitle = Pattern.compile( "title=\"-- (.*) --\"" );
162
Matcher matTitle = patTitle.matcher( dataElementCode );
164
if ( matDataElementName.find() && matDataElementName.groupCount() > 0 )
166
String temp = "[ " + matDataElementName.group( 1 ) + " ]";
167
dataElementCode = dataElementCode.replace( temp, "" );
169
if ( matTitle.find() && matTitle.groupCount() > 0 )
171
temp = "-- " + matTitle.group( 1 ) + " --";
172
dataElementCode = dataElementCode.replace( temp, "" );
175
// -------------------------------------------------------------
176
// Appends dataElementCode
177
// -------------------------------------------------------------
179
String appendCode = dataElementCode;
181
matDataElement.appendReplacement( sb, appendCode );
184
// -----------------------------------------------------------------
185
// Go to next data entry field
186
// -----------------------------------------------------------------
188
result = matDataElement.find();
191
// -----------------------------------------------------------------
192
// Add remaining code (after the last match), and return formatted code.
193
// -----------------------------------------------------------------
195
matDataElement.appendTail( sb );
197
return sb.toString();