use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException in project dhis2-core by dhis2.
the class DefaultMetadataVersionService method saveVersion.
/**
* This method is taking care of 3 steps:
* 1. Generating a metadata snapshot (using the ExportService)
* 2. Saving that snapshot to the DataStore
* 3. Creating the actual MetadataVersion entry.
*/
@Override
public synchronized boolean saveVersion(VersionType versionType) {
MetadataVersion currentVersion = getCurrentVersion();
String versionName = MetadataVersionNameGenerator.getNextVersionName(currentVersion);
Date minDate;
if (currentVersion == null) {
minDate = null;
} else {
minDate = currentVersion.getCreated();
}
//1. Get export of metadata
ByteArrayOutputStream os = getMetadataExport(minDate);
//2. Save the metadata snapshot in DHIS Data Store
String value = getBodyAsString(StandardCharsets.UTF_8, os);
createMetadataVersionInDataStore(versionName, value);
//3. Create an entry for the MetadataVersion
MetadataVersion version = new MetadataVersion();
version.setName(versionName);
version.setCreated(new Date());
version.setType(versionType);
try {
String hashCode = HashCodeGenerator.getHashCode(value);
version.setHashCode(hashCode);
} catch (NoSuchAlgorithmException e) {
String message = "Exception occurred while generating MetadataVersion HashCode " + e.getMessage();
log.error(message, e);
throw new MetadataVersionServiceException(message, e);
}
try {
addVersion(version);
metadataSystemSettingService.setSystemMetadataVersion(version.getName());
} catch (Exception ex) {
String message = "Exception occurred while saving a new MetadataVersion " + ex.getMessage();
log.error(message, ex);
throw new MetadataVersionServiceException(message, ex);
}
return true;
}
use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException in project dhis2-core by dhis2.
the class DefaultMetadataVersionService method getMetadataExport.
//--------------------------------------------------------------------------
// Private methods
//--------------------------------------------------------------------------
/**
* Generates the metadata export based on the created date of the current version.
*/
private ByteArrayOutputStream getMetadataExport(Date minDate) {
ByteArrayOutputStream os = null;
try {
MetadataExportParams exportParams = new MetadataExportParams();
if (minDate != null) {
List<String> defaultFilterList = new ArrayList<String>();
defaultFilterList.add("lastUpdated:gte:" + DateUtils.getLongGmtDateString(minDate));
exportParams.setDefaultFilter(defaultFilterList);
metadataExportService.validate(exportParams);
}
os = new ByteArrayOutputStream(1024);
RootNode metadata = metadataExportService.getMetadataAsNode(exportParams);
nodeService.serialize(metadata, "application/json", os);
} catch (//We have to catch the "Exception" object as no specific exception on the contract.
Exception ex) {
String message = "Exception occurred while exporting metadata for capturing a metadata version" + ex.getMessage();
log.error(message, ex);
throw new MetadataVersionServiceException(message, ex);
}
return os;
}
use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException in project dhis2-core by dhis2.
the class MetadataVersionDelegate method getMetaDataDifference.
public List<MetadataVersion> getMetaDataDifference(MetadataVersion metadataVersion) {
String url;
List<MetadataVersion> metadataVersions = new ArrayList<>();
if (metadataVersion == null) {
url = metadataSystemSettingService.getEntireVersionHistory();
} else {
url = metadataSystemSettingService.getMetaDataDifferenceURL(metadataVersion.getName());
}
DhisHttpResponse dhisHttpResponse = getDhisHttpResponse(url, VERSION_TIMEOUT);
if (isValidDhisHttpResponse(dhisHttpResponse)) {
try {
metadataVersions = renderService.fromMetadataVersion(new ByteArrayInputStream(dhisHttpResponse.getResponse().getBytes()), RenderFormat.JSON);
return metadataVersions;
} catch (IOException io) {
String message = "Exception occurred while trying to do JSON conversion. Caused by: " + io.getMessage();
log.error(message, io);
throw new MetadataVersionServiceException(message, io);
}
}
log.warn("Returning empty for the metadata versions difference");
return metadataVersions;
}
use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException in project dhis2-core by dhis2.
the class MetadataVersionDelegate method getDhisHttpResponse.
//----------------------------------------------------------------------------------------
// Private Methods
//----------------------------------------------------------------------------------------
private DhisHttpResponse getDhisHttpResponse(String url, int timeout) {
AvailabilityStatus remoteServerAvailable = synchronizationManager.isRemoteServerAvailable();
if (!(remoteServerAvailable.isAvailable())) {
String message = remoteServerAvailable.getMessage();
log.error(message);
throw new RemoteServerUnavailableException(message);
}
String username = metadataSystemSettingService.getRemoteInstanceUserName();
String password = metadataSystemSettingService.getRemoteInstancePassword();
log.info("Remote server metadata version URL: " + url + ", username: " + username);
DhisHttpResponse dhisHttpResponse = null;
try {
dhisHttpResponse = HttpUtils.httpGET(url, true, username, password, null, timeout, true);
} catch (Exception e) {
String message = "Exception occurred while trying to make the GET call to URL: " + url;
log.error(message, e);
throw new MetadataVersionServiceException(message, e);
}
return dhisHttpResponse;
}
use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException in project dhis2-core by dhis2.
the class MetadataVersionController method getMetaDataVersionHistory.
//Gets the list of all versions in between the passed version name and latest system version
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/history", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getMetaDataVersionHistory(@RequestParam(value = "baseline", required = false) String versionName) throws MetadataVersionException, BadRequestException {
List<MetadataVersion> allVersionsInBetween = new ArrayList<>();
boolean enabled = isMetadataVersioningEnabled();
try {
if (!enabled) {
throw new BadRequestException("Metadata versioning is not enabled for this instance.");
}
Date startDate;
if (versionName == null || versionName.isEmpty()) {
MetadataVersion initialVersion = versionService.getInitialVersion();
if (initialVersion == null) {
return versionService.getMetadataVersionsAsNode(allVersionsInBetween);
}
startDate = initialVersion.getCreated();
} else {
startDate = versionService.getCreatedDate(versionName);
}
if (startDate == null) {
throw new MetadataVersionException("There is no such metadata version. The latest version is Version " + versionService.getCurrentVersion().getName());
}
Date endDate = new Date();
allVersionsInBetween = versionService.getAllVersionsInBetween(startDate, endDate);
if (allVersionsInBetween != null) {
//now remove the baseline version details
for (Iterator<MetadataVersion> iterator = allVersionsInBetween.iterator(); iterator.hasNext(); ) {
MetadataVersion m = iterator.next();
if (m.getName().equals(versionName)) {
iterator.remove();
break;
}
}
if (!allVersionsInBetween.isEmpty()) {
return versionService.getMetadataVersionsAsNode(allVersionsInBetween);
}
}
} catch (MetadataVersionServiceException ex) {
throw new MetadataVersionException(ex.getMessage(), ex);
}
return null;
}
Aggregations