• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 13015466563

28 Jan 2025 04:53PM UTC coverage: 63.705% (-0.1%) from 63.806%
13015466563

push

github

web-flow
TRUNK-6292: Changes to Liquibase files breaks OpenMRS startup (#4899)

21982 of 34506 relevant lines covered (63.7%)

0.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.1
/api/src/main/java/org/openmrs/layout/name/NameSupport.java
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.layout.name;
11

12
import org.apache.commons.lang.StringEscapeUtils;
13
import org.apache.commons.lang3.StringUtils;
14

15
import java.util.ArrayList;
16
import java.util.List;
17
import java.util.stream.Collectors;
18

19
import org.openmrs.GlobalProperty;
20
import org.openmrs.api.APIException;
21
import org.openmrs.api.GlobalPropertyListener;
22
import org.openmrs.api.context.Context;
23
import org.openmrs.layout.LayoutSupport;
24
import org.openmrs.util.OpenmrsConstants;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

28
/**
29
 * @since 1.12
30
 */
31
public class NameSupport extends LayoutSupport<NameTemplate> implements GlobalPropertyListener {
32

33
        private static final Logger log = LoggerFactory.getLogger(NameSupport.class);
1✔
34
        private static NameSupport singleton;
35
        private boolean initialized = false;
1✔
36
        
37
        public NameSupport() {
1✔
38
                if (singleton == null) {
1✔
39
                        singleton = this;
1✔
40
                }
41
        }
1✔
42
        
43
        public static NameSupport getInstance() {
44
                if (singleton == null) {
1✔
45
                        throw new APIException("Not Yet Instantiated");
×
46
                } else {
47
                        singleton.init();
1✔
48
                        return singleton;
1✔
49
                }
50
        }
51

52
        /**
53
         * Initializes layout templates with a custom template configured
54
         * via the "layout.name.template" GP.
55
         */
56
        private void init() {
57
                if (initialized) {
1✔
58
                        return;
1✔
59
                }
60
                Context.getAdministrationService().addGlobalPropertyListener(singleton);
1✔
61
                // Get configured name template to override the existing one if any
62
                String layoutTemplateXml = Context.getAdministrationService().getGlobalProperty(
1✔
63
                        OpenmrsConstants.GLOBAL_PROPERTY_LAYOUT_NAME_TEMPLATE);
64
                NameTemplate nameTemplate = deserializeXmlTemplate(layoutTemplateXml);
1✔
65
                
66
                if (nameTemplate != null) {
1✔
67
                        updateLayoutTemplates(nameTemplate);
×
68
                }
69
                
70
                initialized = true;
1✔
71
        }
1✔
72
        
73
        /**
74
         * Update existing layout templates if present with the provided template
75
         */
76
        private void updateLayoutTemplates(NameTemplate nameTemplate) {
77
                if (getLayoutTemplates() == null) {
1✔
78
                        setLayoutTemplates(new ArrayList<>());
1✔
79
                }
80
                List<NameTemplate> list = new ArrayList<>();
1✔
81
                // filter out unaffected templates to keep
82
                list.addAll(getLayoutTemplates().stream().filter(existingTemplate -> existingTemplate.getCodeName() != nameTemplate.getCodeName()).collect(Collectors.toList()));
1✔
83
                list.add(nameTemplate);
1✔
84
                setLayoutTemplates(list);
1✔
85
        }
1✔
86
        
87
        /**
88
         * @return Returns the defaultLayoutFormat
89
         */
90
        private NameTemplate deserializeXmlTemplate(String xml) {
91
                NameTemplate nameTemplate = null;
1✔
92
                if (StringUtils.isBlank(xml)) {
1✔
93
                        return null;
1✔
94
                }
95
                try {
96
                        nameTemplate = Context.getSerializationService().getDefaultSerializer().deserialize(StringEscapeUtils.unescapeXml(xml),
1✔
97
                                NameTemplate.class);
98
                } catch (Exception e) {
×
99
                        log.error("Error in deserializing provided name template", e);
×
100
                }
1✔
101
                return nameTemplate;
1✔
102
        }
103

104
        /**
105
         * @return Returns the defaultLayoutFormat
106
         */
107
        @Override
108
        public String getDefaultLayoutFormat() {
109
                String ret = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LAYOUT_NAME_FORMAT);
1✔
110
                return (ret != null && ret.length() > 0) ? ret : defaultLayoutFormat;
1✔
111
        }
112

113
        /**
114
         * @see org.openmrs.api.GlobalPropertyListener#supportsPropertyName(String)
115
         */
116
        @Override
117
        public boolean supportsPropertyName(String propertyName) {
118
                return OpenmrsConstants.GLOBAL_PROPERTY_LAYOUT_NAME_TEMPLATE.equals(propertyName);
1✔
119
        }
120

121
        /**
122
         * @see org.openmrs.api.GlobalPropertyListener#globalPropertyChanged(org.openmrs.GlobalProperty)
123
         */
124
        @Override
125
        public void globalPropertyChanged(GlobalProperty newValue) {
126
                NameTemplate nameTemplate = deserializeXmlTemplate(newValue.getPropertyValue());
1✔
127
                if (nameTemplate != null) {
1✔
128
                        updateLayoutTemplates(nameTemplate);
1✔
129
                }        
130
        }
1✔
131

132
        /**
133
         * @see org.openmrs.api.GlobalPropertyListener#globalPropertyDeleted(String)
134
         */
135
        @Override
136
        public void globalPropertyDeleted(String propertyName) {
137

138
        }
×
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc