View Javadoc
1   /*
2    * #%L
3    * EUGene :: EUGene
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.eugene.java.extension;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.nuiton.eugene.models.object.ObjectModelClassifier;
28  
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Extension for ObjectModel to manage imports for all classifiers in the model.
35   *
36   * Created: 2 nov. 2009
37   *
38   * @author Florian Desbois - desbois@codelutin.com
39   * @author Tony Chemit - chemit@codelutin.com
40   */
41  public class ImportsManagerExtension {
42  
43      /**
44       * Logger.
45       */
46      private static final Log log =
47              LogFactory.getLog(ImportsManagerExtension.class);
48  
49      /** Extension static used to identify ImportsManagerExtension in ObjectModel */
50      public static final String OBJECTMODEL_EXTENSION = "imports";
51  
52      /**
53       * Map of ImportsManager with key equals to the classifier qualified name
54       * associated to the ImportsManager.
55       */
56      protected Map<String, ImportsManager> managers;
57  
58      /**
59       * Get the ImportsManager associated to the classifier.
60       *
61       * <strong>Note:</strong> if not exist, it will be created.
62       *
63       * @param classifier reference for the ImportsManager
64       * @return the importsManager associated to the classifier (never null)
65       */
66      public ImportsManager getManager(ObjectModelClassifier classifier) {
67          String fqn = classifier.getQualifiedName();
68          ImportsManager manager = getManager(fqn);
69          return manager;
70      }
71  
72      /**
73       * Get the ImportsManager associated to the given {@code fqn}.
74       *
75       * <strong>Note:</strong> if not exist, it will be created.
76       *
77       * @param fqn reference for the ImportsManager
78       * @return the importsManager associated to the classifier (never null)
79       * @since 2.3.2
80       */
81      public ImportsManager getManager(String fqn) {
82          Map<String, ImportsManager> managers = getManagers();
83          ImportsManager manager = managers.get(fqn);
84          if (manager == null) {
85              manager = new ImportsManager();
86              managers.put(fqn, manager);
87              if (log.isDebugEnabled()) {
88                  log.debug("Add new importsManager for : " + fqn);
89              }
90          }
91          return manager;
92      }
93  
94      /**
95       * Get imports for a classifier. The ImportsManager must be defined in the model.
96       *
97       * @param classifier reference for the imports
98       * @return a List of String which contains all imports for the classifier
99       */
100     public List<String> getImports(ObjectModelClassifier classifier) {
101         List<String> imports = getImports(classifier.getQualifiedName(),
102                                           classifier.getPackageName()
103         );
104         return imports;
105     }
106 
107     /**
108      * Get imports for a classifier.
109      *
110      * The ImportsManager must be defined in the model.
111      *
112      * @param fqn         reference for the imports
113      * @param packageName package name of the fqn
114      * @return a List of String which contains all imports for the classifier
115      * @since 2.3.2
116      */
117     public List<String> getImports(String fqn, String packageName) {
118         ImportsManager manager = getManager(fqn);
119         return manager.getImports(packageName);
120     }
121 
122     protected Map<String, ImportsManager> getManagers() {
123         if (managers == null) {
124             managers = new HashMap<>();
125         }
126         return managers;
127     }
128 }