=== modified file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/action/ExcelImportResultAction.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/action/ExcelImportResultAction.java 2010-12-29 16:17:28 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/action/ExcelImportResultAction.java 2011-04-15 11:42:21 +0000 @@ -591,9 +591,8 @@ orgUnitList = new ArrayList(); orgUnit = organisationUnitService.getOrganisationUnit( ouIDTB ); orgUnitList.add( orgUnit ); - } - + DataSet dataSet = dataSetService.getDataSet( dataSetId ); selectedPeriod = periodService.getPeriod( availablePeriods ); === added directory 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/portal' === added directory 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/portal/action' === added file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/portal/action/PortalExcelImportFormAction.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/portal/action/PortalExcelImportFormAction.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/portal/action/PortalExcelImportFormAction.java 2011-04-15 11:42:21 +0000 @@ -0,0 +1,191 @@ +package org.hisp.dhis.excelimport.portal.action; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.hisp.dhis.excelimport.util.PortalImportSheet; +import org.hisp.dhis.excelimport.util.ReportService; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import com.opensymphony.xwork2.Action; + +public class PortalExcelImportFormAction implements Action +{ + + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + + private ReportService reportService; + + public void setReportService( ReportService reportService ) + { + this.reportService = reportService; + } + + // ------------------------------------------------------------------------- + // Getter & Setter + // ------------------------------------------------------------------------- + + private List excelImportSheetList; + + public List getExcelImportSheetList() + { + return excelImportSheetList; + } + + private String raFolderName; + + // ------------------------------------------------------------------------- + // Action implementation + // ------------------------------------------------------------------------- + public String execute() throws Exception + { + raFolderName = reportService.getRAFolderName(); + + excelImportSheetList = new ArrayList(); + + getExcelImportSheetList( "portalDataImportSheetList.xml" ); + + return SUCCESS; + } + + + public void getExcelImportSheetList( String reportListFileName ) + { + String fileName = reportListFileName; + + String excelImportFolderName = "excelimport"; + + String path = System.getProperty( "user.home" ) + File.separator + "dhis" + raFolderName + File.separator + excelImportFolderName + File.separator + fileName; + + try + { + String newpath = System.getenv( "DHIS2_HOME" ); + if ( newpath != null ) + { + path = newpath + File.separator + raFolderName + File.separator + excelImportFolderName + File.separator + fileName; + } + } + catch ( NullPointerException npe ) + { + System.out.println("DHIS2_HOME is not set"); + } + + String xmlTemplateName; + String displayName; + String periodicity; + String proforma; + String checkerTemplateName; + String checkerRangeForHeader; + String checkerRangeForData; + String datasetId; + String orgunitGroupId; + String facilityStart; + + int count = 0; + + try + { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse( new File( path ) ); + if ( doc == null ) + { + System.out.println( "XML File Not Found at DHIS HOME" ); + return; + } + + NodeList listOfReports = doc.getElementsByTagName( "PortalImportSheet" ); + int totalReports = listOfReports.getLength(); + for ( int s = 0; s < totalReports; s++ ) + { + Node reportNode = listOfReports.item( s ); + if ( reportNode.getNodeType() == Node.ELEMENT_NODE ) + { + Element reportElement = (Element) reportNode; + + NodeList nodeList = reportElement.getElementsByTagName( "xmlTemplateName" ); + Element element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + xmlTemplateName = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "displayName" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + displayName = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "periodicity" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + periodicity = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "proforma" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + proforma = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "checkerTemplateName" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + checkerTemplateName = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "checkerRangeForHeader" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + checkerRangeForHeader = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "checkerRangeForData" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + checkerRangeForData = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "dataset" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + datasetId = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "orgunitgroup" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + orgunitGroupId = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + nodeList = reportElement.getElementsByTagName( "facilityStart" ); + element = (Element) nodeList.item( 0 ); + nodeList = element.getChildNodes(); + facilityStart = ((Node) nodeList.item( 0 )).getNodeValue().trim(); + + PortalImportSheet portalImportSheet = new PortalImportSheet( xmlTemplateName, displayName, periodicity, proforma, checkerTemplateName, checkerRangeForHeader, checkerRangeForData, datasetId, orgunitGroupId, facilityStart ); + + excelImportSheetList.add( count, portalImportSheet ); + + count++; + } + }// end of for loop with s var + }// try block end + catch ( SAXParseException err ) + { + System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() ); + System.out.println( " " + err.getMessage() ); + } + catch ( SAXException e ) + { + Exception x = e.getException(); + ((x == null) ? e : x).printStackTrace(); + } + catch ( Throwable t ) + { + t.printStackTrace(); + } + } + +} === added file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/portal/action/PortalExcelImportResultAction.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/portal/action/PortalExcelImportResultAction.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/portal/action/PortalExcelImportResultAction.java 2011-04-15 11:42:21 +0000 @@ -0,0 +1,752 @@ +package org.hisp.dhis.excelimport.portal.action; + +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import jxl.Sheet; +import jxl.Workbook; +import jxl.write.WritableCellFormat; +import jxl.write.WritableSheet; +import jxl.write.WritableWorkbook; + +import org.amplecode.quick.StatementManager; +import org.hisp.dhis.datalock.DataSetLock; +import org.hisp.dhis.datalock.DataSetLockService; +import org.hisp.dhis.dataset.DataSet; +import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.excelimport.util.ExcelImport_DeCode; +import org.hisp.dhis.excelimport.util.ExcelImport_Header; +import org.hisp.dhis.excelimport.util.ReportService; +import org.hisp.dhis.i18n.I18nFormat; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.organisationunit.OrganisationUnitGroup; +import org.hisp.dhis.organisationunit.OrganisationUnitGroupService; +import org.hisp.dhis.organisationunit.OrganisationUnitService; +import org.hisp.dhis.period.Period; +import org.hisp.dhis.period.PeriodService; +import org.hisp.dhis.period.PeriodType; +import org.hisp.dhis.user.CurrentUserService; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.rowset.SqlRowSet; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import com.opensymphony.xwork2.Action; + +public class PortalExcelImportResultAction implements Action +{ + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + + private StatementManager statementManager; + + public void setStatementManager( StatementManager statementManager ) + { + this.statementManager = statementManager; + } + + private ReportService reportService; + + public void setReportService( ReportService reportService ) + { + this.reportService = reportService; + } + + private PeriodService periodService; + + public void setPeriodService( PeriodService periodService ) + { + this.periodService = periodService; + } + + private JdbcTemplate jdbcTemplate; + + public void setJdbcTemplate( JdbcTemplate jdbcTemplate ) + { + this.jdbcTemplate = jdbcTemplate; + } + + private OrganisationUnitService organisationUnitService; + + public void setOrganisationUnitService( OrganisationUnitService organisationUnitService ) + { + this.organisationUnitService = organisationUnitService; + } + + private OrganisationUnitGroupService organisationUnitGroupService; + + public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService ) + { + this.organisationUnitGroupService = organisationUnitGroupService; + } + + private CurrentUserService currentUserService; + + public void setCurrentUserService( CurrentUserService currentUserService ) + { + this.currentUserService = currentUserService; + } + + private DataSetLockService dataSetLockService; + + public void setDataSetLockService( DataSetLockService dataSetLockService ) + { + this.dataSetLockService = dataSetLockService; + } + + private DataSetService dataSetService; + + public void setDataSetService( DataSetService dataSetService ) + { + this.dataSetService = dataSetService; + } + + + private I18nFormat format; + + public void setFormat( I18nFormat format ) + { + this.format = format; + } + + // ------------------------------------------------------------------------- + // Getter & Setter + // ------------------------------------------------------------------------- + + private String proforma; + + public void setProforma( String proforma ) + { + this.proforma = proforma; + } + + private String checkerTemplateName; + + public void setCheckerTemplateName( String checkerTemplateName ) + { + this.checkerTemplateName = checkerTemplateName; + } + + private String checkerRangeForHeader; + + public void setCheckerRangeForHeader( String checkerRangeForHeader ) + { + this.checkerRangeForHeader = checkerRangeForHeader; + } + + private String checkerRangeForData; + + public void setCheckerRangeForData( String checkerRangeForData ) + { + this.checkerRangeForData = checkerRangeForData; + } + + private Integer datasetId; + + public void setDatasetId( Integer datasetId ) + { + this.datasetId = datasetId; + } + + private Integer orgunitGroupId; + + public void setOrgunitGroupId( Integer orgunitGroupId ) + { + this.orgunitGroupId = orgunitGroupId; + } + + private String facilityStart; + + public void setFacilityStart( String facilityStart ) + { + this.facilityStart = facilityStart; + } + + private String importSheetId; + + public void setImportSheetId( String importSheetId ) + { + this.importSheetId = importSheetId; + } + + private String message = ""; + + public String getMessage() + { + return message; + } + + private File output; + + public File getOutput() + { + return output; + } + + private File upload; + + public File getUpload() + { + return upload; + } + + public void setUpload( File upload ) + { + this.upload = upload; + } + + private String raFolderName; + + // ------------------------------------------------------------------------- + // Action implementation + // ------------------------------------------------------------------------- + public String execute() throws Exception + { + statementManager.initialise(); + + message += "
Importing StartTime : " + new Date() + " - By "+currentUserService.getCurrentUsername() + "
"; + System.out.println( message ); + + raFolderName = reportService.getRAFolderName(); + + String excelTemplatePath = System.getenv( "DHIS2_HOME" ) + File.separator + raFolderName + File.separator + + "excelimport" + File.separator + "template" + File.separator + checkerTemplateName; + + String outputReportPath = System.getenv( "DHIS2_HOME" ) + File.separator + raFolderName + File.separator + "output" + File.separator + UUID.randomUUID().toString() + ".xls"; + + Workbook excelImportFile = Workbook.getWorkbook( upload ); + WritableWorkbook writableExcelImportFile = Workbook.createWorkbook( new File(outputReportPath), excelImportFile ); + Workbook excelTemplateFile = Workbook.getWorkbook( new File( excelTemplatePath ) ); + + if( validateReport( excelImportFile, excelTemplateFile ) ) + { + System.out.println("Uploaded ExcelSheet is matched with Template file."); + importPortalData( writableExcelImportFile ); + + } + else + { + message = "The file you are trying to import is not the correct format"; + } + + try + { + + } + finally + { + excelImportFile.close(); + excelTemplateFile.close(); + writableExcelImportFile.close(); + } + + System.out.println("Importing has been completed which is started by : "+currentUserService.getCurrentUsername() + " at " + new Date() ); + message += "

Importing EndTime : " + new Date() + " - By "+currentUserService.getCurrentUsername() + ""; + + statementManager.destroy(); + + return SUCCESS; + } + + + public List getHeaderInfo( String fileName ) + { + String excelImportFolderName = "excelimport"; + List headerInfoList = new ArrayList(); + + String path = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator + raFolderName + + File.separator + excelImportFolderName + File.separator + fileName; + try + { + String newpath = System.getenv( "DHIS2_HOME" ); + if ( newpath != null ) + { + path = newpath + File.separator + raFolderName + File.separator + excelImportFolderName + + File.separator + fileName; + } + } + catch ( NullPointerException npe ) + { + System.out.println("DHIS_HOME is not set"); + } + + try + { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse( new File( path ) ); + if ( doc == null ) + { + System.out.println( "There is no DECodes related XML file in the DHIS2 Home" ); + return null; + } + + NodeList listOfHeaders = doc.getElementsByTagName( "header" ); + int totalHeaders = listOfHeaders.getLength(); + + for( int s = 0; s < totalHeaders; s++ ) + { + Element headerElement = (Element) listOfHeaders.item( s ); + NodeList textHeaderList = headerElement.getChildNodes(); + String headerExpression = ((Node) textHeaderList.item( 0 )).getNodeValue().trim(); + Integer sheetNo = Integer.parseInt( headerElement.getAttribute( "sheetno" ) ); + Integer rowNo = Integer.parseInt( headerElement.getAttribute( "rowno" ) ); + Integer colNo = Integer.parseInt( headerElement.getAttribute( "colno" ) ); + ExcelImport_Header excelImport_Header = new ExcelImport_Header( sheetNo, rowNo, colNo, headerExpression ); + + headerInfoList.add( excelImport_Header ); + }// end of for loop with s var + }// try block end + catch ( SAXParseException err ) + { + System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() ); + System.out.println( " " + err.getMessage() ); + } + catch ( SAXException e ) + { + Exception x = e.getException(); + ((x == null) ? e : x).printStackTrace(); + } + catch ( Throwable t ) + { + t.printStackTrace(); + } + + return headerInfoList; + } + + public List getDataInfo( String fileName ) + { + String excelImportFolderName = "excelimport"; + List deCodeList = new ArrayList(); + + String path = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator + raFolderName + File.separator + excelImportFolderName + File.separator + fileName; + try + { + String newpath = System.getenv( "DHIS2_HOME" ); + if ( newpath != null ) + { + path = newpath + File.separator + raFolderName + File.separator + excelImportFolderName + File.separator + fileName; + } + } + catch ( NullPointerException npe ) + { + System.out.println("DHIS_HOME is not set"); + } + + try + { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse( new File( path ) ); + if ( doc == null ) + { + System.out.println( "There is no DECodes related XML file in the DHIS2 Home" ); + return null; + } + + NodeList listOfDeCodes = doc.getElementsByTagName( "de-code" ); + int totalDeCodes = listOfDeCodes.getLength(); + + for( int s = 0; s < totalDeCodes; s++ ) + { + Element deCodeElement = (Element) listOfDeCodes.item( s ); + NodeList textDeCodeList = deCodeElement.getChildNodes(); + String deCodeExpression = ((Node) textDeCodeList.item( 0 )).getNodeValue().trim(); + Integer sheetNo = Integer.parseInt( deCodeElement.getAttribute( "sheetno" ) ); + Integer rowNo = Integer.parseInt( deCodeElement.getAttribute( "rowno" ) ); + Integer colNo = Integer.parseInt( deCodeElement.getAttribute( "colno" ) ); + ExcelImport_DeCode excelImport_DeCode = new ExcelImport_DeCode( sheetNo, rowNo, colNo, deCodeExpression ); + + deCodeList.add( excelImport_DeCode ); + }// end of for loop with s var + }// try block end + catch ( SAXParseException err ) + { + System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() ); + System.out.println( " " + err.getMessage() ); + } + catch ( SAXException e ) + { + Exception x = e.getException(); + ((x == null) ? e : x).printStackTrace(); + } + catch ( Throwable t ) + { + t.printStackTrace(); + } + + return deCodeList; + } + + private void importPortalData( WritableWorkbook importWorkbook ) throws Exception + { + List headerInfoList = new ArrayList(); + headerInfoList = getHeaderInfo( importSheetId ); + + List deCodeList = new ArrayList(); + deCodeList = getDataInfo( importSheetId ); + + Map monthMap = new HashMap(); + monthMap.put( "January", "01" ); + monthMap.put( "February", "02" ); + monthMap.put( "March", "03" ); + monthMap.put( "April", "04" ); + monthMap.put( "May", "05" ); + monthMap.put( "June", "06" ); + monthMap.put( "July", "07" ); + monthMap.put( "August", "08" ); + monthMap.put( "September", "09" ); + monthMap.put( "October", "10" ); + monthMap.put( "November", "11" ); + monthMap.put( "December", "12" ); + + String selectedMonth = ""; + String selectedPeriodicity = ""; + String selectedFinancialYear = ""; + String selectedFormat = ""; + String selectedParentName = ""; + + String query = ""; + String storedBy = currentUserService.getCurrentUsername(); + if ( storedBy == null ) + { + storedBy = "[unknown]"; + } + + DataSet dataSet = dataSetService.getDataSet( datasetId ); + + Sheet sheet = importWorkbook.getSheet( 0 ); + + for( ExcelImport_Header header : headerInfoList ) + { + sheet = importWorkbook.getSheet( header.getSheetno() ); + String cellContent = sheet.getCell( header.getColno(), header.getRowno() ).getContents(); + + if( cellContent.equalsIgnoreCase( "" ) || cellContent == null || cellContent.equalsIgnoreCase( " " ) ) + { + continue; + } + + if( header.getExpression().equalsIgnoreCase( ExcelImport_Header.HEADER_PERIOD ) ) + { + selectedMonth = monthMap.get( cellContent ); + } + else if( header.getExpression().equalsIgnoreCase( ExcelImport_Header.HEADER_FINANCIALYEAR ) ) + { + selectedFinancialYear = cellContent; + } + else if( header.getExpression().equalsIgnoreCase( ExcelImport_Header.HEADER_FORMAT ) ) + { + selectedFormat = cellContent; + } + else if( header.getExpression().equalsIgnoreCase( ExcelImport_Header.HEADER_FACILITY_PARENT ) ) + { + selectedParentName = cellContent; + } + else if( header.getExpression().equalsIgnoreCase( ExcelImport_Header.HEADER_PERIODICITY ) ) + { + selectedPeriodicity = cellContent; + } + } + + String selStartDate = ""; + String selEndDate = ""; + if( !selectedFinancialYear.trim().equalsIgnoreCase( "" ) ) + { + if( selectedMonth.equals( "01" ) || selectedMonth.equals( "02" ) || selectedMonth.equals( "03" ) ) + { + selStartDate = selectedFinancialYear.split( "-" )[1] + "-" + selectedMonth + "-" + "01"; + } + else + { + selStartDate = selectedFinancialYear.split( "-" )[0] + "-" + selectedMonth + "-" + "01"; + } + } + + PeriodType periodType = periodService.getPeriodTypeByName( selectedPeriodicity ); + Period selectedPeriod = getSelectedPeriod( selStartDate, periodType ); + SimpleDateFormat periodFormat; + if( periodType.getName().equalsIgnoreCase("Monthly") ) + { + periodFormat = new SimpleDateFormat("MMM-yyyy"); + } + else if( periodType.getName().equalsIgnoreCase("Monthly") ) + { + periodFormat = new SimpleDateFormat("yyyy"); + } + else + { + periodFormat = new SimpleDateFormat("yyyy-MM-dd"); + } + + Integer selectedBlockId = getOrgUnitIdByComment( selectedParentName ); + if( selectedBlockId != null ) + { + List orgUnitList = new ArrayList( organisationUnitService.getOrganisationUnitWithChildren( selectedBlockId ) ); + OrganisationUnitGroup orgUnitGroup = organisationUnitGroupService.getOrganisationUnitGroup( orgunitGroupId ); + orgUnitList.retainAll( orgUnitGroup.getMembers() ); + + System.out.println( orgUnitList.size() + " : " + orgUnitGroup.getMembers().size() + " : " + orgUnitList.size() ); + + int facilityStartRow = Integer.parseInt( facilityStart.split( "," )[0] ); + int facilityStartCol = Integer.parseInt( facilityStart.split( "," )[1] ); + + String facility = sheet.getCell( facilityStartCol, facilityStartRow ).getContents(); + int colCount = facilityStartCol; + while( facility != null && !facility.trim().equalsIgnoreCase( "" ) ) + { + if( facility.trim().equalsIgnoreCase( "Total" ) ) + { + colCount++; + facility = sheet.getCell( colCount, facilityStartRow ).getContents(); + + continue; + } + + Integer currentOrgunitId = getOrgUnitIdByComment( facility ); + if( currentOrgunitId != null ) + { + OrganisationUnit portalOrgUnit = organisationUnitService.getOrganisationUnit( currentOrgunitId ); + + if( portalOrgUnit != null && orgUnitList.contains( portalOrgUnit ) ) + { + System.out.println("--------Importing started for :"+portalOrgUnit.getName() + "-------------" ); + DataSetLock dataSetLock = dataSetLockService.getDataSetLockByDataSetPeriodAndSource( dataSet, selectedPeriod, portalOrgUnit ); + if( dataSetLock != null ) + { + message += "
Unable to Import : Corresponding Dataset ( "+dataSet.getName()+" ) for " + portalOrgUnit.getName() + " and for period : " + periodFormat.format( selectedPeriod.getStartDate() ) + " is locked."; + System.out.println("Unable to Import : Corresponding Dataset ( "+dataSet.getName()+" ) for " + portalOrgUnit.getName() + " and for period : " + periodFormat.format( selectedPeriod.getStartDate() ) + " is locked."); + colCount++; + facility = sheet.getCell( colCount, facilityStartRow ).getContents(); + + continue; + } + + int insertFlag = 1; + String insertQuery = "INSERT INTO datavalue (dataelementid, periodid, sourceid, categoryoptioncomboid, value, storedby, lastupdated ) VALUES "; + + for( ExcelImport_DeCode deCode : deCodeList ) + { + String deCodeExpression = deCode.getExpression(); + if( deCodeExpression != null && !deCodeExpression.trim().equals( "" ) ) + { + Integer deId = Integer.parseInt( deCodeExpression.split( "\\." )[0] ); + Integer deCOCId = Integer.parseInt( deCodeExpression.split( "\\." )[1] ); + + String dataValue = sheet.getCell( colCount, deCode.getRowno() ).getContents(); + + query = "SELECT value FROM datavalue WHERE dataelementid = " + deId + + " AND categoryoptioncomboid = " + deCOCId + + " AND periodid = " + selectedPeriod.getId() + + " AND sourceid = " + portalOrgUnit.getId(); + + long t; + Date d = new Date(); + t = d.getTime(); + java.sql.Date lastUpdatedDate = new java.sql.Date( t ); + + SqlRowSet sqlResultSet1 = jdbcTemplate.queryForRowSet( query ); + if ( sqlResultSet1 != null && sqlResultSet1.next() ) + { + String updateQuery = "UPDATE datavalue SET value = '" + dataValue + "', storedby = '" + storedBy + "',lastupdated='" + lastUpdatedDate + "' WHERE dataelementid = "+ deId +" AND periodid = " + selectedPeriod.getId() + " AND sourceid = " + portalOrgUnit.getId() + " AND categoryoptioncomboid = "+deCOCId; + jdbcTemplate.update( updateQuery ); + } + else + { + if( dataValue != null && !dataValue.trim().equalsIgnoreCase( "" ) ) + { + insertQuery += "( "+ deId + ", " + selectedPeriod.getId() + ", "+ portalOrgUnit.getId() +", " + deCOCId + ", '" + dataValue + "', '" + storedBy + "', '" + lastUpdatedDate + "' ), "; + insertFlag = 2; + } + } + } + } + + if( insertFlag != 1 ) + { + insertQuery = insertQuery.substring( 0, insertQuery.length()-2 ); + jdbcTemplate.update( insertQuery ); + System.out.println("Data is uploaded in DHIS for : "+ facility); + } + message += "
Data is uploaded into DHIS for : "+ facility + " and for period : "+ periodFormat.format( selectedPeriod.getStartDate() ); + } + else + { + System.out.println( facility + " is not a memeber of orgunitgroup : "+ orgUnitGroup.getName() ); + message += "
" + facility + " is not a memeber of orgunitgroup : "+ orgUnitGroup.getName() + ""; + } + } + else + { + System.out.println("No Mapping found in DHIS for :"+ facility + " : NULL"); + message += "
No Mapping found in DHIS for : "+ facility + ""; + } + + colCount++; + facility = sheet.getCell( colCount, facilityStartRow ).getContents(); + + } + + + } + + } + + public void setTextFormatForExcelShett( WritableWorkbook excelImportFile ) + { + WritableSheet sheet = excelImportFile.getSheet( 0 ); + int facilityStartRow = Integer.parseInt( facilityStart.split( "," )[0] ); + int facilityStartCol = Integer.parseInt( facilityStart.split( "," )[1] ); + + WritableCellFormat wCellformat = new WritableCellFormat (sheet.getCell( facilityStartCol, facilityStartRow ).getCellFormat() ); + int rowEnd = sheet.getRows(); + int colEnd = sheet.getColumns(); + + for ( int c = 0; c <= colEnd; c++ ) + { + for ( int r = 0; r <= rowEnd; r++ ) + { + sheet.getWritableCell( c, r ).setCellFormat( wCellformat ); + } + } + } + + public Integer getOrgUnitIdByComment( String comment ) + { + String query = "SELECT organisationunitid FROM organisationunit WHERE comment LIKE '"+ comment +"'"; + SqlRowSet sqlResultSet = jdbcTemplate.queryForRowSet( query ); + if ( sqlResultSet != null && sqlResultSet.next() ) + { + return sqlResultSet.getInt( 1 ); + } + return null; + } + + public boolean validateReport( Workbook excelImportFile, Workbook excelTemplateFile ) + { + boolean validator = true; + + int sheetNumber = 0; + + String headerParts[] = checkerRangeForHeader.split("-"); + int headerStartRow = Integer.parseInt( headerParts[0].split( "," )[0] ); + int headerEndRow = Integer.parseInt( headerParts[1].split( "," )[0] );; + int headerStartCol = Integer.parseInt( headerParts[0].split( "," )[1] );; + int headerEndCol = Integer.parseInt( headerParts[1].split( "," )[1] );; + + String dataParts[] = checkerRangeForData.split("-"); + int dataStartRow = Integer.parseInt( dataParts[0].split( "," )[0] ); + int dataEndRow = Integer.parseInt( dataParts[1].split( "," )[0] );; + int dataStartCol = Integer.parseInt( dataParts[0].split( "," )[1] );; + int dataEndCol = Integer.parseInt( dataParts[1].split( "," )[1] );; + + Sheet importFileSheet = excelImportFile.getSheet( sheetNumber ); + Sheet templateFileSheet = excelTemplateFile.getSheet( sheetNumber ); + + if ( excelImportFile.getSheet( sheetNumber ).getRows() == excelTemplateFile.getSheet( sheetNumber ).getRows() ) + { + // Checking Header Cells + for ( int c = headerStartCol; c <= headerEndCol; c++ ) + { + for ( int r = headerStartRow; r <= headerEndRow; r++ ) + { + String cellContent = importFileSheet.getCell( c, r ).getContents(); + String templateContent = templateFileSheet.getCell( c, r ).getContents(); + + if ( templateContent.equalsIgnoreCase( cellContent ) && cellContent.equalsIgnoreCase( templateContent ) ) + { + continue; + } + else + { + validator = false; + break; + } + } + } + + // Checking Data Cells + for ( int c = dataStartCol; c <= dataEndCol; c++ ) + { + for ( int r = dataStartRow; r <= dataEndRow; r++ ) + { + String cellContent = importFileSheet.getCell( c, r ).getContents(); + String templateContent = templateFileSheet.getCell( c, r ).getContents(); + + if ( templateContent.equalsIgnoreCase( cellContent ) && cellContent.equalsIgnoreCase( templateContent ) ) + { + continue; + } + else + { + validator = false; + break; + } + } + } + } + else + { + validator = false; + } + + return validator; + } + + + public Period getSelectedPeriod( String startDate, PeriodType periodType ) throws Exception + { + SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" ); + + List periods = new ArrayList( periodService.getPeriodsByPeriodType( periodType ) ); + for ( Period period : periods ) + { + String tempDate = dateFormat.format( period.getStartDate() ); + if ( tempDate.equalsIgnoreCase( startDate ) ) + { + return period; + } + } + + Period period = periodType.createPeriod( dateFormat.parse( startDate ) ); + period = reloadPeriodForceAdd( period ); + periodService.addPeriod( period ); + + return period; + } + + private final Period reloadPeriod( Period period ) + { + return periodService.getPeriod( period.getStartDate(), period.getEndDate(), period.getPeriodType() ); + } + + private final Period reloadPeriodForceAdd( Period period ) + { + Period storedPeriod = reloadPeriod( period ); + + if ( storedPeriod == null ) + { + periodService.addPeriod( period ); + + return period; + } + + return storedPeriod; + } + +} === added directory 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/tcs' === added directory 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/tcs/action' === added file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/tcs/action/TCSXmlImportFormAction.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/tcs/action/TCSXmlImportFormAction.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/tcs/action/TCSXmlImportFormAction.java 2011-04-15 11:42:21 +0000 @@ -0,0 +1,24 @@ +package org.hisp.dhis.excelimport.tcs.action; + +import com.opensymphony.xwork2.Action; + +public class TCSXmlImportFormAction implements Action +{ + + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + + // ------------------------------------------------------------------------- + // Getter & Setter + // ------------------------------------------------------------------------- + + // ------------------------------------------------------------------------- + // Action implementation + // ------------------------------------------------------------------------- + public String execute() throws Exception + { + return SUCCESS; + } + +} === added file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/tcs/action/TCSXmlImportResultAction.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/tcs/action/TCSXmlImportResultAction.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/tcs/action/TCSXmlImportResultAction.java 2011-04-15 11:42:21 +0000 @@ -0,0 +1,491 @@ +package org.hisp.dhis.excelimport.tcs.action; + +import java.io.File; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.amplecode.quick.StatementManager; +import org.hisp.dhis.datalock.DataSetLockService; +import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.excelimport.util.ReportService; +import org.hisp.dhis.excelimport.util.TCSXMLMap; +import org.hisp.dhis.i18n.I18nFormat; +import org.hisp.dhis.organisationunit.OrganisationUnitService; +import org.hisp.dhis.period.MonthlyPeriodType; +import org.hisp.dhis.period.Period; +import org.hisp.dhis.period.PeriodService; +import org.hisp.dhis.system.util.StreamUtils; +import org.hisp.dhis.user.CurrentUserService; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.rowset.SqlRowSet; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import com.opensymphony.xwork2.Action; + +public class TCSXmlImportResultAction implements Action +{ + + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + + private StatementManager statementManager; + + public void setStatementManager( StatementManager statementManager ) + { + this.statementManager = statementManager; + } + + private ReportService reportService; + + public void setReportService( ReportService reportService ) + { + this.reportService = reportService; + } + + private PeriodService periodService; + + public void setPeriodService( PeriodService periodService ) + { + this.periodService = periodService; + } + + private JdbcTemplate jdbcTemplate; + + public void setJdbcTemplate( JdbcTemplate jdbcTemplate ) + { + this.jdbcTemplate = jdbcTemplate; + } + + private OrganisationUnitService organisationUnitService; + + public void setOrganisationUnitService( OrganisationUnitService organisationUnitService ) + { + this.organisationUnitService = organisationUnitService; + } + + private CurrentUserService currentUserService; + + public void setCurrentUserService( CurrentUserService currentUserService ) + { + this.currentUserService = currentUserService; + } + + private DataSetLockService dataSetLockService; + + public void setDataSetLockService( DataSetLockService dataSetLockService ) + { + this.dataSetLockService = dataSetLockService; + } + + private DataSetService dataSetService; + + public void setDataSetService( DataSetService dataSetService ) + { + this.dataSetService = dataSetService; + } + + private I18nFormat format; + + public void setFormat( I18nFormat format ) + { + this.format = format; + } + + // ------------------------------------------------------------------------- + // Getter & Setter + // ------------------------------------------------------------------------- + + private String message = ""; + + public String getMessage() + { + return message; + } + + private File outputFile; + + + private File upload; + + public File getUpload() + { + return upload; + } + + public void setUpload( File upload ) + { + this.upload = upload; + } + + private String fileName; + + public void setUploadFileName( String fileName ) + { + this.fileName = fileName; + } + + private String raFolderName; + + int insertCount = 0; + int updateCount = 0; + int facilityCount = 0; + + // ------------------------------------------------------------------------- + // Action implementation + // ------------------------------------------------------------------------- + public String execute() throws Exception + { + statementManager.initialise(); + + message += "
Importing StartTime : " + new Date() + " - By "+currentUserService.getCurrentUsername() + "
"; + System.out.println( message ); + + raFolderName = reportService.getRAFolderName(); + + String outputPath = System.getenv( "DHIS2_HOME" ) + File.separator + raFolderName + File.separator + "output" + File.separator + UUID.randomUUID().toString() + ".xml"; + + try + { + outputFile = new File( outputPath ); + + StreamUtils.write( upload, outputFile ); + + if( importTCSData() !=0 ) + { + message += "
Importing has been done successfully for the file : "+ fileName; + message += "
Total number of Facilities that are imported : "+ facilityCount; + message += "
Total new records that are imported : "+insertCount; + message += "
Total records that are updated : "+updateCount; + } + } + catch( Exception e ) + { + e.printStackTrace(); + message += "
Some problem occured while Importing the file : "+ fileName + "
Error Message: "+e.getMessage()+"
"; + } + + System.out.println("Importing has been completed which is started by : "+currentUserService.getCurrentUsername() + " at " + new Date() ); + message += "

Importing EndTime : " + new Date() + " - By "+currentUserService.getCurrentUsername() + ""; + + statementManager.destroy(); + + return SUCCESS; + } + + private int importTCSData( ) throws Exception + { + //List dataValueList = new ArrayList( getDataInfo() ); + List dataValueList = getDataInfo(); + if( dataValueList == null ) + { + return 0; + } + + + Map monthMap = new HashMap( getPeriodMap() ); + + String facilityCode = "0"; + + String storedBy = currentUserService.getCurrentUsername(); + if ( storedBy == null ) + { + storedBy = "[unknown]"; + } + + long t; + Date d = new Date(); + t = d.getTime(); + java.sql.Date lastUpdatedDate = new java.sql.Date( t ); + + String query = ""; + int insertFlag = 1; + String insertQuery = "INSERT INTO datavalue (dataelementid, periodid, sourceid, categoryoptioncomboid, value, storedby, lastupdated ) VALUES "; + int count = 1; + for( TCSXMLMap tcsDataValue : dataValueList ) + { + Integer deId = Integer.parseInt( tcsDataValue.getDhisDataElement().split( "\\." )[0] ); + Integer deCOCId = Integer.parseInt( tcsDataValue.getDhisDataElement().split( "\\." )[1] ); + + String orgUnitCode = tcsDataValue.getOrgunitCode(); + Integer orgUnitId = getOrgUnitIdByCode( orgUnitCode ); + + if( !facilityCode.equals( orgUnitCode ) ) + { + facilityCode = orgUnitCode; + facilityCount++; + } + + String tcsPeriod = tcsDataValue.getTscPeriod(); + String selMonth = monthMap.get( tcsPeriod.split( "-" )[0] ); + String startDate = tcsPeriod.split( "-" )[1] + "-" + selMonth + "-01"; + Period selectedPeriod = reportService.getSelectedPeriod( startDate, new MonthlyPeriodType() ); + + String dataValue = tcsDataValue.getDataValue(); + + query = "SELECT value FROM datavalue WHERE dataelementid = " + deId + + " AND categoryoptioncomboid = " + deCOCId + + " AND periodid = " + selectedPeriod.getId() + + " AND sourceid = " + orgUnitId; + + SqlRowSet sqlResultSet1 = jdbcTemplate.queryForRowSet( query ); + if ( sqlResultSet1 != null && sqlResultSet1.next() ) + { + String updateQuery = "UPDATE datavalue SET value = '" + dataValue + "', storedby = '" + storedBy + "',lastupdated='" + lastUpdatedDate + "' WHERE dataelementid = "+ deId +" AND periodid = " + selectedPeriod.getId() + " AND sourceid = " + orgUnitId + " AND categoryoptioncomboid = "+deCOCId; + jdbcTemplate.update( updateQuery ); + updateCount++; + } + else + { + if( dataValue != null && !dataValue.trim().equalsIgnoreCase( "" ) ) + { + insertQuery += "( "+ deId + ", " + selectedPeriod.getId() + ", "+ orgUnitId +", " + deCOCId + ", '" + dataValue + "', '" + storedBy + "', '" + lastUpdatedDate + "' ), "; + insertFlag = 2; + insertCount++; + } + } + + if( count == 1000 ) + { + count = 1; + + if( insertFlag != 1 ) + { + insertQuery = insertQuery.substring( 0, insertQuery.length()-2 ); + jdbcTemplate.update( insertQuery ); + } + + insertFlag = 1; + insertQuery = "INSERT INTO datavalue (dataelementid, periodid, sourceid, categoryoptioncomboid, value, storedby, lastupdated ) VALUES "; + } + + count++; + } + + if( insertFlag != 1 ) + { + insertQuery = insertQuery.substring( 0, insertQuery.length()-2 ); + jdbcTemplate.update( insertQuery ); + } + + return 1; + } + + + public Integer getOrgUnitIdByCode( String orgUnitCode ) + { + String query = "SELECT organisationunitid FROM organisationunit WHERE code LIKE '"+ orgUnitCode +"'"; + SqlRowSet sqlResultSet = jdbcTemplate.queryForRowSet( query ); + if ( sqlResultSet != null && sqlResultSet.next() ) + { + return sqlResultSet.getInt( 1 ); + } + return null; + } + + + + public Map getPeriodMap() + { + Map periodMap = new HashMap(); + + String newpath = ""; + try + { + newpath = System.getenv( "DHIS2_HOME" ) + File.separator + raFolderName + File.separator + "excelimport" + File.separator + "tcs_dhis_mapping.xml"; + } + catch ( NullPointerException npe ) + { + System.out.println("DHIS_HOME is not set"); + } + + try + { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse( new File( newpath ) ); + if ( doc == null ) + { + System.out.println( "There is no MAP XML file in the DHIS2 Home" ); + return null; + } + + NodeList listOfPeriodMap = doc.getElementsByTagName( "periodmap" ); + int totalPeriodMap = listOfPeriodMap.getLength(); + + for( int s = 0; s < totalPeriodMap; s++ ) + { + Element element = (Element) listOfPeriodMap.item( s ); + String tcsPeriod = element.getAttribute( "tcsperiod" ); + String dhisPeriod = element.getAttribute( "dhisperiod" ); + + if( tcsPeriod != null && dhisPeriod != null ) + { + periodMap.put( tcsPeriod, dhisPeriod ); + } + }// end of for loop with s var + }// try block end + catch ( SAXParseException err ) + { + System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() ); + System.out.println( " " + err.getMessage() ); + } + catch ( SAXException e ) + { + Exception x = e.getException(); + ((x == null) ? e : x).printStackTrace(); + } + catch ( Throwable t ) + { + t.printStackTrace(); + } + + return periodMap; + } + + public Map getDataElementMap() + { + Map dataElementMap = new HashMap(); + + String newpath = ""; + try + { + newpath = System.getenv( "DHIS2_HOME" ) + File.separator + raFolderName + File.separator + "excelimport" + File.separator + "tcs_dhis_mapping.xml"; + } + catch ( NullPointerException npe ) + { + System.out.println("DHIS_HOME is not set"); + message += "
DHIS_HOME is not set"; + return null; + } + + try + { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse( new File( newpath ) ); + if ( doc == null ) + { + System.out.println( "There is no MAP XML file in the DHIS2 Home" ); + message += "
TNHMIS - DHIS mapping file is not found"; + return null; + } + + NodeList listOfDeMap = doc.getElementsByTagName( "demap" ); + int totalDeMap = listOfDeMap.getLength(); + + for( int s = 0; s < totalDeMap; s++ ) + { + Element element = (Element) listOfDeMap.item( s ); + String tcsDe = element.getAttribute( "tcsde" ); + String dhisDe = element.getAttribute( "dhisde" ); + + if( tcsDe != null && dhisDe != null ) + { + dataElementMap.put( tcsDe, dhisDe ); + } + }// end of for loop with s var + }// try block end + catch ( SAXParseException err ) + { + System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() ); + System.out.println( " " + err.getMessage() ); + message += "
TNHMIS - DHIS mapping file is not found"; + return null; + } + catch ( SAXException e ) + { + Exception x = e.getException(); + ((x == null) ? e : x).printStackTrace(); + message += "
TNHMIS - DHIS mapping file is not found"; + return null; + } + catch ( Throwable t ) + { + t.printStackTrace(); + message += "
TNHMIS - DHIS mapping file is not found"; + return null; + } + + return dataElementMap; + } + + + public List getDataInfo() + { + List dataValueList = new ArrayList(); + + //Map tcs_dhis_deMap = new HashMap( getDataElementMap() ); + Map tcs_dhis_deMap = getDataElementMap(); + if( tcs_dhis_deMap == null ) + { + return null; + } + + try + { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse( outputFile ); + if ( doc == null ) + { + System.out.println( "There is no Data XML file in the DHIS2 Home" ); + message += "
TNHMIS - DHIS mapping file is not found"; + return null; + } + + NodeList listOfDataValues = doc.getElementsByTagName( "dataValue" ); + int totalDataValues = listOfDataValues.getLength(); + + for( int s = 0; s < totalDataValues; s++ ) + { + Element element = (Element) listOfDataValues.item( s ); + String tcsDataElement = element.getAttribute( "dataElement" ); + String orgunitCode = element.getAttribute( "source" ); + String tscPeriod = element.getAttribute( "period" ); + String dataValue = element.getAttribute( "value" ); + String dhisDataElement = tcs_dhis_deMap.get( tcsDataElement ); + + if( dhisDataElement != null && dataValue != null && !dataValue.trim().equalsIgnoreCase( "" ) ) + { + TCSXMLMap tcsDataValue = new TCSXMLMap( tcsDataElement, dhisDataElement, orgunitCode, tscPeriod, dataValue ); + dataValueList.add( tcsDataValue ); + } + }// end of for loop with s var + }// try block end + catch ( SAXParseException err ) + { + System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() ); + System.out.println( " " + err.getMessage() ); + message += "
"+ fileName +" is not proper format, please generate XML File from TNHMIS and try again"; + return null; + } + catch ( SAXException e ) + { + Exception x = e.getException(); + ((x == null) ? e : x).printStackTrace(); + message += "
"+ fileName +" is not proper format, please generate XML File from TNHMIS and try again"; + return null; + } + catch ( Throwable t ) + { + t.printStackTrace(); + message += "
"+ fileName +" is not proper format, please generate XML File from TNHMIS and try again"; + return null; + } + + return dataValueList; + } + +} \ No newline at end of file === added file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ExcelImport_DeCode.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ExcelImport_DeCode.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ExcelImport_DeCode.java 2011-04-15 11:42:21 +0000 @@ -0,0 +1,87 @@ +package org.hisp.dhis.excelimport.util; + +import java.io.Serializable; + +public class ExcelImport_DeCode implements Serializable +{ + /** + * Sheet number + */ + private int sheetno; + + /** + * Row number + */ + private int rowno; + + /** + * Column number + */ + private int colno; + + /** + * Formula to calculate the values. + */ + private String expression; + + // ------------------------------------------------------------------------- + // Contructors + // ------------------------------------------------------------------------- + public ExcelImport_DeCode() + { + + } + + public ExcelImport_DeCode( int sheetno, int rowno, int colno, String expression ) + { + this.sheetno = sheetno; + this.rowno = rowno; + this.colno = colno; + this.expression = expression; + } + + // ------------------------------------------------------------------------- + // Getters and setters + // ------------------------------------------------------------------------- + + public int getSheetno() + { + return sheetno; + } + + public void setSheetno( int sheetno ) + { + this.sheetno = sheetno; + } + + public int getRowno() + { + return rowno; + } + + public void setRowno( int rowno ) + { + this.rowno = rowno; + } + + public int getColno() + { + return colno; + } + + public void setColno( int colno ) + { + this.colno = colno; + } + + public String getExpression() + { + return expression; + } + + public void setExpression( String expression ) + { + this.expression = expression; + } + +} === added file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ExcelImport_Header.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ExcelImport_Header.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ExcelImport_Header.java 2011-04-15 11:42:21 +0000 @@ -0,0 +1,93 @@ +package org.hisp.dhis.excelimport.util; + +import java.io.Serializable; + +public class ExcelImport_Header implements Serializable +{ + public static final String HEADER_PERIOD = "PERIOD"; + public static final String HEADER_FINANCIALYEAR = "FINANCIAL_YEAR"; + public static final String HEADER_FORMAT = "FORMAT"; + public static final String HEADER_FACILITY_PARENT = "FACILITY_PARENT"; + public static final String HEADER_PERIODICITY = "PERIODICITY"; + + /** + * Sheet number + */ + private int sheetno; + + /** + * Row number + */ + private int rowno; + + /** + * Column number + */ + private int colno; + + /** + * Formula to calculate the values. + */ + private String expression; + + // ------------------------------------------------------------------------- + // Contructors + // ------------------------------------------------------------------------- + public ExcelImport_Header() + { + + } + + public ExcelImport_Header( int sheetno, int rowno, int colno, String expression ) + { + this.sheetno = sheetno; + this.rowno = rowno; + this.colno = colno; + this.expression = expression; + } + + // ------------------------------------------------------------------------- + // Getters and setters + // ------------------------------------------------------------------------- + + public int getSheetno() + { + return sheetno; + } + + public void setSheetno( int sheetno ) + { + this.sheetno = sheetno; + } + + public int getRowno() + { + return rowno; + } + + public void setRowno( int rowno ) + { + this.rowno = rowno; + } + + public int getColno() + { + return colno; + } + + public void setColno( int colno ) + { + this.colno = colno; + } + + public String getExpression() + { + return expression; + } + + public void setExpression( String expression ) + { + this.expression = expression; + } + +} === added file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/PortalImportSheet.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/PortalImportSheet.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/PortalImportSheet.java 2011-04-15 11:42:21 +0000 @@ -0,0 +1,144 @@ +package org.hisp.dhis.excelimport.util; + +import java.io.Serializable; + +public class PortalImportSheet implements Serializable +{ + private String xmlTemplateName; + private String displayName; + private String periodicity; + private String proforma; + private String checkerTemplateName; + private String checkerRangeForHeader; + private String checkerRangeForData; + private String datasetId; + private String orgunitGroupId; + private String facilityStart; + + // ------------------------------------------------------------------------- + // Constructors + // ------------------------------------------------------------------------- + public PortalImportSheet() + { + + } + + public PortalImportSheet( String xmlTemplateName, String displayName, String periodicity, String proforma, String checkerTemplateName, String checkerRangeForHeader, String checkerRangeForData, String datasetId, String orgunitGroupId, String facilityStart ) + { + this.xmlTemplateName = xmlTemplateName; + this.displayName = displayName; + this.periodicity = periodicity; + this.proforma = proforma; + this.checkerTemplateName = checkerTemplateName; + this.checkerRangeForHeader = checkerRangeForHeader; + this.checkerRangeForData = checkerRangeForData; + this.datasetId = datasetId; + this.orgunitGroupId = orgunitGroupId; + this.facilityStart = facilityStart; + } + + // ------------------------------------------------------------------------- + // Getters and setters + // ------------------------------------------------------------------------- + + public String getXmlTemplateName() + { + return xmlTemplateName; + } + + public void setXmlTemplateName( String xmlTemplateName ) + { + this.xmlTemplateName = xmlTemplateName; + } + + public String getDisplayName() + { + return displayName; + } + + public void setDisplayName( String displayName ) + { + this.displayName = displayName; + } + + public String getPeriodicity() + { + return periodicity; + } + + public void setPeriodicity( String periodicity ) + { + this.periodicity = periodicity; + } + + public String getProforma() + { + return proforma; + } + + public void setProforma( String proforma ) + { + this.proforma = proforma; + } + + public String getCheckerTemplateName() + { + return checkerTemplateName; + } + + public void setCheckerTemplateName( String checkerTemplateName ) + { + this.checkerTemplateName = checkerTemplateName; + } + + public String getCheckerRangeForHeader() + { + return checkerRangeForHeader; + } + + public void setCheckerRangeForHeader( String checkerRangeForHeader ) + { + this.checkerRangeForHeader = checkerRangeForHeader; + } + + public String getCheckerRangeForData() + { + return checkerRangeForData; + } + + public void setCheckerRangeForData( String checkerRangeForData ) + { + this.checkerRangeForData = checkerRangeForData; + } + + public String getDatasetId() + { + return datasetId; + } + + public void setDatasetId( String datasetId ) + { + this.datasetId = datasetId; + } + + public String getOrgunitGroupId() + { + return orgunitGroupId; + } + + public void setOrgunitGroupId( String orgunitGroupId ) + { + this.orgunitGroupId = orgunitGroupId; + } + + public String getFacilityStart() + { + return facilityStart; + } + + public void setFacilityStart( String facilityStart ) + { + this.facilityStart = facilityStart; + } + +} === modified file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ReportService.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ReportService.java 2010-12-29 16:17:28 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/ReportService.java 2011-04-15 11:42:21 +0000 @@ -1,989 +1,1031 @@ -package org.hisp.dhis.excelimport.util; - -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.hisp.dhis.aggregation.AggregationService; -import org.hisp.dhis.config.ConfigurationService; -import org.hisp.dhis.config.Configuration_IN; -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -import org.hisp.dhis.dataelement.DataElementCategoryService; -import org.hisp.dhis.dataelement.DataElementService; -import org.hisp.dhis.dataset.DataSet; -import org.hisp.dhis.dataset.DataSetService; -import org.hisp.dhis.datavalue.DataValue; -import org.hisp.dhis.datavalue.DataValueService; -import org.hisp.dhis.indicator.Indicator; -import org.hisp.dhis.indicator.IndicatorService; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.period.Period; -import org.hisp.dhis.period.PeriodService; -import org.hisp.dhis.period.PeriodType; -import org.hisp.dhis.system.util.MathUtils; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.support.rowset.SqlRowSet; - -public class ReportService -{ - - public static final String KEY_RAFOLDER = "reportfolder"; - - public static final String NULL_REPLACEMENT = "0"; - - // ------------------------------------------------------------------------- - // Dependencies - // ------------------------------------------------------------------------- - - private PeriodService periodService; - - public void setPeriodService( PeriodService periodService ) - { - this.periodService = periodService; - } - - /* - * private DBConnection dbConnection; - * - * public void setDbConnection( DBConnection dbConnection ) { - * this.dbConnection = dbConnection; } - */ - - private JdbcTemplate jdbcTemplate; - - public void setJdbcTemplate( JdbcTemplate jdbcTemplate ) - { - this.jdbcTemplate = jdbcTemplate; - } - - private DataElementService dataElementService; - - public void setDataElementService( DataElementService dataElementService ) - { - this.dataElementService = dataElementService; - } - - private AggregationService aggregationService; - - public void setAggregationService( AggregationService aggregationService ) - { - this.aggregationService = aggregationService; - } - - private DataElementCategoryService dataElementCategoryService; - - public void setDataElementCategoryService( DataElementCategoryService dataElementCategoryService ) - { - this.dataElementCategoryService = dataElementCategoryService; - } - - private DataValueService dataValueService; - - public void setDataValueService( DataValueService dataValueService ) - { - this.dataValueService = dataValueService; - } - - private DataSetService dataSetService; - - public void setDataSetService( DataSetService dataSetService ) - { - this.dataSetService = dataSetService; - } - - private IndicatorService indicatorService; - - public void setIndicatorService( IndicatorService indicatorService ) - { - this.indicatorService = indicatorService; - } - - private ConfigurationService configurationService; - - public void setConfigurationService( ConfigurationService configurationService ) - { - this.configurationService = configurationService; - } - - // ------------------------------------------------------------------------- - // Services - // ------------------------------------------------------------------------- - - public List getMonthlyPeriods( Date start, Date end ) - { - List periodList = new ArrayList( periodService.getPeriodsBetweenDates( start, end ) ); - PeriodType monthlyPeriodType = PeriodType.getByNameIgnoreCase( "monthly" ); - - List monthlyPeriodList = new ArrayList(); - - for ( Period period : periodList ) - { - if ( period.getPeriodType().getId() == monthlyPeriodType.getId() ) - { - monthlyPeriodList.add( period ); - } - } - return monthlyPeriodList; - } - - /* - * Returns the Period Object of the given date For ex:- if the month is 3, - * year is 2006 and periodType Object of type Monthly then it returns the - * corresponding Period Object - */ - public Period getPeriodByMonth( int month, int year, PeriodType periodType ) - { - int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; - - Calendar cal = Calendar.getInstance(); - cal.set( year, month, 1, 0, 0, 0 ); - Date firstDay = new Date( cal.getTimeInMillis() ); - - if ( periodType.getName().equals( "Monthly" ) ) - { - cal.set( year, month, 1, 0, 0, 0 ); - if ( year % 4 == 0 ) - { - cal.set( Calendar.DAY_OF_MONTH, monthDays[month] + 1 ); - } - else - { - cal.set( Calendar.DAY_OF_MONTH, monthDays[month] ); - } - } - else if ( periodType.getName().equals( "Yearly" ) ) - { - cal.set( year, Calendar.DECEMBER, 31 ); - } - Date lastDay = new Date( cal.getTimeInMillis() ); - System.out.println( lastDay.toString() ); - Period newPeriod = new Period(); - newPeriod = periodService.getPeriod( firstDay, lastDay, periodType ); - return newPeriod; - } - - public List getLinelistingRecordNos( OrganisationUnit organisationUnit, Period period, String lltype ) - { - List recordNosList = new ArrayList(); - - // Connection con = dbConnection.openConnection(); - - Statement st = null; - - // ResultSet rs1 = null; - - String query = ""; - - int dataElementid = 1020; - - /* - * if( lltype.equalsIgnoreCase( "lllivebirth" ) ) dataElementid = - * LLDataSets.LLB_CHILD_NAME; else if( lltype.equalsIgnoreCase( - * "lllivebirth" ) ) dataElementid = LLDataSets.LLD_CHILD_NAME; else if( - * lltype.equalsIgnoreCase( "lllivebirth" ) ) dataElementid = - * LLDataSets.LLMD_MOTHER_NAME; - */ - - if ( lltype.equalsIgnoreCase( "lllivebirth-l4" ) || lltype.equalsIgnoreCase( "lllivebirth-l5" ) - || lltype.equalsIgnoreCase( "lllivebirth-l6" ) ) - dataElementid = 1020; - else if ( lltype.equalsIgnoreCase( "lldeath-l4" ) || lltype.equalsIgnoreCase( "lldeath-l5" ) - || lltype.equalsIgnoreCase( "lldeath-l6" ) ) - dataElementid = 1027; - else if ( lltype.equalsIgnoreCase( "llmaternaldeath-l4" ) || lltype.equalsIgnoreCase( "llmaternaldeath-l5" ) - || lltype.equalsIgnoreCase( "llmaternaldeath-l6" ) ) - dataElementid = 1032; - - try - { - // st = con.createStatement(); - - query = "SELECT recordno FROM lldatavalue WHERE dataelementid = " + dataElementid + " AND periodid = " - + period.getId() + " AND sourceid = " + organisationUnit.getId(); - // rs1 = st.executeQuery( query ); - - SqlRowSet rs1 = jdbcTemplate.queryForRowSet( query ); - - while ( rs1.next() ) - { - recordNosList.add( rs1.getInt( 1 ) ); - } - - Collections.sort( recordNosList ); - } - catch ( Exception e ) - { - System.out.println( "SQL Exception : " + e.getMessage() ); - return null; - } - /* - * finally { try { SqlRowSet rs1; - * - * if ( st != null ) st.close(); if ( rs1 != null ) rs1.close(); - * - * if ( con != null ) con.close(); } catch ( Exception e ) { - * System.out.println( "SQL Exception : " + e.getMessage() ); return - * null; } } - */// finally block end - - return recordNosList; - } - - public String getRAFolderName() - { - // Connection con = dbConnection.openConnection(); - - // Statement st = null; - - // ResultSet rs1 = null; - - String raFolderName = "ra_national"; - - try - { - // st = con.createStatement(); - - /* - * query = "SELECT mvalue FROM maintenancein WHERE mkey LIKE '" + - * KEY_RAFOLDER + "'"; //rs1 = st.executeQuery( query ); - * - * SqlRowSet rs1 = jdbcTemplate.queryForRowSet( query ); - * - * if ( rs1.next() ) { raFolderName = rs1.getString( 1 ); } - */ - - raFolderName = configurationService.getConfigurationByKey( Configuration_IN.KEY_REPORTFOLDER ).getValue(); - - } - catch ( Exception e ) - { - System.out.println( "Exception : " + e.getMessage() ); - return null; - } - - return raFolderName; - - } - - /* - * Returns the PeriodType Object for selected DataElement, If no PeriodType - * is found then by default returns Monthly Period type - */ - public PeriodType getDataElementPeriodType( DataElement de ) - { - List dataSetList = new ArrayList( dataSetService.getAllDataSets() ); - Iterator it = dataSetList.iterator(); - while ( it.hasNext() ) - { - DataSet ds = (DataSet) it.next(); - List dataElementList = new ArrayList( ds.getDataElements() ); - if ( dataElementList.contains( de ) ) - { - return ds.getPeriodType(); - } - } - - return null; - - } // getDataElementPeriodType end - - private String getResultDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit ) - { - try - { - // System.out.println( "expression : " + formula + " ***** " + - // String.valueOf( startDate ) + " **** " - // + String.valueOf( endDate ) ); - - int deFlag1 = 0; - int deFlag2 = 0; - Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); - - Matcher matcher = pattern.matcher( formula ); - StringBuffer buffer = new StringBuffer(); - - String resultValue = ""; - - while ( matcher.find() ) - { - String replaceString = matcher.group(); - - replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); - String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1, - replaceString.length() ); - - replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); - - int dataElementId = Integer.parseInt( replaceString ); - int optionComboId = Integer.parseInt( optionComboIdStr ); - - DataElement dataElement = dataElementService.getDataElement( dataElementId ); - DataElementCategoryOptionCombo optionCombo = dataElementCategoryService - .getDataElementCategoryOptionCombo( optionComboId ); - - if ( dataElement == null || optionCombo == null ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - } - if ( dataElement.getType().equalsIgnoreCase( "int" ) ) - { - Double aggregatedValue = aggregationService.getAggregatedDataValue( dataElement, optionCombo, - startDate, endDate, organisationUnit ); - if ( aggregatedValue == null ) - { - replaceString = NULL_REPLACEMENT; - } - else - { - replaceString = String.valueOf( aggregatedValue ); - - deFlag2 = 1; - } - - } - else - { - deFlag1 = 1; - PeriodType dePeriodType = getDataElementPeriodType( dataElement ); - List periodList = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( - dePeriodType, startDate, endDate ) ); - Period tempPeriod = new Period(); - if ( periodList == null || periodList.isEmpty() ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - } - else - { - tempPeriod = (Period) periodList.get( 0 ); - } - - DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod, - optionCombo ); - - if ( dataValue != null ) - { - // Works for both text and boolean data types - - replaceString = dataValue.getValue(); - } - - else - replaceString = ""; - - if ( replaceString == null ) - replaceString = ""; - } - matcher.appendReplacement( buffer, replaceString ); - - resultValue = replaceString; - } - - matcher.appendTail( buffer ); - - if ( deFlag1 == 0 ) - { - - double d = 0.0; - try - { - d = MathUtils.calculateExpression( buffer.toString() ); - } - catch ( Exception e ) - { - d = 0.0; - resultValue = ""; - } - if ( d == -1 ) - { - d = 0.0; - resultValue = ""; - } - else - { - - // This is to display financial data as it is like 2.1476838 - resultValue = "" + d; - - // These lines are to display financial data that do not - // have decimals - d = d * 10; - - if ( d % 10 == 0 ) - { - resultValue = "" + (int) d / 10; - } - - d = d / 10; - - // These line are to display non financial data that do not - // require decimals - // if ( !(reportModelTB.equalsIgnoreCase( "STATIC-FINANCIAL" - // )) ) - resultValue = "" + (int) d; - - // if ( resultValue.equalsIgnoreCase( "0" ) ) - // { - // resultValue = ""; - // } - } - - } - else - { - resultValue = buffer.toString(); - } - - if ( resultValue.equalsIgnoreCase( "" ) ) - resultValue = " "; - - return resultValue; - } - catch ( NumberFormatException ex ) - { - throw new RuntimeException( "Illegal DataElement id", ex ); - } - } - - private String getIndividualResultDataValue( String formula, Date startDate, Date endDate, - OrganisationUnit organisationUnit ) - { - try - { - int deFlag1 = 0; - int deFlag2 = 0; - Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); - - Matcher matcher = pattern.matcher( formula ); - StringBuffer buffer = new StringBuffer(); - - String resultValue = ""; - boolean valueDoesNotExist = true; - - while ( matcher.find() ) - { - - String replaceString = matcher.group(); - - replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); - String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1, - replaceString.length() ); - - replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); - - int dataElementId = Integer.parseInt( replaceString ); - int optionComboId = Integer.parseInt( optionComboIdStr ); - - DataElement dataElement = dataElementService.getDataElement( dataElementId ); - DataElementCategoryOptionCombo optionCombo = dataElementCategoryService - .getDataElementCategoryOptionCombo( optionComboId ); - - if ( dataElement == null || optionCombo == null ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - } - if ( dataElement.getType().equalsIgnoreCase( "int" ) ) - { - - PeriodType dePeriodType = getDataElementPeriodType( dataElement ); - List periodList = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( - dePeriodType, startDate, endDate ) ); - - if ( periodList == null || periodList.isEmpty() ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - } - else - { - - double aggregatedValue = 0.0; - for ( Period tempPeriod : periodList ) - { - DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, - tempPeriod, optionCombo ); - - if ( dataValue != null ) - { - aggregatedValue += Double.parseDouble( dataValue.getValue() ); - - valueDoesNotExist = false; - } - } - - replaceString = String.valueOf( aggregatedValue ); - - deFlag2 = 1; - } - - } - else - { - deFlag1 = 1; - PeriodType dePeriodType = getDataElementPeriodType( dataElement ); - List periodList = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( - dePeriodType, startDate, endDate ) ); - Period tempPeriod = new Period(); - if ( periodList == null || periodList.isEmpty() ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - } - else - { - tempPeriod = (Period) periodList.get( 0 ); - } - - DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod, - optionCombo ); - - if ( dataValue != null ) - { - // Works for both text and boolean data types - - replaceString = dataValue.getValue(); - valueDoesNotExist = false; - } - - else - replaceString = ""; - - if ( replaceString == null ) - replaceString = ""; - } - matcher.appendReplacement( buffer, replaceString ); - - resultValue = replaceString; - } - - matcher.appendTail( buffer ); - - if ( deFlag1 == 0 ) - { - double d = 0.0; - try - { - d = MathUtils.calculateExpression( buffer.toString() ); - } - catch ( Exception e ) - { - d = 0.0; - - resultValue = ""; - } - if ( d == -1 ) - { - d = 0.0; - - resultValue = ""; - } - else - { - // This is to display financial data as it is like 2.1476838 - resultValue = "" + d; - - // These lines are to display financial data that do not - // have decimals - d = d * 10; - - if ( d % 10 == 0 ) - { - resultValue = "" + (int) d / 10; - } - - d = d / 10; - - // These line are to display non financial data that do not - // require decimals - // if ( !(reportModelTB.equalsIgnoreCase( "STATIC-FINANCIAL" - // )) ) - resultValue = "" + (int) d; - - // if ( resultValue.equalsIgnoreCase( "0" ) ) - // { - // resultValue = ""; - // } - } - } - else - { - resultValue = buffer.toString(); - } - - if ( valueDoesNotExist ) - resultValue = " "; - - if ( resultValue.equalsIgnoreCase( "" ) ) - resultValue = " "; - - return resultValue; - } - catch ( NumberFormatException ex ) - { - throw new RuntimeException( "Illegal DataElement id", ex ); - } - } - - private String getBooleanDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit ) - { - try - { - int deFlag1 = 0; - int deFlag2 = 0; - Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); - - Matcher matcher = pattern.matcher( formula ); - StringBuffer buffer = new StringBuffer(); - - while ( matcher.find() ) - { - String replaceString = matcher.group(); - - replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); - String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1, - replaceString.length() ); - - replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); - - int dataElementId = Integer.parseInt( replaceString ); - int optionComboId = Integer.parseInt( optionComboIdStr ); - - DataElement dataElement = dataElementService.getDataElement( dataElementId ); - DataElementCategoryOptionCombo optionCombo = dataElementCategoryService - .getDataElementCategoryOptionCombo( optionComboId ); - - if ( dataElement == null || optionCombo == null ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - } - - if ( dataElement.getType().equalsIgnoreCase( "bool" ) ) - { - deFlag1 = 1; - PeriodType dePeriodType = getDataElementPeriodType( dataElement ); - List periodList = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( - dePeriodType, startDate, endDate ) ); - Period tempPeriod = new Period(); - if ( periodList == null || periodList.isEmpty() ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - } - else - { - tempPeriod = (Period) periodList.get( 0 ); - } - - DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod, - optionCombo ); - - if ( dataValue != null ) - { - // Works for both text and boolean data types - - if ( dataValue.getValue().equalsIgnoreCase( "true" ) ) - { - replaceString = "Yes"; - } - else if ( dataValue.getValue().equalsIgnoreCase( "false" ) ) - { - replaceString = "No"; - } - else - { - replaceString = dataValue.getValue(); - } - } - - else - { - replaceString = ""; - } - - } - else - { - Double aggregatedValue = aggregationService.getAggregatedDataValue( dataElement, optionCombo, - startDate, endDate, organisationUnit ); - if ( aggregatedValue == null ) - { - replaceString = NULL_REPLACEMENT; - } - else - { - replaceString = String.valueOf( aggregatedValue ); - - deFlag2 = 1; - } - } - matcher.appendReplacement( buffer, replaceString ); - } - - matcher.appendTail( buffer ); - - String resultValue = ""; - if ( deFlag1 == 0 ) - { - double d = 0.0; - try - { - d = MathUtils.calculateExpression( buffer.toString() ); - } - catch ( Exception e ) - { - d = 0.0; - } - if ( d == -1 ) - { - d = 0.0; - } - else - { - d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 ); - resultValue = "" + (int) d; - } - - if ( deFlag2 == 0 ) - { - resultValue = " "; - } - } - else - { - resultValue = buffer.toString(); - } - return resultValue; - } - catch ( NumberFormatException ex ) - { - throw new RuntimeException( "Illegal DataElement id", ex ); - } - } - - private String getResultIndicatorValue( String formula, Date startDate, Date endDate, - OrganisationUnit organisationUnit ) - { - try - { - - int deFlag1 = 0; - int deFlag2 = 0; - Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); - - Matcher matcher = pattern.matcher( formula ); - StringBuffer buffer = new StringBuffer(); - - while ( matcher.find() ) - { - String replaceString = matcher.group(); - - replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); - - replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); - - int indicatorId = Integer.parseInt( replaceString ); - - Indicator indicator = indicatorService.getIndicator( indicatorId ); - - if ( indicator == null ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - - } - - Double aggregatedValue = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate, - organisationUnit ); - - if ( aggregatedValue == null ) - { - replaceString = NULL_REPLACEMENT; - } - else - { - replaceString = String.valueOf( aggregatedValue ); - deFlag2 = 1; - } - matcher.appendReplacement( buffer, replaceString ); - } - - matcher.appendTail( buffer ); - - String resultValue = ""; - if ( deFlag1 == 0 ) - { - double d = 0.0; - try - { - d = MathUtils.calculateExpression( buffer.toString() ); - } - catch ( Exception e ) - { - d = 0.0; - } - if ( d == -1 ) - d = 0.0; - else - { - d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 ); - resultValue = "" + d; - } - - if ( deFlag2 == 0 ) - { - resultValue = " "; - } - } - else - { - resultValue = buffer.toString(); - } - return resultValue; - } - catch ( NumberFormatException ex ) - { - throw new RuntimeException( "Illegal DataElement id", ex ); - } - } - - private String getIndividualResultIndicatorValue( String formula, Date startDate, Date endDate, - OrganisationUnit organisationUnit ) - { - try - { - - int deFlag1 = 0; - int deFlag2 = 0; - Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); - - Matcher matcher = pattern.matcher( formula ); - StringBuffer buffer = new StringBuffer(); - - while ( matcher.find() ) - { - String replaceString = matcher.group(); - - replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); - - replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); - - int indicatorId = Integer.parseInt( replaceString ); - - Indicator indicator = indicatorService.getIndicator( indicatorId ); - - if ( indicator == null ) - { - replaceString = ""; - matcher.appendReplacement( buffer, replaceString ); - continue; - - } - - String numeratorExp = indicator.getNumerator(); - String denominatorExp = indicator.getDenominator(); - int indicatorFactor = indicator.getIndicatorType().getFactor(); - String numeratorVal = getIndividualResultDataValue( numeratorExp, startDate, endDate, organisationUnit ); - String denominatorVal = getIndividualResultDataValue( denominatorExp, startDate, endDate, - organisationUnit ); - - double numeratorValue; - try - { - numeratorValue = Double.parseDouble( numeratorVal ); - } - catch ( Exception e ) - { - System.out.println( "Exception while getting Numerator : " + numeratorExp + " for Indicaotr " - + indicator.getName() ); - numeratorValue = 0.0; - } - - double denominatorValue; - try - { - denominatorValue = Double.parseDouble( denominatorVal ); - } - catch ( Exception e ) - { - System.out.println( "Exception while getting Deniminator : " + denominatorExp + " for Indicaotr " - + indicator.getName() ); - denominatorValue = 1.0; - } - - double aggregatedValue; - try - { - aggregatedValue = (numeratorValue / denominatorValue) * indicatorFactor; - } - catch ( Exception e ) - { - System.out.println( "Exception while calculating Indicator value for Indicaotr " - + indicator.getName() ); - aggregatedValue = 0.0; - } - - replaceString = String.valueOf( aggregatedValue ); - deFlag2 = 1; - - matcher.appendReplacement( buffer, replaceString ); - } - - matcher.appendTail( buffer ); - - String resultValue = ""; - if ( deFlag1 == 0 ) - { - double d = 0.0; - try - { - d = MathUtils.calculateExpression( buffer.toString() ); - } - catch ( Exception e ) - { - d = 0.0; - } - if ( d == -1 ) - d = 0.0; - else - { - d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 ); - resultValue = "" + d; - } - - if ( deFlag2 == 0 ) - { - resultValue = " "; - } - } - else - { - resultValue = buffer.toString(); - } - return resultValue; - } - catch ( NumberFormatException ex ) - { - throw new RuntimeException( "Illegal DataElement id", ex ); - } - } - -} +package org.hisp.dhis.excelimport.util; + +import java.sql.Statement; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.hisp.dhis.aggregation.AggregationService; +import org.hisp.dhis.config.ConfigurationService; +import org.hisp.dhis.config.Configuration_IN; +import org.hisp.dhis.dataelement.DataElement; +import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; +import org.hisp.dhis.dataelement.DataElementCategoryService; +import org.hisp.dhis.dataelement.DataElementService; +import org.hisp.dhis.dataset.DataSet; +import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.datavalue.DataValue; +import org.hisp.dhis.datavalue.DataValueService; +import org.hisp.dhis.indicator.Indicator; +import org.hisp.dhis.indicator.IndicatorService; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.period.Period; +import org.hisp.dhis.period.PeriodService; +import org.hisp.dhis.period.PeriodType; +import org.hisp.dhis.system.util.MathUtils; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.rowset.SqlRowSet; + +public class ReportService +{ + + public static final String KEY_RAFOLDER = "reportfolder"; + + public static final String NULL_REPLACEMENT = "0"; + + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + + private PeriodService periodService; + + public void setPeriodService( PeriodService periodService ) + { + this.periodService = periodService; + } + + /* + * private DBConnection dbConnection; + * + * public void setDbConnection( DBConnection dbConnection ) { + * this.dbConnection = dbConnection; } + */ + + private JdbcTemplate jdbcTemplate; + + public void setJdbcTemplate( JdbcTemplate jdbcTemplate ) + { + this.jdbcTemplate = jdbcTemplate; + } + + private DataElementService dataElementService; + + public void setDataElementService( DataElementService dataElementService ) + { + this.dataElementService = dataElementService; + } + + private AggregationService aggregationService; + + public void setAggregationService( AggregationService aggregationService ) + { + this.aggregationService = aggregationService; + } + + private DataElementCategoryService dataElementCategoryService; + + public void setDataElementCategoryService( DataElementCategoryService dataElementCategoryService ) + { + this.dataElementCategoryService = dataElementCategoryService; + } + + private DataValueService dataValueService; + + public void setDataValueService( DataValueService dataValueService ) + { + this.dataValueService = dataValueService; + } + + private DataSetService dataSetService; + + public void setDataSetService( DataSetService dataSetService ) + { + this.dataSetService = dataSetService; + } + + private IndicatorService indicatorService; + + public void setIndicatorService( IndicatorService indicatorService ) + { + this.indicatorService = indicatorService; + } + + private ConfigurationService configurationService; + + public void setConfigurationService( ConfigurationService configurationService ) + { + this.configurationService = configurationService; + } + + // ------------------------------------------------------------------------- + // Services + // ------------------------------------------------------------------------- + + public List getMonthlyPeriods( Date start, Date end ) + { + List periodList = new ArrayList( periodService.getPeriodsBetweenDates( start, end ) ); + PeriodType monthlyPeriodType = PeriodType.getByNameIgnoreCase( "monthly" ); + + List monthlyPeriodList = new ArrayList(); + + for ( Period period : periodList ) + { + if ( period.getPeriodType().getId() == monthlyPeriodType.getId() ) + { + monthlyPeriodList.add( period ); + } + } + return monthlyPeriodList; + } + + /* + * Returns the Period Object of the given date For ex:- if the month is 3, + * year is 2006 and periodType Object of type Monthly then it returns the + * corresponding Period Object + */ + public Period getPeriodByMonth( int month, int year, PeriodType periodType ) + { + int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + Calendar cal = Calendar.getInstance(); + cal.set( year, month, 1, 0, 0, 0 ); + Date firstDay = new Date( cal.getTimeInMillis() ); + + if ( periodType.getName().equals( "Monthly" ) ) + { + cal.set( year, month, 1, 0, 0, 0 ); + if ( year % 4 == 0 ) + { + cal.set( Calendar.DAY_OF_MONTH, monthDays[month] + 1 ); + } + else + { + cal.set( Calendar.DAY_OF_MONTH, monthDays[month] ); + } + } + else if ( periodType.getName().equals( "Yearly" ) ) + { + cal.set( year, Calendar.DECEMBER, 31 ); + } + Date lastDay = new Date( cal.getTimeInMillis() ); + System.out.println( lastDay.toString() ); + Period newPeriod = new Period(); + newPeriod = periodService.getPeriod( firstDay, lastDay, periodType ); + return newPeriod; + } + + public List getLinelistingRecordNos( OrganisationUnit organisationUnit, Period period, String lltype ) + { + List recordNosList = new ArrayList(); + + // Connection con = dbConnection.openConnection(); + + Statement st = null; + + // ResultSet rs1 = null; + + String query = ""; + + int dataElementid = 1020; + + /* + * if( lltype.equalsIgnoreCase( "lllivebirth" ) ) dataElementid = + * LLDataSets.LLB_CHILD_NAME; else if( lltype.equalsIgnoreCase( + * "lllivebirth" ) ) dataElementid = LLDataSets.LLD_CHILD_NAME; else if( + * lltype.equalsIgnoreCase( "lllivebirth" ) ) dataElementid = + * LLDataSets.LLMD_MOTHER_NAME; + */ + + if ( lltype.equalsIgnoreCase( "lllivebirth-l4" ) || lltype.equalsIgnoreCase( "lllivebirth-l5" ) + || lltype.equalsIgnoreCase( "lllivebirth-l6" ) ) + dataElementid = 1020; + else if ( lltype.equalsIgnoreCase( "lldeath-l4" ) || lltype.equalsIgnoreCase( "lldeath-l5" ) + || lltype.equalsIgnoreCase( "lldeath-l6" ) ) + dataElementid = 1027; + else if ( lltype.equalsIgnoreCase( "llmaternaldeath-l4" ) || lltype.equalsIgnoreCase( "llmaternaldeath-l5" ) + || lltype.equalsIgnoreCase( "llmaternaldeath-l6" ) ) + dataElementid = 1032; + + try + { + // st = con.createStatement(); + + query = "SELECT recordno FROM lldatavalue WHERE dataelementid = " + dataElementid + " AND periodid = " + + period.getId() + " AND sourceid = " + organisationUnit.getId(); + // rs1 = st.executeQuery( query ); + + SqlRowSet rs1 = jdbcTemplate.queryForRowSet( query ); + + while ( rs1.next() ) + { + recordNosList.add( rs1.getInt( 1 ) ); + } + + Collections.sort( recordNosList ); + } + catch ( Exception e ) + { + System.out.println( "SQL Exception : " + e.getMessage() ); + return null; + } + /* + * finally { try { SqlRowSet rs1; + * + * if ( st != null ) st.close(); if ( rs1 != null ) rs1.close(); + * + * if ( con != null ) con.close(); } catch ( Exception e ) { + * System.out.println( "SQL Exception : " + e.getMessage() ); return + * null; } } + */// finally block end + + return recordNosList; + } + + public String getRAFolderName() + { + // Connection con = dbConnection.openConnection(); + + // Statement st = null; + + // ResultSet rs1 = null; + + String raFolderName = "ra_national"; + + try + { + // st = con.createStatement(); + + /* + * query = "SELECT mvalue FROM maintenancein WHERE mkey LIKE '" + + * KEY_RAFOLDER + "'"; //rs1 = st.executeQuery( query ); + * + * SqlRowSet rs1 = jdbcTemplate.queryForRowSet( query ); + * + * if ( rs1.next() ) { raFolderName = rs1.getString( 1 ); } + */ + + raFolderName = configurationService.getConfigurationByKey( Configuration_IN.KEY_REPORTFOLDER ).getValue(); + + } + catch ( Exception e ) + { + System.out.println( "Exception : " + e.getMessage() ); + return null; + } + + return raFolderName; + + } + + /* + * Returns the PeriodType Object for selected DataElement, If no PeriodType + * is found then by default returns Monthly Period type + */ + public PeriodType getDataElementPeriodType( DataElement de ) + { + List dataSetList = new ArrayList( dataSetService.getAllDataSets() ); + Iterator it = dataSetList.iterator(); + while ( it.hasNext() ) + { + DataSet ds = (DataSet) it.next(); + List dataElementList = new ArrayList( ds.getDataElements() ); + if ( dataElementList.contains( de ) ) + { + return ds.getPeriodType(); + } + } + + return null; + + } // getDataElementPeriodType end + + private String getResultDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit ) + { + try + { + // System.out.println( "expression : " + formula + " ***** " + + // String.valueOf( startDate ) + " **** " + // + String.valueOf( endDate ) ); + + int deFlag1 = 0; + int deFlag2 = 0; + Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); + + Matcher matcher = pattern.matcher( formula ); + StringBuffer buffer = new StringBuffer(); + + String resultValue = ""; + + while ( matcher.find() ) + { + String replaceString = matcher.group(); + + replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); + String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1, + replaceString.length() ); + + replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); + + int dataElementId = Integer.parseInt( replaceString ); + int optionComboId = Integer.parseInt( optionComboIdStr ); + + DataElement dataElement = dataElementService.getDataElement( dataElementId ); + DataElementCategoryOptionCombo optionCombo = dataElementCategoryService + .getDataElementCategoryOptionCombo( optionComboId ); + + if ( dataElement == null || optionCombo == null ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + } + if ( dataElement.getType().equalsIgnoreCase( "int" ) ) + { + Double aggregatedValue = aggregationService.getAggregatedDataValue( dataElement, optionCombo, + startDate, endDate, organisationUnit ); + if ( aggregatedValue == null ) + { + replaceString = NULL_REPLACEMENT; + } + else + { + replaceString = String.valueOf( aggregatedValue ); + + deFlag2 = 1; + } + + } + else + { + deFlag1 = 1; + PeriodType dePeriodType = getDataElementPeriodType( dataElement ); + List periodList = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( + dePeriodType, startDate, endDate ) ); + Period tempPeriod = new Period(); + if ( periodList == null || periodList.isEmpty() ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + } + else + { + tempPeriod = (Period) periodList.get( 0 ); + } + + DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod, + optionCombo ); + + if ( dataValue != null ) + { + // Works for both text and boolean data types + + replaceString = dataValue.getValue(); + } + + else + replaceString = ""; + + if ( replaceString == null ) + replaceString = ""; + } + matcher.appendReplacement( buffer, replaceString ); + + resultValue = replaceString; + } + + matcher.appendTail( buffer ); + + if ( deFlag1 == 0 ) + { + + double d = 0.0; + try + { + d = MathUtils.calculateExpression( buffer.toString() ); + } + catch ( Exception e ) + { + d = 0.0; + resultValue = ""; + } + if ( d == -1 ) + { + d = 0.0; + resultValue = ""; + } + else + { + + // This is to display financial data as it is like 2.1476838 + resultValue = "" + d; + + // These lines are to display financial data that do not + // have decimals + d = d * 10; + + if ( d % 10 == 0 ) + { + resultValue = "" + (int) d / 10; + } + + d = d / 10; + + // These line are to display non financial data that do not + // require decimals + // if ( !(reportModelTB.equalsIgnoreCase( "STATIC-FINANCIAL" + // )) ) + resultValue = "" + (int) d; + + // if ( resultValue.equalsIgnoreCase( "0" ) ) + // { + // resultValue = ""; + // } + } + + } + else + { + resultValue = buffer.toString(); + } + + if ( resultValue.equalsIgnoreCase( "" ) ) + resultValue = " "; + + return resultValue; + } + catch ( NumberFormatException ex ) + { + throw new RuntimeException( "Illegal DataElement id", ex ); + } + } + + private String getIndividualResultDataValue( String formula, Date startDate, Date endDate, + OrganisationUnit organisationUnit ) + { + try + { + int deFlag1 = 0; + int deFlag2 = 0; + Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); + + Matcher matcher = pattern.matcher( formula ); + StringBuffer buffer = new StringBuffer(); + + String resultValue = ""; + boolean valueDoesNotExist = true; + + while ( matcher.find() ) + { + + String replaceString = matcher.group(); + + replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); + String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1, + replaceString.length() ); + + replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); + + int dataElementId = Integer.parseInt( replaceString ); + int optionComboId = Integer.parseInt( optionComboIdStr ); + + DataElement dataElement = dataElementService.getDataElement( dataElementId ); + DataElementCategoryOptionCombo optionCombo = dataElementCategoryService + .getDataElementCategoryOptionCombo( optionComboId ); + + if ( dataElement == null || optionCombo == null ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + } + if ( dataElement.getType().equalsIgnoreCase( "int" ) ) + { + + PeriodType dePeriodType = getDataElementPeriodType( dataElement ); + List periodList = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( + dePeriodType, startDate, endDate ) ); + + if ( periodList == null || periodList.isEmpty() ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + } + else + { + + double aggregatedValue = 0.0; + for ( Period tempPeriod : periodList ) + { + DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, + tempPeriod, optionCombo ); + + if ( dataValue != null ) + { + aggregatedValue += Double.parseDouble( dataValue.getValue() ); + + valueDoesNotExist = false; + } + } + + replaceString = String.valueOf( aggregatedValue ); + + deFlag2 = 1; + } + + } + else + { + deFlag1 = 1; + PeriodType dePeriodType = getDataElementPeriodType( dataElement ); + List periodList = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( + dePeriodType, startDate, endDate ) ); + Period tempPeriod = new Period(); + if ( periodList == null || periodList.isEmpty() ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + } + else + { + tempPeriod = (Period) periodList.get( 0 ); + } + + DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod, + optionCombo ); + + if ( dataValue != null ) + { + // Works for both text and boolean data types + + replaceString = dataValue.getValue(); + valueDoesNotExist = false; + } + + else + replaceString = ""; + + if ( replaceString == null ) + replaceString = ""; + } + matcher.appendReplacement( buffer, replaceString ); + + resultValue = replaceString; + } + + matcher.appendTail( buffer ); + + if ( deFlag1 == 0 ) + { + double d = 0.0; + try + { + d = MathUtils.calculateExpression( buffer.toString() ); + } + catch ( Exception e ) + { + d = 0.0; + + resultValue = ""; + } + if ( d == -1 ) + { + d = 0.0; + + resultValue = ""; + } + else + { + // This is to display financial data as it is like 2.1476838 + resultValue = "" + d; + + // These lines are to display financial data that do not + // have decimals + d = d * 10; + + if ( d % 10 == 0 ) + { + resultValue = "" + (int) d / 10; + } + + d = d / 10; + + // These line are to display non financial data that do not + // require decimals + // if ( !(reportModelTB.equalsIgnoreCase( "STATIC-FINANCIAL" + // )) ) + resultValue = "" + (int) d; + + // if ( resultValue.equalsIgnoreCase( "0" ) ) + // { + // resultValue = ""; + // } + } + } + else + { + resultValue = buffer.toString(); + } + + if ( valueDoesNotExist ) + resultValue = " "; + + if ( resultValue.equalsIgnoreCase( "" ) ) + resultValue = " "; + + return resultValue; + } + catch ( NumberFormatException ex ) + { + throw new RuntimeException( "Illegal DataElement id", ex ); + } + } + + private String getBooleanDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit ) + { + try + { + int deFlag1 = 0; + int deFlag2 = 0; + Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); + + Matcher matcher = pattern.matcher( formula ); + StringBuffer buffer = new StringBuffer(); + + while ( matcher.find() ) + { + String replaceString = matcher.group(); + + replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); + String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1, + replaceString.length() ); + + replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); + + int dataElementId = Integer.parseInt( replaceString ); + int optionComboId = Integer.parseInt( optionComboIdStr ); + + DataElement dataElement = dataElementService.getDataElement( dataElementId ); + DataElementCategoryOptionCombo optionCombo = dataElementCategoryService + .getDataElementCategoryOptionCombo( optionComboId ); + + if ( dataElement == null || optionCombo == null ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + } + + if ( dataElement.getType().equalsIgnoreCase( "bool" ) ) + { + deFlag1 = 1; + PeriodType dePeriodType = getDataElementPeriodType( dataElement ); + List periodList = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( + dePeriodType, startDate, endDate ) ); + Period tempPeriod = new Period(); + if ( periodList == null || periodList.isEmpty() ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + } + else + { + tempPeriod = (Period) periodList.get( 0 ); + } + + DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod, + optionCombo ); + + if ( dataValue != null ) + { + // Works for both text and boolean data types + + if ( dataValue.getValue().equalsIgnoreCase( "true" ) ) + { + replaceString = "Yes"; + } + else if ( dataValue.getValue().equalsIgnoreCase( "false" ) ) + { + replaceString = "No"; + } + else + { + replaceString = dataValue.getValue(); + } + } + + else + { + replaceString = ""; + } + + } + else + { + Double aggregatedValue = aggregationService.getAggregatedDataValue( dataElement, optionCombo, + startDate, endDate, organisationUnit ); + if ( aggregatedValue == null ) + { + replaceString = NULL_REPLACEMENT; + } + else + { + replaceString = String.valueOf( aggregatedValue ); + + deFlag2 = 1; + } + } + matcher.appendReplacement( buffer, replaceString ); + } + + matcher.appendTail( buffer ); + + String resultValue = ""; + if ( deFlag1 == 0 ) + { + double d = 0.0; + try + { + d = MathUtils.calculateExpression( buffer.toString() ); + } + catch ( Exception e ) + { + d = 0.0; + } + if ( d == -1 ) + { + d = 0.0; + } + else + { + d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 ); + resultValue = "" + (int) d; + } + + if ( deFlag2 == 0 ) + { + resultValue = " "; + } + } + else + { + resultValue = buffer.toString(); + } + return resultValue; + } + catch ( NumberFormatException ex ) + { + throw new RuntimeException( "Illegal DataElement id", ex ); + } + } + + private String getResultIndicatorValue( String formula, Date startDate, Date endDate, + OrganisationUnit organisationUnit ) + { + try + { + + int deFlag1 = 0; + int deFlag2 = 0; + Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); + + Matcher matcher = pattern.matcher( formula ); + StringBuffer buffer = new StringBuffer(); + + while ( matcher.find() ) + { + String replaceString = matcher.group(); + + replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); + + replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); + + int indicatorId = Integer.parseInt( replaceString ); + + Indicator indicator = indicatorService.getIndicator( indicatorId ); + + if ( indicator == null ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + + } + + Double aggregatedValue = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate, + organisationUnit ); + + if ( aggregatedValue == null ) + { + replaceString = NULL_REPLACEMENT; + } + else + { + replaceString = String.valueOf( aggregatedValue ); + deFlag2 = 1; + } + matcher.appendReplacement( buffer, replaceString ); + } + + matcher.appendTail( buffer ); + + String resultValue = ""; + if ( deFlag1 == 0 ) + { + double d = 0.0; + try + { + d = MathUtils.calculateExpression( buffer.toString() ); + } + catch ( Exception e ) + { + d = 0.0; + } + if ( d == -1 ) + d = 0.0; + else + { + d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 ); + resultValue = "" + d; + } + + if ( deFlag2 == 0 ) + { + resultValue = " "; + } + } + else + { + resultValue = buffer.toString(); + } + return resultValue; + } + catch ( NumberFormatException ex ) + { + throw new RuntimeException( "Illegal DataElement id", ex ); + } + } + + private String getIndividualResultIndicatorValue( String formula, Date startDate, Date endDate, + OrganisationUnit organisationUnit ) + { + try + { + + int deFlag1 = 0; + int deFlag2 = 0; + Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" ); + + Matcher matcher = pattern.matcher( formula ); + StringBuffer buffer = new StringBuffer(); + + while ( matcher.find() ) + { + String replaceString = matcher.group(); + + replaceString = replaceString.replaceAll( "[\\[\\]]", "" ); + + replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) ); + + int indicatorId = Integer.parseInt( replaceString ); + + Indicator indicator = indicatorService.getIndicator( indicatorId ); + + if ( indicator == null ) + { + replaceString = ""; + matcher.appendReplacement( buffer, replaceString ); + continue; + + } + + String numeratorExp = indicator.getNumerator(); + String denominatorExp = indicator.getDenominator(); + int indicatorFactor = indicator.getIndicatorType().getFactor(); + String numeratorVal = getIndividualResultDataValue( numeratorExp, startDate, endDate, organisationUnit ); + String denominatorVal = getIndividualResultDataValue( denominatorExp, startDate, endDate, + organisationUnit ); + + double numeratorValue; + try + { + numeratorValue = Double.parseDouble( numeratorVal ); + } + catch ( Exception e ) + { + System.out.println( "Exception while getting Numerator : " + numeratorExp + " for Indicaotr " + + indicator.getName() ); + numeratorValue = 0.0; + } + + double denominatorValue; + try + { + denominatorValue = Double.parseDouble( denominatorVal ); + } + catch ( Exception e ) + { + System.out.println( "Exception while getting Deniminator : " + denominatorExp + " for Indicaotr " + + indicator.getName() ); + denominatorValue = 1.0; + } + + double aggregatedValue; + try + { + aggregatedValue = (numeratorValue / denominatorValue) * indicatorFactor; + } + catch ( Exception e ) + { + System.out.println( "Exception while calculating Indicator value for Indicaotr " + + indicator.getName() ); + aggregatedValue = 0.0; + } + + replaceString = String.valueOf( aggregatedValue ); + deFlag2 = 1; + + matcher.appendReplacement( buffer, replaceString ); + } + + matcher.appendTail( buffer ); + + String resultValue = ""; + if ( deFlag1 == 0 ) + { + double d = 0.0; + try + { + d = MathUtils.calculateExpression( buffer.toString() ); + } + catch ( Exception e ) + { + d = 0.0; + } + if ( d == -1 ) + d = 0.0; + else + { + d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 ); + resultValue = "" + d; + } + + if ( deFlag2 == 0 ) + { + resultValue = " "; + } + } + else + { + resultValue = buffer.toString(); + } + return resultValue; + } + catch ( NumberFormatException ex ) + { + throw new RuntimeException( "Illegal DataElement id", ex ); + } + } + + + public Period getSelectedPeriod( String startDate, PeriodType periodType ) throws Exception + { + SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" ); + + List periods = new ArrayList( periodService.getPeriodsByPeriodType( periodType ) ); + for ( Period period : periods ) + { + String tempDate = dateFormat.format( period.getStartDate() ); + if ( tempDate.equalsIgnoreCase( startDate ) ) + { + return period; + } + } + + Period period = periodType.createPeriod( dateFormat.parse( startDate ) ); + period = reloadPeriodForceAdd( period ); + periodService.addPeriod( period ); + + return period; + } + + private final Period reloadPeriod( Period period ) + { + return periodService.getPeriod( period.getStartDate(), period.getEndDate(), period.getPeriodType() ); + } + + private final Period reloadPeriodForceAdd( Period period ) + { + Period storedPeriod = reloadPeriod( period ); + + if ( storedPeriod == null ) + { + periodService.addPeriod( period ); + + return period; + } + + return storedPeriod; + } + +} === added file 'local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/TCSXMLMap.java' --- local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/TCSXMLMap.java 1970-01-01 00:00:00 +0000 +++ local/in/dhis-web-excelimport/src/main/java/org/hisp/dhis/excelimport/util/TCSXMLMap.java 2011-04-15 11:42:21 +0000 @@ -0,0 +1,84 @@ +package org.hisp.dhis.excelimport.util; + +import java.io.Serializable; + +public class TCSXMLMap implements Serializable +{ + private String tcsDataElement; + private String dhisDataElement; + private String orgunitCode; + private String tscPeriod; + private String dataValue; + + // ------------------------------------------------------------------------- + // Constructors + // ------------------------------------------------------------------------- + public TCSXMLMap() + { + + } + + public TCSXMLMap( String tcsDataElement, String dhisDataElement, String orgunitCode, String tscPeriod, String dataValue ) + { + this.tcsDataElement = tcsDataElement; + this.dhisDataElement = dhisDataElement; + this.orgunitCode = orgunitCode; + this.tscPeriod = tscPeriod; + this.dataValue = dataValue; + } + + // ------------------------------------------------------------------------- + // Getters and setters + // ------------------------------------------------------------------------- + + public String getTcsDataElement() + { + return tcsDataElement; + } + + public void setTcsDataElement( String tcsDataElement ) + { + this.tcsDataElement = tcsDataElement; + } + + public String getDhisDataElement() + { + return dhisDataElement; + } + + public void setDhisDataElement( String dhisDataElement ) + { + this.dhisDataElement = dhisDataElement; + } + + public String getOrgunitCode() + { + return orgunitCode; + } + + public void setOrgunitCode( String orgunitCode ) + { + this.orgunitCode = orgunitCode; + } + + public String getTscPeriod() + { + return tscPeriod; + } + + public void setTscPeriod( String tscPeriod ) + { + this.tscPeriod = tscPeriod; + } + + public String getDataValue() + { + return dataValue; + } + + public void setDataValue( String dataValue ) + { + this.dataValue = dataValue; + } + +}