Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCLM-78 - Enable importing single concept exports from OCL #84

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,19 @@ public Concept getConceptByUuid(String uuid) {
return concept;
}
}

public Concept getConceptByMapping(String source, String code) {
String cacheKey = source + ":" + code;
Concept concept = concepts.get(cacheKey);
if (concept != null) {
return concept;
}
else {
concept = conceptService.getConceptByMapping(code, source);
if (concept != null) {
concepts.put(cacheKey, concept);
}
return concept;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@
package org.openmrs.module.openconceptlab;


import org.apache.commons.lang.StringUtils;
import org.openmrs.GlobalProperty;
import org.openmrs.api.context.Context;
import org.openmrs.module.DaemonToken;
import org.openmrs.module.DaemonTokenAware;
import org.openmrs.module.ModuleActivator;
import org.openmrs.module.openconceptlab.importer.Importer;
import org.openmrs.module.openconceptlab.scheduler.UpdateScheduler;
import org.openmrs.util.OpenmrsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;

/**
* This class contains the logic that is run every time this module is either started or stopped.
Expand All @@ -50,44 +45,19 @@ public void contextRefreshed() {
Context.openSession();
}

String loadAtStartupPath = Context.getAdministrationService()
.getGlobalProperty(OpenConceptLabConstants.GP_OCL_LOAD_AT_STARTUP_PATH);
if (StringUtils.isBlank(loadAtStartupPath)) {
loadAtStartupPath =
new File(new File(new File(OpenmrsUtil.getApplicationDataDirectory(), "ocl"), "configuration"),
"loadAtStartup").getAbsolutePath();
Context.getAdministrationService()
.saveGlobalProperty(
new GlobalProperty(OpenConceptLabConstants.GP_OCL_LOAD_AT_STARTUP_PATH, loadAtStartupPath)
);
}

File loadAtStartupDir = new File(loadAtStartupPath);
if (!loadAtStartupDir.exists()) {
loadAtStartupDir.mkdirs();
}

UpdateScheduler scheduler = Context.getRegisteredComponent("openconceptlab.updateScheduler", UpdateScheduler.class);

File loadAtStartupDir = Utils.getLoadAtStartupDirectory();
File[] files = loadAtStartupDir.listFiles();
if (files != null && files.length > 1) {
throw new IllegalStateException(
"There is more than one file in ocl/loadAtStartup directory\n" +
"Ensure that there is only one file\n" +
"Absolute directory path: " + loadAtStartupDir.getAbsolutePath());
} else if (files != null && files.length != 0) {
if (files[0].getName().endsWith(".zip")) {
try {
ZipFile zipFile = (new ZipFile(files[0]));
Importer importer = Context.getRegisteredComponent("openconceptlab.importer", Importer.class);
importer.run(zipFile);
}
catch (IOException e) {
throw new IllegalStateException("Failed to open zip file", e);
}
} else {
throw new IllegalStateException("File " + files[0].getName() + " must be in *.zip format");
}
Importer importer = Context.getRegisteredComponent("openconceptlab.importer", Importer.class);
importer.setImportFile(files[0]);
importer.run();
}
scheduler.scheduleUpdate();
log.info("Open Concept Lab Module refreshed");
Expand Down
43 changes: 43 additions & 0 deletions api/src/main/java/org/openmrs/module/openconceptlab/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@

import static org.openmrs.module.openconceptlab.OpenConceptLabConstants.OPEN_CONCEPT_LAB_NAMESPACE_UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.openmrs.GlobalProperty;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.Context;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.api.APIException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -130,6 +136,43 @@ public static InputStream extractExportInputStreamFromZip(ZipFile zipfile) throw
return in;
}

public static File getOclDirectory() {
return OpenmrsUtil.getDirectoryInApplicationDataDirectory("ocl");
}

public static File getOclConfigurationDirectory() {
File configDir = new File(getOclDirectory(), "configuration");
if (!configDir.exists()) {
configDir.mkdirs();
}
return configDir;
}

public static File getLoadAtStartupDirectory() {
AdministrationService as = Context.getAdministrationService();
String gpName = OpenConceptLabConstants.GP_OCL_LOAD_AT_STARTUP_PATH;
File loadAtStartupDir = new File(getOclConfigurationDirectory(), "loadAtStartup");
String loadAtStartupPath = as.getGlobalProperty(gpName);
if (StringUtils.isBlank(loadAtStartupPath)) {
as.saveGlobalProperty(new GlobalProperty(gpName, loadAtStartupDir.getAbsolutePath()));
}
else {
loadAtStartupDir = new File(loadAtStartupPath);
}
if (!loadAtStartupDir.exists()) {
loadAtStartupDir.mkdirs();
}
return loadAtStartupDir;
}

public static File getImportDirectory() {
File importDir = new File(getOclDirectory(), "imports");
if (!importDir.exists()) {
importDir.mkdirs();
}
return importDir;
}

/**
* Implements a version 5 UUID from RFC 4122 using the {@link OpenConceptLabConstants#OPEN_CONCEPT_LAB_NAMESPACE_UUID}
* as the namespace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class OclConcept {

private Extras extras;

private List<OclMapping> mappings;

public String getType() {
return type;
}
Expand Down Expand Up @@ -191,6 +193,14 @@ public void setExtras(Extras extras) {
this.extras = extras;
}

public List<OclMapping> getMappings() {
return mappings;
}

public void setMappings(List<OclMapping> mappings) {
this.mappings = mappings;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Name {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class OclMapping {

@JsonProperty("map_type")
private String mapType;

@JsonProperty("from_source_name")
private String fromSourceName;

@JsonProperty("from_concept_code")
private String fromConceptCode;

@JsonProperty("from_source_url")
private String fromSourceUrl;
Expand Down Expand Up @@ -107,7 +113,15 @@ public String getMapType() {
public void setMapType(String mapType) {
this.mapType = mapType;
}


public String getFromSourceName() {
return fromSourceName;
}

public void setFromSourceName(String fromSourceName) {
this.fromSourceName = fromSourceName;
}

public String getFromSourceUrl() {
return fromSourceUrl;
}
Expand All @@ -123,7 +137,15 @@ public String getFromConceptUrl() {
public void setFromConceptUrl(String fromConceptUrl) {
this.fromConceptUrl = fromConceptUrl;
}


public String getFromConceptCode() {
return fromConceptCode;
}

public void setFromConceptCode(String fromConceptCode) {
this.fromConceptCode = fromConceptCode;
}

public String getToSourceName() {
return toSourceName != null ? toSourceName : toSourceNameResolved;
}
Expand Down
Loading