Skip to content

Commit

Permalink
Add delete/purge for program states in the workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
khayamuddin31 committed Apr 8, 2013
1 parent fe98f3a commit c7aa668
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 5 deletions.
3 changes: 3 additions & 0 deletions api/src/main/java/org/openmrs/api/ProgramWorkflowService.java
Expand Up @@ -624,6 +624,9 @@ public void purgeConceptStateConversion(ConceptStateConversion conceptStateConve
public List<ProgramWorkflowState> getPossibleNextStates(PatientProgram patientProgram, ProgramWorkflow workflow)
throws APIException;

@Authorized( { PrivilegeConstants.DELETE_PROGRAMWORKFLOWSTATE })
public void deleteProgramWorkflowState(ProgramWorkflowState state) throws APIException;

/**
* Returns boolean indicating whether it is legal to transition from one ProgramWorkflowState to
* another
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/org/openmrs/api/db/ProgramWorkflowDAO.java
Expand Up @@ -273,4 +273,6 @@ public List<PatientProgram> getPatientPrograms(Patient patient, Program program,
* @return - A List of ProgramWorkflowStates
*/
public List<ProgramWorkflowState> getProgramWorkflowStatesByConcept(Concept concept);

public void deleteProgramWorkflowState(ProgramWorkflowState state);
}
Expand Up @@ -347,4 +347,10 @@ public List<ProgramWorkflowState> getProgramWorkflowStatesByConcept(Concept conc
squery.setEntity("concept", concept);
return squery.list();
}

@Override
public void deleteProgramWorkflowState(ProgramWorkflowState state) throws DAOException {
// TODO Auto-generated method stub

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Can we remove this TODO?

sessionFactory.getCurrentSession().delete(state);
}
}
Expand Up @@ -33,6 +33,7 @@
import org.openmrs.ProgramWorkflow;
import org.openmrs.ProgramWorkflowState;
import org.openmrs.User;
import org.openmrs.annotation.Authorized;
import org.openmrs.api.APIException;
import org.openmrs.api.ProgramNameDuplicatedException;
import org.openmrs.api.ProgramWorkflowService;
Expand Down Expand Up @@ -900,4 +901,11 @@ public ProgramWorkflow getWorkflowByUuid(String uuid) {
return dao.getWorkflowByUuid(uuid);
}

@Override
@Authorized("Delete ProgramWorkflowState")
public void deleteProgramWorkflowState(ProgramWorkflowState state) throws APIException {
// TODO Auto-generated method stub

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Can we remove this TODO?

dao.deleteProgramWorkflowState(state);
}

}
3 changes: 3 additions & 0 deletions api/src/main/java/org/openmrs/util/PrivilegeConstants.java
Expand Up @@ -101,6 +101,9 @@ public class PrivilegeConstants {
@AddOnStartup(description = "Able to get which programs that patients are in")
public static final String GET_PATIENT_PROGRAMS = "Get Patient Programs";

@AddOnStartup(description = "Able to delete state from the system")

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Can we change the description by specifying which state we are referring to?

public static final String DELETE_PROGRAMWORKFLOWSTATE = "Delete ProgramWorkflowState";

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Can we change to something more readable like DELETE_PROGRAM_WORKFLOW_STATE = "Delete Program Workflow State"


@AddOnStartup(description = "Able to get global properties on the administration screen")
public static final String GET_GLOBAL_PROPERTIES = "Get Global Properties";

Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.logging.LogFactory;
import org.openmrs.ProgramWorkflow;
import org.openmrs.ProgramWorkflowState;
import org.openmrs.api.APIException;
import org.openmrs.api.ProgramWorkflowService;
import org.openmrs.api.context.Context;
import org.openmrs.web.WebConstants;
Expand All @@ -35,6 +36,7 @@
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.dao.DataIntegrityViolationException;

public class WorkflowFormController extends SimpleFormController {

Expand Down Expand Up @@ -85,12 +87,46 @@ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse
log.debug("about to save " + obj);

HttpSession httpSession = request.getSession();
ProgramWorkflowService pwsr = Context.getProgramWorkflowService();

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

What does the r in pwsr mean? Can we use a more meaningful variable name? :)


String view = getFormView();

if (Context.isAuthenticated()) {
ProgramWorkflow wf = (ProgramWorkflow) obj;

if (request.getParameter("delete") != null) {
String detstatesStr = request.getParameter("deletedStates");

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Did you mean "deletedStatesStr" instead of "detstatesStr"?

for (StringTokenizer st = new StringTokenizer(detstatesStr, "|"); st.hasMoreTokens();) {
String str = st.nextToken();
String[] tmp = str.split(",");
Integer conceptId = Integer.valueOf(tmp[0]);
ProgramWorkflowState pws = null;
for (ProgramWorkflowState s : wf.getStates()) {
if (s.getConcept().getConceptId().equals(conceptId)) {
pws = s;
break;
}
}

try {
wf.removeState(pws);
pwsr.deleteProgramWorkflowState(pws);
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "ProgramWorkflowState.delete.success");
}
catch (DataIntegrityViolationException e) {
httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.delete");

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Did you just forget to put 'error.object.inuse.cannot.delete" in messages.properties? Or did you mean to use "error.object.inuse.cannot.purge"?

}
catch (APIException e) {
httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: "
+ e.getLocalizedMessage());
}
view = getSuccessView();
return new ModelAndView(new RedirectView(view));

}

}

// get list of states, and update the command object
String statesStr = request.getParameter("newStates");
// This is a brute-force algorithm, but n will be small.
Expand Down
4 changes: 1 addition & 3 deletions webapp/src/main/webapp/WEB-INF/messages.properties
Expand Up @@ -68,7 +68,6 @@ error.extraPrivilegesRequired=Extra privileges required
error.insufficientPrivileges=Insufficient Privileges
error.aunthenticationRequired=Basic authentication required
error.failedToSendRequest=Failed to send request
error.description.required=Description is required

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Did you mean to remove this?


general.at=at
general.with=with
Expand Down Expand Up @@ -428,7 +427,6 @@ Alert.assignedTo.recipient=1 recipient
Alert.recipients=Recipients
Alert.roles=Roles
Alert.text=Alert Text
Alert.text.required=Alert Text is required

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Did you intend to remove this?

Alert.dateToExpire=Date To Expire
Alert.satisfiedByAny=Satisfied By Any
Alert.satisfiedByAny.description=Alert will be marked read for all recipients once one recipient does marks it.
Expand Down Expand Up @@ -2168,6 +2166,7 @@ State.list.title=Current State(s)
State.error.name.duplicate=State already exists
State.retire.confirmation=Are you sure you want to retire this state?
State.unretire.confirmation=Are you sure you want to unretire this state?
State.delete.confirmation=Are you sure you want to delete this state?
State.error.noDate=You cannot change from a non-null state without giving a change date
State.error.noState=You must choose a state
State.error.invalidChangeState=You cannot change out of a state that has an end date already
Expand Down Expand Up @@ -2475,7 +2474,6 @@ Visit.clickToViewEncounters=Click to view encounters
Visit.clickToHideEncounters=Click to hide encounters
Visit.noVisitAssigned=No visit assigned
Visit.cannotPurgeVisitWithEncounters=Can't purge a visit that has encounters associated to it
Visit.encounter.search.placeholder=Enter encounter id, provider identifier, location, form, encounter type or provider name

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Apr 30, 2013

Did you intend to remove this?


Visit.configure.startCloseVisitsTask=Start auto close visits task
Visit.configure.closeVisitsTask.failedToStart=Failed to start task
Expand Down
Expand Up @@ -12,11 +12,16 @@
width: 50px;
border: 1px solid #009d8e;
}
.statesToDelete{
border:1px solid #009d8e;
width:50px;
}
</style>
<script type="text/javascript">
var displayedStates = new Array();
var idToNameMap = new Array();
var retiredStates = new Array();
var deletedStates=new Array();
var allSates= new Array();
var activeStates=new Array();
var isActiveDisplay=true;
Expand All @@ -39,7 +44,8 @@
function (st) { return getIdToNameMap(st[0]); },
function (st) { return '<input type="checkbox" id="initial_' + st[0] + '" ' + (st[1] ? 'checked' : '') + '/>'; },
function (st) { return '<input type="checkbox" id="terminal_' + st[0] + '" ' + (st[2] ? 'checked' : '') + '/>'; },
function (st) { return getButton(st[0]); }
function (st) { return getButton(st[0]); },
function (st) { return getDeleteButton(st[0]); }
], { escapeHtml:false });
} else {
dwr.util.addRows('stateTable', ['<openmrs:message code="general.none"/>'], [
Expand Down Expand Up @@ -96,7 +102,7 @@
}
function getButton(conceptId){
if(isStateRetired(conceptId)){
return '<input type="button" class="statesToRetire" value="<openmrs:message code="general.unretire"/>" onclick="unretireState('+conceptId+')"}/>';
}else{
Expand All @@ -106,6 +112,34 @@ function getButton(conceptId){
}
function getDeleteButton(conceptId){
return '<form id="delete'+conceptId+'" method="post" onsubmit="return deleteState('+conceptId+')">' + '<input type="submit" name="Delete" class="statesToDelete" value="delete" /> <input type="hidden" id="statesToDelete" name="deletedStates" /> </form>';
}
function deleteState(conceptId) {
for (var i = 0; i < activeStates.length; ++i) {
if (activeStates[i][0] == conceptId) {
var x=window.confirm("<openmrs:message code='State.delete.confirmation'/>")
if (x) {
var tmp = "";
var conceptId = activeStates[i][0];
var isInitial = jQuery('#initial_' + conceptId).is(':checked');
var isTerminal = jQuery('#terminal_' + conceptId).is(':checked');
tmp += conceptId + ",";
tmp += isInitial + ",";
tmp += isTerminal + "|";
var t=document.forms['delete'+conceptId].elements['deletedStates'];
t.value=tmp;
jQuery('#statesToDelete').val(tmp);
return true;
}
else
return false;
}
}
return false;
}
function isStateRetired(conceptId){
for(var i = 0; i < retiredStates.length;++i){
if(conceptId==retiredStates[i][0]){
Expand Down Expand Up @@ -192,6 +226,7 @@ refreshStateTable();
code="State.list.title" /> </b>

<form method="post" id="theForm">
<div id="debug"> </div>
<table>
<thead>
<tr>
Expand All @@ -213,9 +248,11 @@ refreshStateTable();
</tbody>
</table>
<input type="hidden" id="statesToSubmit" name="newStates" />
<input type="hidden" id="statesToDelete" name="deletedStates" />
<input type="button" onClick="handleSave()" value="<openmrs:message code="general.save" />" />
</form>


<script type="text/javascript">
<c:forEach var="state" items="${workflow.states}">
idToNameMap[${state.concept.conceptId}] = '<openmrs:concept conceptId="${state.concept.conceptId}" nameVar="n" var="v" numericVar="nv">${n.name}</openmrs:concept>';
Expand Down

0 comments on commit c7aa668

Please sign in to comment.