=== added file 'local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/catalog/CatalogDataEntryService.java' --- local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/catalog/CatalogDataEntryService.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/catalog/CatalogDataEntryService.java 2012-06-14 05:58:42 +0000 @@ -0,0 +1,27 @@ +package org.hisp.dhis.coldchain.catalog; + +import java.util.Collection; +import java.util.regex.Pattern; + +import org.hisp.dhis.i18n.I18n; + +/** + * @author Mithilesh Kumar Thakur + * + * @version CatalogDataEntryService.java Jun 7, 2012 5:12:27 PM + */ +public interface CatalogDataEntryService +{ + final Pattern INPUT_PATTERN = Pattern.compile( "(", Pattern.DOTALL ); + + final Pattern IDENTIFIER_PATTERN_FIELD = Pattern.compile( "id=\"(\\d+)-(\\d+)-val\"" ); + + //-------------------------------------------------------------------------- + // ProgramDataEntryService + //-------------------------------------------------------------------------- + + String prepareDataEntryFormForCatalog( String htmlCode, Collection dataValues, String disabled, + I18n i18n, CatalogType catalogType ); + + String prepareDataEntryFormForEdit( String htmlCode ); +} === added file 'local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/catalog/DefaultCatalogDataEntryService.java' --- local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/catalog/DefaultCatalogDataEntryService.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/catalog/DefaultCatalogDataEntryService.java 2012-06-14 05:58:42 +0000 @@ -0,0 +1,1279 @@ +package org.hisp.dhis.coldchain.catalog; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang.BooleanUtils; +import org.hisp.dhis.i18n.I18n; + +/** + * @author Mithilesh Kumar Thakur + * + * @version DefaultCatalogDataEntryService.java Jun 7, 2012 5:18:09 PM + */ +public class DefaultCatalogDataEntryService implements CatalogDataEntryService +{ + + private static final String EMPTY = ""; + + private static final String CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST = "[ Catalogtype attribute does not exist ]"; + + private static final String EMPTY_VALUE_TAG = "value=\"\""; + + private static final String EMPTY_TITLE_TAG = "title=\"\""; + + + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + + private CatalogTypeAttributeService catalogTypeAttributeService; + + public void setCatalogTypeAttributeService( CatalogTypeAttributeService catalogTypeAttributeService ) + { + this.catalogTypeAttributeService = catalogTypeAttributeService; + } + + // ------------------------------------------------------------------------- + // Implementation methods + // ------------------------------------------------------------------------- + + @Override + public String prepareDataEntryFormForCatalog( String htmlCode, Collection dataValues, String disabled, + I18n i18n, CatalogType catalogType ) + { + Map> mapDataValue = new HashMap>(); + + String result = ""; + + result = populateCustomDataEntryForTextBox( htmlCode, dataValues, disabled, i18n, catalogType, mapDataValue ); + + result = populateCustomDataEntryForCOMBO( htmlCode, dataValues, disabled, i18n, catalogType, mapDataValue ); + + result = populateCustomDataEntryForDate( result, dataValues, disabled, i18n, catalogType, mapDataValue ); + + result = populateCustomDataEntryForBoolean( result, dataValues, disabled, i18n, catalogType, mapDataValue ); + + result = populateI18nStrings( result, i18n ); + + return result; + } + + + + public String prepareDataEntryFormForEdit( String htmlCode ) + { + String result = populateCustomDataEntryForDate( htmlCode ); + + result = populateCustomDataEntryForBoolean( result ); + + result = populateCustomDataEntryForTextBox( result ); + + result = populateCustomDataEntryForCOMBO( result ); + + return result; + } + + + // ------------------------------------------------------------------------- + // Supportive methods + // ------------------------------------------------------------------------- + + private String populateCustomDataEntryForTextBox( String htmlCode ) + { + // --------------------------------------------------------------------- + // Metadata code to add to HTML before outputting + // --------------------------------------------------------------------- + + StringBuffer sb = new StringBuffer(); + + // --------------------------------------------------------------------- + // Pattern to match catalogType attribute in the HTML code + // --------------------------------------------------------------------- + + Matcher inputMatcher = INPUT_PATTERN.matcher( htmlCode ); + + // --------------------------------------------------------------------- + // Iterate through all matching catalogType attribute fields + // --------------------------------------------------------------------- + + while ( inputMatcher.find() ) + { + // ----------------------------------------------------------------- + // Get HTML input field code + // ----------------------------------------------------------------- + + String catalogTypeAttributeCode = inputMatcher.group( 1 ); + + String inputHTML = inputMatcher.group(); + inputHTML = inputHTML.replace( ">", "" ); + + Matcher identifierMatcher = CatalogDataEntryService.IDENTIFIER_PATTERN_FIELD.matcher( catalogTypeAttributeCode ); + + if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 ) + { + // ------------------------------------------------------------- + // Get catalogType attribute ID of catalogType attribute + // ------------------------------------------------------------- + + int catalogTypeAttributeId = Integer.parseInt( identifierMatcher.group( 2 ) ); + + CatalogTypeAttribute catalogTypeAttribute = catalogTypeAttributeService.getCatalogTypeAttribute( catalogTypeAttributeId ); + + if ( catalogTypeAttribute != null && (!CatalogTypeAttribute.TYPE_INT.equalsIgnoreCase( catalogTypeAttribute.getValueType() ) && !CatalogTypeAttribute.TYPE_STRING.equalsIgnoreCase( catalogTypeAttribute.getValueType() )) ) + { + continue; + } + + + String displayValue = ( catalogTypeAttribute == null ) ? " value=\"" + CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST + "\" " + : " value=\"[ " + catalogTypeAttribute.getName() + " ]\""; + + inputHTML = inputHTML.contains( EMPTY_VALUE_TAG ) ? inputHTML.replace( EMPTY_VALUE_TAG, displayValue ) + : inputHTML + " " + displayValue; + + String displayTitle = ( catalogTypeAttribute == null ) ? " title=\"" + CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST + "\" " + : " title=\"" + catalogTypeAttribute.getId() + "." + catalogTypeAttribute.getName() + "-" + + catalogTypeAttribute.getValueType() + "\" "; + + inputHTML = inputHTML.contains( EMPTY_TITLE_TAG ) ? inputHTML.replace( EMPTY_TITLE_TAG, displayTitle ) + : inputHTML + " " + displayTitle; + + inputHTML = inputHTML + ">"; + + inputMatcher.appendReplacement( sb, inputHTML ); + } + } + + inputMatcher.appendTail( sb ); + + return (sb.toString().isEmpty()) ? htmlCode : sb.toString(); + } + + + private String populateCustomDataEntryForCOMBO( String htmlCode ) + { + // --------------------------------------------------------------------- + // Metadata code to add to HTML before outputting + // --------------------------------------------------------------------- + + StringBuffer sb = new StringBuffer(); + + // --------------------------------------------------------------------- + // Pattern to match catalogType attribute in the HTML code + // --------------------------------------------------------------------- + + Matcher inputMatcher = INPUT_PATTERN.matcher( htmlCode ); + + // --------------------------------------------------------------------- + // Iterate through all matching catalogType attribute fields + // --------------------------------------------------------------------- + + while ( inputMatcher.find() ) + { + // ----------------------------------------------------------------- + // Get HTML input field code + // ----------------------------------------------------------------- + + String catalogTypeAttributeCode = inputMatcher.group( 1 ); + + String inputHTML = inputMatcher.group(); + inputHTML = inputHTML.replace( ">", "" ); + + Matcher identifierMatcher = CatalogDataEntryService.IDENTIFIER_PATTERN_FIELD.matcher( catalogTypeAttributeCode ); + + if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 ) + { + // ------------------------------------------------------------- + // Get catalogType attribute ID of catalogType attribute + // ------------------------------------------------------------- + + int catalogTypeAttributeId = Integer.parseInt( identifierMatcher.group( 2 ) ); + + CatalogTypeAttribute catalogTypeAttribute = catalogTypeAttributeService.getCatalogTypeAttribute( catalogTypeAttributeId ); + + if ( catalogTypeAttribute != null && !CatalogTypeAttribute.TYPE_COMBO.equalsIgnoreCase( catalogTypeAttribute.getValueType() ) ) + { + continue; + } + + + String displayValue = ( catalogTypeAttribute == null ) ? " value=\"" + CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST + "\" " + : " value=\"[ " + catalogTypeAttribute.getName() + " ]\""; + + inputHTML = inputHTML.contains( EMPTY_VALUE_TAG ) ? inputHTML.replace( EMPTY_VALUE_TAG, displayValue ) + : inputHTML + " " + displayValue; + + String displayTitle = ( catalogTypeAttribute == null ) ? " title=\"" + CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST + "\" " + : " title=\"" + catalogTypeAttribute.getId() + "." + catalogTypeAttribute.getName() + "-" + + catalogTypeAttribute.getValueType() + "\" "; + + inputHTML = inputHTML.contains( EMPTY_TITLE_TAG ) ? inputHTML.replace( EMPTY_TITLE_TAG, displayTitle ) + : inputHTML + " " + displayTitle; + + inputHTML = inputHTML + ">"; + + inputMatcher.appendReplacement( sb, inputHTML ); + } + } + + inputMatcher.appendTail( sb ); + + return (sb.toString().isEmpty()) ? htmlCode : sb.toString(); + } + + + private String populateCustomDataEntryForBoolean( String htmlCode ) + { + // --------------------------------------------------------------------- + // Metadata code to add to HTML before outputting + // --------------------------------------------------------------------- + + StringBuffer sb = new StringBuffer(); + + // --------------------------------------------------------------------- + // Pattern to match catalogType attribute in the HTML code + // --------------------------------------------------------------------- + + Matcher inputMatcher = INPUT_PATTERN.matcher( htmlCode ); + + // --------------------------------------------------------------------- + // Iterate through all matching catalogType attribute fields + // --------------------------------------------------------------------- + + while ( inputMatcher.find() ) + { + String inputHTML = inputMatcher.group(); + inputHTML = inputHTML.replace( ">", "" ); + + // ----------------------------------------------------------------- + // Get HTML input field code + // ----------------------------------------------------------------- + + String catalogTypeAttributeCode = inputMatcher.group( 1 ); + + Matcher identifierMatcher = IDENTIFIER_PATTERN_FIELD.matcher( catalogTypeAttributeCode ); + + if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 ) + { + // ------------------------------------------------------------- + // Get catalogType attribute ID of catalogType attribute + // ------------------------------------------------------------- + int catalogTypeAttributeId = Integer.parseInt( identifierMatcher.group( 2 ) ); + + CatalogTypeAttribute catalogTypeAttribute = catalogTypeAttributeService.getCatalogTypeAttribute( catalogTypeAttributeId ); + + if ( catalogTypeAttribute != null && !CatalogTypeAttribute.TYPE_BOOL.equalsIgnoreCase( catalogTypeAttribute.getValueType() ) ) + { + continue; + } + + String displayValue = ( catalogTypeAttribute == null) ? " value=\"" + CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST + "\" " + : " value=\"[ " + catalogTypeAttribute.getName() + " ]\" "; + + inputHTML = inputHTML.contains( EMPTY_VALUE_TAG ) ? inputHTML.replace( EMPTY_VALUE_TAG, displayValue ) + : inputHTML + " " + displayValue; + + String displayTitle = ( catalogTypeAttribute == null) ? " title=\"" + CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST + "\" " + : " title=\"" + catalogTypeAttribute.getId() + "." + catalogTypeAttribute.getName() + "-" + + catalogTypeAttribute.getValueType() + "\" "; + + inputHTML = inputHTML.contains( EMPTY_TITLE_TAG ) ? inputHTML.replace( EMPTY_TITLE_TAG, displayTitle ) + : inputHTML + " " + displayTitle; + + inputHTML = inputHTML + ">"; + + inputMatcher.appendReplacement( sb, inputHTML ); + } + } + + inputMatcher.appendTail( sb ); + + return (sb.toString().isEmpty()) ? htmlCode : sb.toString(); + } + + + private String populateCustomDataEntryForDate( String htmlCode ) + { + // --------------------------------------------------------------------- + // Metadata code to add to HTML before outputting + // --------------------------------------------------------------------- + + StringBuffer sb = new StringBuffer(); + + // --------------------------------------------------------------------- + // Pattern to match data elements in the HTML code + // --------------------------------------------------------------------- + + Matcher inputMatcher = INPUT_PATTERN.matcher( htmlCode ); + + // --------------------------------------------------------------------- + // Iterate through all matching data element fields + // --------------------------------------------------------------------- + + while ( inputMatcher.find() ) + { + String inputHTML = inputMatcher.group(); + + inputHTML = inputHTML.replace( ">", "" ); + + // ----------------------------------------------------------------- + // Get HTML input field code + // ----------------------------------------------------------------- + + String catalogTypeAttributeCode = inputMatcher.group( 1 ); + + Matcher identifierMatcher = IDENTIFIER_PATTERN_FIELD.matcher( catalogTypeAttributeCode ); + + if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 ) + { + // ------------------------------------------------------------- + // Get catalogType attribute ID of catalogType attribute + // ------------------------------------------------------------- + + int catalogTypeAttributeId = Integer.parseInt( identifierMatcher.group( 2 ) ); + + CatalogTypeAttribute catalogTypeAttribute = catalogTypeAttributeService.getCatalogTypeAttribute( catalogTypeAttributeId ); + + //int dataElementId = Integer.parseInt( identifierMatcher.group( 2 ) ); + //DataElement dataElement = dataElementService.getDataElement( dataElementId ); + + if ( catalogTypeAttribute != null && !CatalogTypeAttribute.TYPE_DATE.equalsIgnoreCase( catalogTypeAttribute.getValueType() ) ) + { + continue; + } + + String displayValue = ( catalogTypeAttribute == null ) ? " value=\"" + CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST + "\"" + : " value=\"[ " + catalogTypeAttribute.getName() + " ]\""; + + inputHTML = inputHTML.contains( EMPTY_VALUE_TAG ) ? inputHTML.replace( EMPTY_VALUE_TAG, displayValue ) + : inputHTML + " " + displayValue; + + String displayTitle = (catalogTypeAttribute == null) ? " title=\"" + CATALOG_TYPE_ATTRIBUTE_DOES_NOT_EXIST + "\"" + : " title=\"" + catalogTypeAttribute.getId() + "." + catalogTypeAttribute.getName() + "-" + + catalogTypeAttribute.getValueType() + "\" "; + + inputHTML = inputHTML.contains( EMPTY_TITLE_TAG ) ? inputHTML.replace( EMPTY_TITLE_TAG, displayTitle ) + : inputHTML + " " + displayTitle; + + inputHTML = inputHTML + ">"; + + inputMatcher.appendReplacement( sb, inputHTML ); + } + } + + inputMatcher.appendTail( sb ); + + return (sb.toString().isEmpty()) ? htmlCode : sb.toString(); + } + + private String populateCustomDataEntryForBoolean( String dataEntryFormCode, + Collection dataValues, String disabled, I18n i18n, CatalogType catalogType,Map> mapDataValue ) + { + // --------------------------------------------------------------------- + // Inline Javascript to add to HTML before outputting + // --------------------------------------------------------------------- + + final String jsCodeForBoolean = " name=\"entryselect\" $DISABLED data=\"{mandatory:$MANDATORY, catalogTypeAttributeName:'$CATALOGTYPEATTRIBUTENAME' }\" onchange=\"saveOpt( $CATALOGTYPEATTRIBUTEID )\" style=\" text-align:center;\" "; + + StringBuffer sb = new StringBuffer(); + + // --------------------------------------------------------------------- + // Pattern to match CatalogTypeAttributes in the HTML code + // --------------------------------------------------------------------- + + Matcher CatalogTypeAttributeMatcher = INPUT_PATTERN.matcher( dataEntryFormCode ); + + // --------------------------------------------------------------------- + // Iterate through all matching catalogTypeAttribute fields + // --------------------------------------------------------------------- + + Map catalogTypeAttributeMap = getCatalogTypeAttributeMap( catalogType ); + + while ( CatalogTypeAttributeMatcher.find() ) + { + // ----------------------------------------------------------------- + // Get HTML input field code + // ----------------------------------------------------------------- + + String mandatory = "null"; + + + String catalogTypeAttributeCode = CatalogTypeAttributeMatcher.group( 1 ); + + Matcher identifierMatcher = IDENTIFIER_PATTERN_FIELD.matcher( catalogTypeAttributeCode ); + if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 ) + { + + // ------------------------------------------------------------- + // Get catalogType attribute ID of catalogType attribute + // ------------------------------------------------------------- + + int catalogTypeId = Integer.parseInt( identifierMatcher.group( 1 ) ); + + int catalogTypeAttributeId = Integer.parseInt( identifierMatcher.group( 2 ) ); + + CatalogTypeAttribute catalogTypeAttribute = null; + + String catalogTypeName = catalogType.getName(); + + if ( catalogTypeId != catalogType.getId() ) + { + catalogTypeAttribute = catalogTypeAttributeService.getCatalogTypeAttribute( catalogTypeAttributeId ); + } + else + { + catalogTypeAttribute = catalogTypeAttributeMap.get( catalogTypeAttributeId ); + + if ( catalogTypeAttribute == null ) + { + return i18n.getString( "some_catalogType_attribute_not_exist" ); + } + + mandatory = BooleanUtils.toStringTrueFalse( catalogTypeAttribute.isMandatory() ); + } + + if ( catalogTypeAttribute == null ) + { + continue; + } + + if ( !CatalogTypeAttribute.TYPE_BOOL.equalsIgnoreCase( catalogTypeAttribute.getValueType() ) ) + { + continue; + } + + // ------------------------------------------------------------- + // Find type of catalogType attribute + // ------------------------------------------------------------- + + String catalogTypeAttributeType = catalogTypeAttribute.getValueType(); + + // ------------------------------------------------------------- + // Find existing value of catalogType Attribute + // ------------------------------------------------------------- + + CatalogDataValue catalogDataValue = null; + + + String catalogTypeAttributeValue = EMPTY; + + if ( catalogTypeId != catalogType.getId() ) + { + + + Collection catalogDataValues = mapDataValue.get( catalogTypeId ); + + if ( catalogDataValues == null ) + { + + } + + catalogDataValue = getValue( catalogDataValues, catalogTypeAttributeId ); + + catalogTypeAttributeValue = catalogDataValue != null ? catalogDataValue.getValue() : catalogTypeAttributeValue; + } + else + { + + catalogDataValue = getValue( dataValues, catalogTypeAttributeId ); + + if ( catalogDataValue != null ) + { + catalogTypeAttributeValue = catalogDataValue.getValue(); + } + } + + String appendCode = catalogTypeAttributeCode.replaceFirst( "input", "select" ); + appendCode = appendCode.replace( "name=\"entryselect\"", jsCodeForBoolean ); + + // ------------------------------------------------------------- + // Add title + // ------------------------------------------------------------- + + if ( catalogTypeAttributeCode.contains( "title=\"\"" ) ) + { + appendCode = appendCode.replace( "title=\"\"", "title=\"" + catalogTypeAttribute.getId() + "." + + catalogTypeAttribute.getName() + "-" + catalogTypeAttributeType + "\" " ); + } + else + { + appendCode += "title=\"" + catalogTypeAttribute.getId() + "." + catalogTypeAttribute.getName() + "-" + + catalogTypeAttributeType + "\" "; + } + + appendCode += ">"; + appendCode += ""; + appendCode += ""; + appendCode += ""; + + // ------------------------------------------------------------- + // Insert value of catalogType Attribute in output code + // ------------------------------------------------------------- + + if ( catalogDataValue != null ) + { + if ( catalogTypeAttributeValue.equalsIgnoreCase( "true" ) ) + { + appendCode = appendCode.replace( "