=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObject.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObject.java 2012-12-27 14:07:54 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObject.java 2013-02-13 00:45:31 +0000 @@ -40,7 +40,7 @@ enum IdentifiableProperty { - ID, UID, NAME, CODE + ID, UID, UUID, NAME, CODE } int getId(); === modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java' --- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java 2012-09-10 09:48:06 +0000 +++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java 2013-02-13 00:45:31 +0000 @@ -27,22 +27,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static org.hisp.dhis.importexport.ImportStrategy.NEW; -import static org.hisp.dhis.importexport.ImportStrategy.NEW_AND_UPDATES; -import static org.hisp.dhis.importexport.ImportStrategy.UPDATES; -import static org.hisp.dhis.scheduling.TaskCategory.DATAVALUE_IMPORT; -import static org.hisp.dhis.system.notification.NotificationLevel.INFO; -import static org.hisp.dhis.system.notification.NotificationLevel.ERROR; -import static org.hisp.dhis.system.util.ConversionUtils.wrap; -import static org.hisp.dhis.system.util.DateUtils.getDefaultDate; - -import java.io.*; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - +import au.com.bytecode.opencsv.CSVReader; import org.amplecode.quick.BatchHandler; import org.amplecode.quick.BatchHandlerFactory; import org.amplecode.staxwax.factory.XMLFactory; @@ -78,7 +63,22 @@ import org.hisp.dhis.user.CurrentUserService; import org.springframework.beans.factory.annotation.Autowired; -import au.com.bytecode.opencsv.CSVReader; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.hisp.dhis.importexport.ImportStrategy.*; +import static org.hisp.dhis.scheduling.TaskCategory.DATAVALUE_IMPORT; +import static org.hisp.dhis.system.notification.NotificationLevel.ERROR; +import static org.hisp.dhis.system.notification.NotificationLevel.INFO; +import static org.hisp.dhis.system.util.ConversionUtils.wrap; +import static org.hisp.dhis.system.util.DateUtils.getDefaultDate; /** * @author Lars Helge Overland @@ -87,42 +87,42 @@ implements DataValueSetService { private static final Log log = LogFactory.getLog( DefaultDataValueSetService.class ); - + private static final String ERROR_INVALID_DATA_SET = "Invalid data set: "; private static final String ERROR_INVALID_PERIOD = "Invalid period: "; private static final String ERROR_INVALID_ORG_UNIT = "Invalid org unit: "; private static final String ERROR_OBJECT_NEEDED_TO_COMPLETE = "Must be provided to complete data set"; - + @Autowired private IdentifiableObjectManager identifiableObjectManager; - + @Autowired private DataElementCategoryService categoryService; - + @Autowired private DataSetService dataSetService; - + @Autowired private OrganisationUnitService organisationUnitService; - + @Autowired private PeriodService periodService; - + @Autowired private BatchHandlerFactory batchHandlerFactory; - + @Autowired private CompleteDataSetRegistrationService registrationService; - + @Autowired private CurrentUserService currentUserService; @Autowired private DataValueSetStore dataValueSetStore; - + @Autowired private Notifier notifier; - + //-------------------------------------------------------------------------- // DataValueSet implementation //-------------------------------------------------------------------------- @@ -132,45 +132,45 @@ DataSet dataSet_ = dataSetService.getDataSet( dataSet ); Period period_ = PeriodType.getPeriodFromIsoString( period ); OrganisationUnit orgUnit_ = organisationUnitService.getOrganisationUnit( orgUnit ); - + if ( dataSet_ == null ) { throw new IllegalArgumentException( ERROR_INVALID_DATA_SET + dataSet ); } - + if ( period_ == null ) { throw new IllegalArgumentException( ERROR_INVALID_PERIOD + period ); } - + if ( orgUnit_ == null ) { throw new IllegalArgumentException( ERROR_INVALID_ORG_UNIT + orgUnit ); } - + CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration( dataSet_, period_, orgUnit_ ); - + Date completeDate = registration != null ? registration.getDate() : null; - + period_ = periodService.reloadPeriod( period_ ); - + dataValueSetStore.writeDataValueSetXml( dataSet_, completeDate, period_, orgUnit_, dataSet_.getDataElements(), wrap( period_ ), wrap( orgUnit_ ), out ); } public void writeDataValueSet( Set dataSets, Date startDate, Date endDate, Set orgUnits, OutputStream out ) { Set periods = new HashSet( periodService.getPeriodsBetweenDates( startDate, endDate ) ); - + dataValueSetStore.writeDataValueSetXml( null, null, null, null, getDataElements( dataSets ), periods, getOrgUnits( orgUnits ), out ); } public void writeDataValueSetCsv( Set dataSets, Date startDate, Date endDate, Set orgUnits, Writer writer ) { Set periods = new HashSet( periodService.getPeriodsBetweenDates( startDate, endDate ) ); - + dataValueSetStore.writeDataValueSetCsv( getDataElements( dataSets ), periods, getOrgUnits( orgUnits ), writer ); } - + public ImportSummary saveDataValueSet( InputStream in ) { return saveDataValueSet( in, ImportOptions.getDefaultImportOptions(), null ); @@ -195,7 +195,7 @@ { try { - DataValueSet dataValueSet = new StreamingDataValueSet( XMLFactory.getXMLReader( in ) ); + DataValueSet dataValueSet = new StreamingDataValueSet( XMLFactory.getXMLReader( in ) ); return saveDataValueSet( importOptions, id, dataValueSet ); } catch ( RuntimeException ex ) @@ -225,7 +225,7 @@ { try { - DataValueSet dataValueSet = new StreamingCsvDataValueSet( new CSVReader( reader ) ); + DataValueSet dataValueSet = new StreamingCsvDataValueSet( new CSVReader( reader ) ); return saveDataValueSet( importOptions, id, dataValueSet ); } catch ( RuntimeException ex ) @@ -239,26 +239,27 @@ private ImportSummary saveDataValueSet( ImportOptions importOptions, TaskId id, DataValueSet dataValueSet ) { notifier.clear( id, DATAVALUE_IMPORT ).notify( id, DATAVALUE_IMPORT, "Process started" ); - + ImportSummary summary = new ImportSummary(); - + importOptions = importOptions != null ? importOptions : ImportOptions.getDefaultImportOptions(); - + IdentifiableProperty dataElementIdScheme = dataValueSet.getDataElementIdScheme() != null ? IdentifiableProperty.valueOf( dataValueSet.getDataElementIdScheme().toUpperCase() ) : importOptions.getDataElementIdScheme(); IdentifiableProperty orgUnitIdScheme = dataValueSet.getOrgUnitIdScheme() != null ? IdentifiableProperty.valueOf( dataValueSet.getOrgUnitIdScheme().toUpperCase() ) : importOptions.getOrgUnitIdScheme(); boolean dryRun = dataValueSet.getDryRun() != null ? dataValueSet.getDryRun() : importOptions.isDryRun(); ImportStrategy strategy = dataValueSet.getStrategy() != null ? ImportStrategy.valueOf( dataValueSet.getStrategy() ) : importOptions.getImportStrategy(); boolean skipExistingCheck = importOptions.isSkipExistingCheck(); - + Map dataElementMap = identifiableObjectManager.getIdMap( DataElement.class, dataElementIdScheme ); Map orgUnitMap = identifiableObjectManager.getIdMap( OrganisationUnit.class, orgUnitIdScheme ); Map categoryOptionComboMap = identifiableObjectManager.getIdMap( DataElementCategoryOptionCombo.class, IdentifiableProperty.UID ); Map periodMap = new HashMap(); - + DataSet dataSet = dataValueSet.getDataSet() != null ? identifiableObjectManager.getObject( DataSet.class, IdentifiableProperty.UID, dataValueSet.getDataSet() ) : null; Date completeDate = getDefaultDate( dataValueSet.getCompleteDate() ); - + Period outerPeriod = PeriodType.getPeriodFromIsoString( dataValueSet.getPeriod() ); + OrganisationUnit outerOrgUnit = dataValueSet.getOrgUnit() != null ? identifiableObjectManager.getObject( OrganisationUnit.class, orgUnitIdScheme, dataValueSet.getOrgUnit() ) : null; if ( dataSet != null && completeDate != null ) @@ -270,7 +271,7 @@ { summary.setDataSetComplete( Boolean.FALSE.toString() ); } - + DataElementCategoryOptionCombo fallbackCategoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo(); BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataValueBatchHandler.class ).init(); @@ -278,23 +279,23 @@ int importCount = 0; int updateCount = 0; int totalCount = 0; - + notifier.notify( id, DATAVALUE_IMPORT, "Importing data values" ); log.info( "importing data values" ); while ( dataValueSet.hasNextDataValue() ) { org.hisp.dhis.dxf2.datavalue.DataValue dataValue = dataValueSet.getNextDataValue(); - + DataValue internalValue = new DataValue(); totalCount++; - + DataElement dataElement = dataElementMap.get( dataValue.getDataElement() ); DataElementCategoryOptionCombo categoryOptionCombo = categoryOptionComboMap.get( dataValue.getCategoryOptionCombo() ); Period period = outerPeriod != null ? outerPeriod : PeriodType.getPeriodFromIsoString( dataValue.getPeriod() ); OrganisationUnit orgUnit = outerOrgUnit != null ? outerOrgUnit : orgUnitMap.get( dataValue.getOrgUnit() ); - + if ( dataElement == null ) { summary.getConflicts().add( new ImportConflict( DataElement.class.getSimpleName(), dataValue.getDataElement() ) ); @@ -306,7 +307,7 @@ summary.getConflicts().add( new ImportConflict( Period.class.getSimpleName(), dataValue.getPeriod() ) ); continue; } - + if ( orgUnit == null ) { summary.getConflicts().add( new ImportConflict( OrganisationUnit.class.getSimpleName(), dataValue.getOrgUnit() ) ); @@ -317,12 +318,12 @@ { categoryOptionCombo = fallbackCategoryOptionCombo; } - + if ( dataValue.getValue() == null && dataValue.getComment() == null ) { continue; } - + if ( periodMap.containsKey( dataValue.getPeriod() ) ) { period = periodMap.get( dataValue.getPeriod() ); @@ -332,17 +333,26 @@ period = periodService.reloadPeriod( period ); periodMap.put( dataValue.getPeriod(), period ); } - + internalValue.setDataElement( dataElement ); internalValue.setPeriod( period ); internalValue.setSource( orgUnit ); internalValue.setOptionCombo( categoryOptionCombo ); internalValue.setValue( dataValue.getValue() ); - internalValue.setStoredBy( dataValue.getStoredBy() ); + + if ( dataValue.getStoredBy() == null || dataValue.getStoredBy().trim().isEmpty() ) + { + internalValue.setStoredBy( currentUserService.getCurrentUsername() ); + } + else + { + internalValue.setStoredBy( dataValue.getStoredBy() ); + } + internalValue.setTimestamp( getDefaultDate( dataValue.getTimestamp() ) ); internalValue.setComment( dataValue.getComment() ); internalValue.setFollowup( dataValue.getFollowup() ); - + if ( !skipExistingCheck && batchHandler.objectExists( internalValue ) ) { if ( NEW_AND_UPDATES.equals( strategy ) || UPDATES.equals( strategy ) ) @@ -353,7 +363,7 @@ } updateCount++; - } + } } else { @@ -363,22 +373,22 @@ { batchHandler.addObject( internalValue ); } - + importCount++; } } } - + batchHandler.flush(); int ignores = totalCount - importCount - updateCount; - + summary.setDataValueCount( new ImportCount( importCount, updateCount, ignores ) ); summary.setStatus( ImportStatus.SUCCESS ); summary.setDescription( "Import process completed successfully" ); - + notifier.notify( id, DATAVALUE_IMPORT, INFO, "Import done", true ).addTaskSummary( id, DATAVALUE_IMPORT, summary ); - + return summary; } @@ -387,77 +397,77 @@ //-------------------------------------------------------------------------- private void handleComplete( DataSet dataSet, Date completeDate, OrganisationUnit orgUnit, Period period, ImportSummary summary ) - { + { if ( orgUnit == null ) { summary.getConflicts().add( new ImportConflict( OrganisationUnit.class.getSimpleName(), ERROR_OBJECT_NEEDED_TO_COMPLETE ) ); return; } - + if ( period == null ) { summary.getConflicts().add( new ImportConflict( Period.class.getSimpleName(), ERROR_OBJECT_NEEDED_TO_COMPLETE ) ); return; } - + period = periodService.reloadPeriod( period ); - + CompleteDataSetRegistration completeAlready = registrationService.getCompleteDataSetRegistration( dataSet, period, orgUnit ); String username = currentUserService.getCurrentUsername(); - + if ( completeAlready != null ) { completeAlready.setStoredBy( username ); completeAlready.setDate( completeDate ); - + registrationService.updateCompleteDataSetRegistration( completeAlready ); - } + } else { CompleteDataSetRegistration registration = new CompleteDataSetRegistration( dataSet, period, orgUnit, completeDate, username ); - + registrationService.saveCompleteDataSetRegistration( registration ); } - + summary.setDataSetComplete( DateUtils.getMediumDateString( completeDate ) ); } - + private Set getDataElements( Set dataSets ) { Set dataElements = new HashSet(); - + for ( String ds : dataSets ) { DataSet dataSet = dataSetService.getDataSet( ds ); - + if ( dataSet == null ) { throw new IllegalArgumentException( ERROR_INVALID_DATA_SET + ds ); } - + dataElements.addAll( dataSet.getDataElements() ); } - + return dataElements; } - + public Set getOrgUnits( Set orgUnits ) { Set organisationUnits = new HashSet(); - + for ( String ou : orgUnits ) { - OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit( ou ); + OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit( ou ); if ( orgUnit == null ) { throw new IllegalArgumentException( ERROR_INVALID_ORG_UNIT + ou ); } - + organisationUnits.add( orgUnit ); } - + return organisationUnits; } } === modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java' --- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java 2012-07-30 10:56:25 +0000 +++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java 2013-02-13 00:45:31 +0000 @@ -27,8 +27,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty; import org.hisp.dhis.importexport.ImportStrategy; -import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty; /** * @author Morten Olav Hansen @@ -36,9 +36,9 @@ public class ImportOptions { private IdentifiableProperty dataElementIdScheme; - + private IdentifiableProperty orgUnitIdScheme; - + private boolean dryRun = false; private ImportStrategy importStrategy; @@ -55,7 +55,7 @@ public ImportOptions() { } - + public ImportOptions( ImportStrategy importStrategy ) { this.importStrategy = importStrategy; @@ -136,7 +136,7 @@ @Override public String toString() { - return "[data element id scheme: " + dataElementIdScheme + ", org unit id scheme: " + + return "[data element id scheme: " + dataElementIdScheme + ", org unit id scheme: " + orgUnitIdScheme + ", dry run: " + dryRun + ", strategy: " + importStrategy + ", skip check: " + skipExistingCheck + "]"; } }