View Javadoc
1   /*
2    * Copyright 2010 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.modules.hl7.config;
17  
18  import java.util.Collection;
19  
20  import ca.uhn.hl7v2.parser.ModelClassFactory;
21  import org.openehealth.ipf.commons.core.config.OrderedConfigurer;
22  import org.openehealth.ipf.commons.core.config.Registry;
23  import org.openehealth.ipf.modules.hl7.parser.CustomModelClassFactory;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * Configurer used to configure all {@link CustomModelClasses}
29   * bean occurrences in the spring application context. Provides
30   * the logic to add the new package definitions after the
31   * existing ones.
32   *
33   * @author Boris Stanojevic
34   * @author Christian Ohr
35   */
36  public class CustomModelClassFactoryConfigurer<R extends Registry> extends OrderedConfigurer<CustomModelClasses, R> {
37  
38      private CustomModelClassFactory customModelClassFactory;
39  
40      private static final Logger LOG = LoggerFactory.getLogger(CustomModelClassFactoryConfigurer.class);
41  
42      boolean configureRecursively = true;
43  
44      @Override
45      public Collection<CustomModelClasses> lookup(R registry) {
46          return registry.beans(CustomModelClasses.class).values();
47      }
48  
49      @Override
50      public void configure(CustomModelClasses configuration) throws Exception {
51          // update the top ModelClassFactory
52          ModelClassFactory delegateFactory = configureAndDelegate(customModelClassFactory, configuration);
53          // delegate if required
54          CustomModelClassFactory currentFactory;
55          while (isConfigureRecursively() && (delegateFactory instanceof CustomModelClassFactory)) {
56              currentFactory = (CustomModelClassFactory) delegateFactory;
57              delegateFactory = configureAndDelegate(currentFactory, configuration);
58          }
59          LOG.debug("Custom model classes configured: {}", configuration);
60      }
61  
62      private ModelClassFactory configureAndDelegate(CustomModelClassFactory factory,
63                                                     CustomModelClasses configuration) {
64          factory.addModels(configuration.getModelClasses());
65          return factory.getDelegate();
66      }
67  
68      public CustomModelClassFactory getCustomModelClassFactory() {
69          return customModelClassFactory;
70      }
71  
72      public void setCustomModelClassFactory(
73              CustomModelClassFactory customModelClassFactory) {
74          this.customModelClassFactory = customModelClassFactory;
75      }
76  
77      public boolean isConfigureRecursively() {
78          return configureRecursively;
79      }
80  
81      public void setConfigureRecursively(boolean configureRecursively) {
82          this.configureRecursively = configureRecursively;
83      }
84  }