| | 58 | /** Constructor with id */ |
|---|
| | 59 | public ProgramWorkflow(Integer programWorkflowId) { |
|---|
| | 60 | setProgramWorkflowId(programWorkflowId); |
|---|
| | 61 | } |
|---|
| | 62 | |
|---|
| | 63 | // ****************** |
|---|
| | 64 | // Instance methods |
|---|
| | 65 | // ****************** |
|---|
| | 66 | |
|---|
| | 67 | /** |
|---|
| | 68 | * Adds a new {@link ProgramWorkflowState} to this ProgramWorkflow |
|---|
| | 69 | * @param state - the {@link ProgramWorkflowState} to add |
|---|
| | 70 | */ |
|---|
| | 71 | public void addState(ProgramWorkflowState state) { |
|---|
| | 72 | state.setProgramWorkflow(this); |
|---|
| | 73 | getStates().add(state); |
|---|
| | 74 | } |
|---|
| | 75 | |
|---|
| | 76 | /** |
|---|
| | 77 | * Removes a {@link ProgramWorkflowState} from this ProgramWorkflow |
|---|
| | 78 | * @param state - the {@link ProgramWorkflowState} to remove |
|---|
| | 79 | */ |
|---|
| | 80 | public void removeState(ProgramWorkflowState state) { |
|---|
| | 81 | if (getStates().contains(state)) { |
|---|
| | 82 | getStates().remove(state); |
|---|
| | 83 | state.setProgramWorkflow(null); |
|---|
| | 84 | } |
|---|
| | 85 | } |
|---|
| | 86 | |
|---|
| | 87 | /** |
|---|
| | 88 | * Retires a {@link ProgramWorkflowState} |
|---|
| | 89 | * @param state - the {@link ProgramWorkflowState} to retire |
|---|
| | 90 | */ |
|---|
| | 91 | public void retireState(ProgramWorkflowState state) { |
|---|
| | 92 | state.setRetired(true); |
|---|
| | 93 | } |
|---|
| | 94 | |
|---|
| | 95 | /** |
|---|
| | 96 | * Returns a {@link ProgramWorkflowState} whose primary key id matches the input parameter |
|---|
| | 97 | * @param programWorkflowStateId the primary key {@link Integer} id to match |
|---|
| | 98 | * @return a {@link ProgramWorkflowState} whose identifier matches the passed <code>programWorkflowStateId</code> |
|---|
| | 99 | */ |
|---|
| | 100 | public ProgramWorkflowState getState(Integer programWorkflowStateId) { |
|---|
| | 101 | for (ProgramWorkflowState s : getStates()) { |
|---|
| | 102 | if (s.getProgramWorkflowStateId().equals(programWorkflowStateId)) { |
|---|
| | 103 | return s; |
|---|
| | 104 | } |
|---|
| | 105 | } |
|---|
| | 106 | return null; |
|---|
| | 107 | } |
|---|
| | 108 | |
|---|
| | 109 | /** |
|---|
| | 110 | * Returns a {@link ProgramWorkflowState} whose Concept matches the passed concept |
|---|
| | 111 | * @param name the Concept to match |
|---|
| | 112 | * @return a {@link ProgramWorkflowState} whose {@link Concept} matches the passed <code>concept</code> |
|---|
| | 113 | */ |
|---|
| | 114 | public ProgramWorkflowState getState(Concept concept) { |
|---|
| | 115 | for (ProgramWorkflowState s : getStates()) { |
|---|
| | 116 | if (s.getConcept().equals(concept)) { |
|---|
| | 117 | return s; |
|---|
| | 118 | } |
|---|
| | 119 | } |
|---|
| | 120 | return null; |
|---|
| | 121 | } |
|---|
| | 122 | |
|---|
| | 123 | /** |
|---|
| | 124 | * Returns a {@link ProgramWorkflowState} whose Concept name matches the passed name in any {@link Locale} |
|---|
| | 125 | * @param name the Concept name to match in any {@link Locale} |
|---|
| | 126 | * @return a {@link ProgramWorkflowState} whose {@link Concept} name matches the passed <code>name</code> |
|---|
| | 127 | */ |
|---|
| | 128 | public ProgramWorkflowState getState(String name) { |
|---|
| | 129 | for (ProgramWorkflowState s : getStates()) { |
|---|
| | 130 | if (s.getConcept().isNamed(name)) { |
|---|
| | 131 | return s; |
|---|
| | 132 | } |
|---|
| | 133 | } |
|---|
| | 134 | return null; |
|---|
| | 135 | } |
|---|
| | 136 | |
|---|
| | 137 | /** |
|---|
| | 138 | * Returns a {@link ProgramWorkflowState} whose {@link Concept} has any {@link ConceptName} that matches the given <code>name</name> |
|---|
| | 139 | * @param name the {@link ProgramWorkflowState} name, in any {@link Locale} |
|---|
| | 140 | * @return a {@link ProgramWorkflowState} which has the passed <code>name</code> in any {@link Locale} |
|---|
| | 141 | */ |
|---|
| | 142 | public ProgramWorkflowState getStateByName(String name) { |
|---|
| | 143 | for (ProgramWorkflowState s : getStates()) { |
|---|
| | 144 | if (s.getConcept().isNamed(name)) { |
|---|
| | 145 | return s; |
|---|
| | 146 | } |
|---|
| | 147 | } |
|---|
| | 148 | return null; |
|---|
| | 149 | } |
|---|
| | 150 | |
|---|
| | 151 | /** |
|---|
| | 152 | * Returns a {@link Set<ProgramWorkflowState>} including all non-retired ProgramWorkflowStates |
|---|
| | 153 | * and all retired ProgramWorkflowStates in this ProgramWorkflow if <code>includeRetired</code> is true |
|---|
| | 154 | * @param includeRetired - if false, returns only non-retired {@link ProgramWorkflowState} objects in this ProgramWorkflow |
|---|
| | 155 | * @return Set<ProgramWorkflowState> - all ProgramWorkflowStates matching input parameters |
|---|
| | 156 | */ |
|---|
| | 157 | public Set<ProgramWorkflowState> getStates(boolean includeRetired) { |
|---|
| | 158 | Set<ProgramWorkflowState> ret = new HashSet<ProgramWorkflowState>(); |
|---|
| | 159 | for (ProgramWorkflowState s : getStates()) { |
|---|
| | 160 | if (includeRetired || !s.isRetired()) { |
|---|
| | 161 | ret.add(s); |
|---|
| | 162 | } |
|---|
| | 163 | } |
|---|
| | 164 | return ret; |
|---|
| | 165 | } |
|---|
| | 166 | |
|---|
| | 167 | /** |
|---|
| | 168 | * Returns a {@link Set<ProgramWorkflowState>} including all ProgramWorkflowStates, sorted by {@link ConceptName} |
|---|
| | 169 | * @return Set<ProgramWorkflowState> - all ProgramWorkflowStates, sorted by {@link ConceptName} |
|---|
| | 170 | */ |
|---|
| | 171 | @SuppressWarnings("unchecked") |
|---|
| | 172 | public Set<ProgramWorkflowState> getSortedStates() { |
|---|
| | 173 | Comparator c = new PropertyComparator("concept.name.name", true, true); |
|---|
| | 174 | TreeSet<ProgramWorkflowState> sorted = new TreeSet<ProgramWorkflowState>(c); |
|---|
| | 175 | if (getStates() != null ) { |
|---|
| | 176 | sorted.addAll(getStates()); |
|---|
| | 177 | } |
|---|
| | 178 | return sorted; |
|---|
| | 179 | } |
|---|
| | 180 | |
|---|
| | 181 | /** |
|---|
| | 182 | * Returns a {@link List<ProgramWorkflowState>} including all possible next ProgramWorkflowStates, |
|---|
| | 183 | * for the passed {@link PatientProgram} ordered by {@link ConceptName} |
|---|
| | 184 | * @param - patientProgram - The PatientProgram to check |
|---|
| | 185 | * @return List<ProgramWorkflowState> - all possible next ProgramWorkflowStates, for the passed {@link PatientProgram} ordered by {@link ConceptName} |
|---|
| | 186 | */ |
|---|
| | 187 | public List<ProgramWorkflowState> getPossibleNextStates(PatientProgram patientProgram) { |
|---|
| | 188 | List<ProgramWorkflowState> ret = new ArrayList<ProgramWorkflowState>(); |
|---|
| | 189 | PatientState currentState = patientProgram.getCurrentState(this); |
|---|
| | 190 | for (ProgramWorkflowState st : getSortedStates()) { |
|---|
| | 191 | if (isLegalTransition(currentState == null ? null : currentState.getState(), st)) { |
|---|
| | 192 | ret.add(st); |
|---|
| | 193 | } |
|---|
| | 194 | } |
|---|
| | 195 | return ret; |
|---|
| | 196 | } |
|---|
| | 197 | |
|---|
| | 198 | /** |
|---|
| | 199 | * Returns a {@link List<ProgramWorkflowState>} including all possible next ProgramWorkflowStates, |
|---|
| | 200 | * for the passed {@link PatientProgram} ordered by {@link ConceptName} |
|---|
| | 201 | * @param fromState - {@link ProgramWorkflowState} to check transition from |
|---|
| | 202 | * @param toState - {@link ProgramWorkflowState} to check transition to |
|---|
| | 203 | * @return boolean - true if it is allowable to transition from <code>fromState</code> to <code>toState</code> |
|---|
| | 204 | */ |
|---|
| | 205 | public boolean isLegalTransition(ProgramWorkflowState fromState, ProgramWorkflowState toState) { |
|---|
| | 206 | if (fromState == null) { |
|---|
| | 207 | return toState.getInitial(); |
|---|
| | 208 | } |
|---|
| | 209 | if (fromState.equals(toState)) { |
|---|
| | 210 | return false; |
|---|
| | 211 | } |
|---|
| | 212 | return true; |
|---|
| | 213 | } |
|---|
| | 214 | |
|---|
| | 215 | /** @see Object#equals(Object) */ |
|---|