|
| 1 | +/** |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 3 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 4 | + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under |
| 5 | + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. |
| 6 | + * |
| 7 | + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS |
| 8 | + * graphic logo is a trademark of OpenMRS Inc. |
| 9 | + */ |
| 10 | +package org.openmrs.module.initializer.api.systemtasks; |
| 11 | + |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.openmrs.annotation.OpenmrsProfile; |
| 14 | +import org.openmrs.api.context.Context; |
| 15 | +import org.openmrs.module.initializer.api.BaseLineProcessor; |
| 16 | +import org.openmrs.module.initializer.api.CsvLine; |
| 17 | +import org.openmrs.module.tasks.Priority; |
| 18 | +import org.openmrs.module.tasks.SystemTask; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.springframework.stereotype.Component; |
| 22 | + |
| 23 | +/** |
| 24 | + * Line processor for SystemTask CSV files. Handles field mapping from CSV columns to SystemTask |
| 25 | + * entity properties. |
| 26 | + */ |
| 27 | +@Component |
| 28 | +@OpenmrsProfile(modules = { "tasks:*" }) |
| 29 | +public class SystemTasksLineProcessor extends BaseLineProcessor<SystemTask> { |
| 30 | + |
| 31 | + private static final Logger log = LoggerFactory.getLogger(SystemTasksLineProcessor.class); |
| 32 | + |
| 33 | + public static final String HEADER_TITLE = "title"; |
| 34 | + |
| 35 | + public static final String HEADER_PRIORITY = "priority"; |
| 36 | + |
| 37 | + public static final String HEADER_DEFAULT_ASSIGNEE_ROLE = "default assignee role"; |
| 38 | + |
| 39 | + public static final String HEADER_RATIONALE = "rationale"; |
| 40 | + |
| 41 | + public SystemTasksLineProcessor() { |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public SystemTask fill(SystemTask systemTask, CsvLine line) throws IllegalArgumentException { |
| 46 | + systemTask.setName(line.getName(true)); // Required - uses HEADER_NAME from base class |
| 47 | + systemTask.setTitle(line.get(HEADER_TITLE, true)); // Required |
| 48 | + systemTask.setDescription(line.get(HEADER_DESC)); // Uses HEADER_DESC from base class |
| 49 | + systemTask.setRationale(line.get(HEADER_RATIONALE)); |
| 50 | + |
| 51 | + String priorityStr = line.get(HEADER_PRIORITY); |
| 52 | + if (StringUtils.isNotBlank(priorityStr)) { |
| 53 | + try { |
| 54 | + systemTask.setPriority(Priority.valueOf(priorityStr.toUpperCase())); |
| 55 | + } |
| 56 | + catch (IllegalArgumentException e) { |
| 57 | + throw new IllegalArgumentException( |
| 58 | + "Invalid priority value: " + priorityStr + ". Must be one of: HIGH, MEDIUM, LOW"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + String assigneeRole = line.get(HEADER_DEFAULT_ASSIGNEE_ROLE); |
| 63 | + if (StringUtils.isNotBlank(assigneeRole)) { |
| 64 | + Integer roleId = resolveProviderRoleId(assigneeRole); |
| 65 | + systemTask.setDefaultAssigneeProviderRoleId(roleId); |
| 66 | + } |
| 67 | + |
| 68 | + return systemTask; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Resolves a provider role ID from either a UUID or a name. Uses reflection to avoid hard |
| 73 | + * dependency on providermanagement module. |
| 74 | + * |
| 75 | + * @param roleReference the UUID or name of the provider role |
| 76 | + * @return the provider role ID, or null if providermanagement is not available |
| 77 | + * @throws IllegalArgumentException if the provider role cannot be found |
| 78 | + */ |
| 79 | + private Integer resolveProviderRoleId(String roleReference) { |
| 80 | + try { |
| 81 | + // Try to get ProviderManagementService via reflection |
| 82 | + Class<?> serviceClass = Context.loadClass("org.openmrs.module.providermanagement.api.ProviderManagementService"); |
| 83 | + Object service = Context.getService(serviceClass); |
| 84 | + |
| 85 | + if (service == null) { |
| 86 | + log.warn("ProviderManagementService not available. Cannot resolve provider role: {}", roleReference); |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + // Check if it looks like a UUID (contains hyphens and is 36 characters) |
| 91 | + if (roleReference.contains("-") && roleReference.length() == 36) { |
| 92 | + java.lang.reflect.Method getByUuid = serviceClass.getMethod("getProviderRoleByUuid", String.class); |
| 93 | + Object role = getByUuid.invoke(service, roleReference); |
| 94 | + if (role != null) { |
| 95 | + java.lang.reflect.Method getId = role.getClass().getMethod("getProviderRoleId"); |
| 96 | + return (Integer) getId.invoke(role); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Try to find by name |
| 101 | + java.lang.reflect.Method getAllRoles = serviceClass.getMethod("getAllProviderRoles", boolean.class); |
| 102 | + @SuppressWarnings("unchecked") |
| 103 | + java.util.List<?> allRoles = (java.util.List<?>) getAllRoles.invoke(service, false); |
| 104 | + |
| 105 | + for (Object role : allRoles) { |
| 106 | + java.lang.reflect.Method getName = role.getClass().getMethod("getName"); |
| 107 | + String roleName = (String) getName.invoke(role); |
| 108 | + if (roleName != null && roleName.equalsIgnoreCase(roleReference)) { |
| 109 | + java.lang.reflect.Method getId = role.getClass().getMethod("getProviderRoleId"); |
| 110 | + return (Integer) getId.invoke(role); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + throw new IllegalArgumentException("Provider role not found: " + roleReference); |
| 115 | + } |
| 116 | + catch (ClassNotFoundException e) { |
| 117 | + log.warn("providermanagement module not available. Cannot resolve provider role: {}", roleReference); |
| 118 | + return null; |
| 119 | + } |
| 120 | + catch (IllegalArgumentException e) { |
| 121 | + throw e; |
| 122 | + } |
| 123 | + catch (Exception e) { |
| 124 | + log.error("Error resolving provider role: {}", roleReference, e); |
| 125 | + return null; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments