use of org.hisp.dhis.dxf2.metadata.Metadata 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.Metadata 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.Metadata 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.Metadata 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.Metadata in project dhis2-core by dhis2.
the class DefaultSynchronizationManager method executeMetadataPull.
@Override
public ImportReport executeMetadataPull(String url) {
User user = currentUserService.getCurrentUser();
String userUid = user != null ? user.getUid() : null;
log.info(String.format("Metadata pull, url: %s, user: %s", url, userUid));
String json = restTemplate.getForObject(url, String.class);
Metadata metadata = null;
try {
metadata = DefaultRenderService.getJsonMapper().readValue(json, Metadata.class);
} catch (IOException ex) {
throw new RuntimeException("Failed to parse remote JSON document", ex);
}
MetadataImportParams importParams = new MetadataImportParams();
importParams.setSkipSharing(true);
importParams.setAtomicMode(AtomicMode.NONE);
importParams.addMetadata(schemaService.getMetadataSchemas(), metadata);
return importService.importMetadata(importParams);
}
Aggregations