1   /*
2    * Copyright 2003 - 2013 The eFaps Team
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   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  
22  package org.efaps.update;
23  
24  import java.sql.SQLException;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.efaps.ci.CIAdminCommon;
29  import org.efaps.db.Context;
30  import org.efaps.db.InstanceQuery;
31  import org.efaps.db.QueryBuilder;
32  import org.efaps.update.util.InstallationException;
33  import org.efaps.util.EFapsException;
34  
35  
36  /**
37   * TODO comment!
38   *
39   * @author The eFaps Team
40   * @version $Id$
41   */
42  public final class AppDependency
43  {
44  
45      /**
46       * The mapping of all names to their Dependency.
47       */
48      private static final Map<String, AppDependency> NAME2APPDEPENDENCY = new  HashMap<String, AppDependency>();
49  
50      /**
51       * Name of the AppDependency.
52       */
53      private final String name;
54  
55      /**
56       * met or not.
57       */
58      private Boolean met;
59  
60      /**
61       * @param _name the name of the AppDependency
62       */
63      private AppDependency(final String _name)
64      {
65          this.name = _name;
66      }
67  
68      /**
69       * @return true if the dependency is met
70       * @throws InstallationException on error
71       */
72      public boolean isMet()
73          throws InstallationException
74      {
75          if (this.met == null) {
76              try {
77                  if (Context.getDbType().existsView(Context.getThreadContext().getConnection(), "V_ADMINTYPE")
78                                  && CIAdminCommon.Version.getType() != null) {
79                      final QueryBuilder queryBldr = new QueryBuilder(CIAdminCommon.Version);
80                      queryBldr.addWhereAttrEqValue(CIAdminCommon.Version.Name, this.name);
81                      final InstanceQuery query = queryBldr.getQuery();
82                      this.met = query.executeWithoutAccessCheck().size() > 0;
83                  } else {
84                      this.met = true;
85                  }
86              } catch (final EFapsException e) {
87                  throw new InstallationException("Latest version could not be found", e);
88              } catch (final SQLException e) {
89                  throw new InstallationException("Latest version could not be found", e);
90              } finally {
91                  if (this.met == null) {
92                      this.met = true;
93                  }
94              }
95          }
96          return this.met;
97      }
98      /**
99       * Getter method for the instance variable {@link #name}.
100      *
101      * @return value of instance variable {@link #name}
102      */
103     public String getName()
104     {
105         return this.name;
106     }
107 
108     @Override
109     public String toString()
110     {
111         return this.name;
112     }
113 
114     @Override
115     public int hashCode()
116     {
117         return this.name.hashCode();
118     }
119 
120     @Override
121     public boolean equals(final Object _object)
122     {
123         boolean ret;
124         if (_object instanceof AppDependency) {
125             ret = this.name.equals(((AppDependency) _object).getName());
126         } else {
127             ret = false;
128         }
129         return ret;
130     }
131 
132     /**
133      * @param _name name of a AppDependency
134      * @return the AppDependency for the given name
135      */
136     public static AppDependency getAppDependency(final String _name)
137     {
138         final AppDependency ret;
139         if (AppDependency.NAME2APPDEPENDENCY.containsKey(_name)) {
140             ret = AppDependency.NAME2APPDEPENDENCY.get(_name);
141         } else {
142             ret = new AppDependency(_name);
143             AppDependency.NAME2APPDEPENDENCY.put(_name, ret);
144         }
145         return ret;
146     }
147 
148     /**
149      *
150      */
151     public static void initialise()
152     {
153         AppDependency.NAME2APPDEPENDENCY.clear();
154     }
155 }