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

AF-59: Extensions should support 'requiredProgram' property #47

Open
wants to merge 2 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
19 changes: 19 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand All @@ -43,6 +49,12 @@
<artifactId>openmrs-api</artifactId>
<type>test-jar</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down Expand Up @@ -80,6 +92,13 @@
<artifactId>handlebars</artifactId>
</dependency>

<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.openmrs.module.appframework;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.Program;
import org.openmrs.ProgramWorkflow;
import org.openmrs.ProgramWorkflowState;
import org.openmrs.api.context.Context;

public class AppFrameworkUtil {

private final static Log log = LogFactory.getLog(AppFrameworkUtil.class);

/**
* Get the concept by id where the id can either be:
* 1) an integer id like 5090
* 2) a mapping type id like "XYZ:HT"
* 3) a uuid like "a3e12268-74bf-11df-9768-17cfc9833272"
* 4) a name
*
* @param id the concept identifier
* @return the concept if exist, else null
* @should find a concept by its conceptId
* @should find a concept by its mapping
* @should find a concept by its uuid
* @should find a concept by its name
* @should return null otherwise
*/
// NOTE: This method is a copy and paste from the htmlformentry module I guess it
// should be deprecated on fixing: https://issues.openmrs.org/browse/TRUNK-5655
public static Concept getConcept(String id) {
if (StringUtils.isNotBlank(id)) {
id = id.trim();
// see if this is parseable to an Integer; if so, try looking up concept by id
try {
// handle integer: id
int conceptId = Integer.parseInt(id);
return Context.getConceptService().getConcept(conceptId);
} catch (Exception ex) {
// pass
}
// handle mapping id: xyz:ht
if (id.indexOf(":") != -1) {
String [] sourceCodeSplit = id.split(":", 2);
String source = sourceCodeSplit[0].trim();
String term = sourceCodeSplit[1].trim();
return Context.getConceptService().getConceptByMapping(term, source, false);
}
// handle name
Concept ret = Context.getConceptService().getConceptByName(id);
if (ret != null) {
return ret;
}
// handle uuid
return Context.getConceptService().getConceptByUuid(id);
}
return null;
}

/**
* Gets <code>workflows</code> associated with a given <code>concept</code>
*/
public static List<ProgramWorkflow> getWorkflowsByConcept(Concept concept) {
List<ProgramWorkflow> ret = new ArrayList<ProgramWorkflow>();
for (ProgramWorkflow candidate : getAllWorkflows()) {
if (candidate.getConcept().equals(concept)) {
ret.add(candidate);
}
}
return ret;
}

/**
* Gets <code>states</code> associated with a given <code>concept</code>
*/
public static List<ProgramWorkflowState> getStatesByConcept(Concept concept) {
List<ProgramWorkflowState> ret = new ArrayList<ProgramWorkflowState>();
for (ProgramWorkflow workflow : getAllWorkflows()) {
for (ProgramWorkflowState candidate : workflow.getStates()) {
if (candidate.getConcept().equals(concept)) {
ret.add(candidate);
}
}
}
return ret;
}

/**
* Gets <code>programs</code> associated with a given <code>concept</code>
*/
public static List<Program> getProgramsByConcept(Concept concept) {
List<Program> ret = new ArrayList<Program>();
List<Program> allPrograms = Context.getProgramWorkflowService().getAllPrograms(false);
if (CollectionUtils.isNotEmpty(allPrograms)) {
for (Program candidate : allPrograms) {
if (candidate.getConcept().equals(concept)) {
ret.add(candidate);
}
}
}
return ret;
}

public static List<ProgramWorkflow> getAllWorkflows() {
List<ProgramWorkflow> ret = new ArrayList<ProgramWorkflow>();
List<Program> allPrograms = Context.getProgramWorkflowService().getAllPrograms(false);
for (Program program : allPrograms) {
Set<ProgramWorkflow> workflows = program.getAllWorkflows();
if (CollectionUtils.isNotEmpty(workflows)) {
ret.addAll(workflows);
}

}
return ret;
}

}
Loading