|
| 1 | +package org.openmrs.module.initializer.api.patientflags; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +import org.apache.commons.lang3.StringUtils; |
| 7 | +import org.openmrs.annotation.OpenmrsProfile; |
| 8 | +import org.openmrs.module.patientflags.Flag; |
| 9 | +import org.openmrs.module.patientflags.Priority; |
| 10 | +import org.openmrs.module.patientflags.Tag; |
| 11 | +import org.openmrs.module.patientflags.api.FlagService; |
| 12 | +import org.openmrs.module.initializer.api.BaseLineProcessor; |
| 13 | +import org.openmrs.module.initializer.api.CsvLine; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 16 | + |
| 17 | +/** |
| 18 | + * Processes CSV lines for Flag entities. |
| 19 | + */ |
| 20 | +@OpenmrsProfile(modules = { "patientflags:3.* - 9.*" }) |
| 21 | +public class FlagsLineProcessor extends BaseLineProcessor<Flag> { |
| 22 | + |
| 23 | + protected static final String HEADER_CRITERIA = "criteria"; |
| 24 | + |
| 25 | + protected static final String HEADER_EVALUATOR = "evaluator"; |
| 26 | + |
| 27 | + protected static final String HEADER_MESSAGE = "message"; |
| 28 | + |
| 29 | + protected static final String HEADER_PRIORITY = "priority"; |
| 30 | + |
| 31 | + protected static final String HEADER_ENABLED = "enabled"; |
| 32 | + |
| 33 | + protected static final String HEADER_TAGS = "tags"; |
| 34 | + |
| 35 | + private FlagService flagService; |
| 36 | + |
| 37 | + private TagListParser tagListParser; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + public FlagsLineProcessor(@Qualifier("flagService") FlagService flagService, TagListParser tagListParser) { |
| 41 | + this.flagService = flagService; |
| 42 | + this.tagListParser = tagListParser; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Flag fill(Flag flag, CsvLine line) throws IllegalArgumentException { |
| 47 | + flag.setName(line.get(HEADER_NAME, true)); |
| 48 | + flag.setDescription(line.get(HEADER_DESC)); |
| 49 | + |
| 50 | + // Required fields |
| 51 | + flag.setCriteria(line.get(HEADER_CRITERIA, true)); |
| 52 | + flag.setEvaluator(line.get(HEADER_EVALUATOR, true)); |
| 53 | + flag.setMessage(line.get(HEADER_MESSAGE, true)); |
| 54 | + |
| 55 | + // Optional priority |
| 56 | + String priorityId = line.getString(HEADER_PRIORITY, ""); |
| 57 | + if (StringUtils.isNotBlank(priorityId)) { |
| 58 | + Priority priority = fetchPriority(priorityId); |
| 59 | + if (priority == null) { |
| 60 | + throw new IllegalArgumentException( |
| 61 | + "The priority referenced by '" + priorityId + "' does not point to any known priority."); |
| 62 | + } |
| 63 | + flag.setPriority(priority); |
| 64 | + } else { |
| 65 | + flag.setPriority(null); |
| 66 | + } |
| 67 | + |
| 68 | + // Optional enabled flag (defaults to true if not specified) |
| 69 | + String enabledStr = line.getString(HEADER_ENABLED, ""); |
| 70 | + if (StringUtils.isNotBlank(enabledStr)) { |
| 71 | + flag.setEnabled(Boolean.parseBoolean(enabledStr.trim())); |
| 72 | + } else { |
| 73 | + flag.setEnabled(true); // Default to enabled |
| 74 | + } |
| 75 | + |
| 76 | + // Optional tags |
| 77 | + String tagsStr = line.getString(HEADER_TAGS, ""); |
| 78 | + if (StringUtils.isNotBlank(tagsStr)) { |
| 79 | + Set<Tag> tags = new HashSet<Tag>(tagListParser.parseList(tagsStr)); |
| 80 | + flag.setTags(tags); |
| 81 | + } else if (flag.getTags() != null) { |
| 82 | + // Clear tags if not specified (only if tags were previously set) |
| 83 | + flag.getTags().clear(); |
| 84 | + } |
| 85 | + |
| 86 | + return flag; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Fetches a Priority by UUID or name. |
| 91 | + */ |
| 92 | + private Priority fetchPriority(String id) { |
| 93 | + if (StringUtils.isBlank(id)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + // Try UUID first |
| 98 | + Priority priority = flagService.getPriorityByUuid(id); |
| 99 | + if (priority != null) { |
| 100 | + return priority; |
| 101 | + } |
| 102 | + |
| 103 | + // Try name |
| 104 | + priority = flagService.getPriorityByName(id); |
| 105 | + if (priority != null) { |
| 106 | + return priority; |
| 107 | + } |
| 108 | + |
| 109 | + return null; |
| 110 | + } |
| 111 | +} |
0 commit comments