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  package org.efaps.update.schema.common;
22  
23  import java.net.URL;
24  import java.sql.SQLException;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.efaps.db.Context;
29  import org.efaps.db.Insert;
30  import org.efaps.db.transaction.ConnectionResource;
31  import org.efaps.update.AbstractUpdate;
32  import org.efaps.update.util.InstallationException;
33  import org.efaps.util.EFapsException;
34  
35  /**
36   * Handles the import / update of system configurations for eFaps read from a
37   * XML configuration item file.
38   *
39   * @author The eFaps Team
40   * @version $Id$
41   */
42  public class NumberGeneratorUpdate
43      extends AbstractUpdate
44  {
45      /**
46       * @param _url URL to the xml file
47       */
48      public NumberGeneratorUpdate(final URL _url)
49      {
50          super(_url, "Admin_Common_NumGen");
51      }
52  
53      /**
54       * @see org.efaps.update.AbstractUpdate#newDefinition()
55       * @return new NumberGeneratorDefinition
56       */
57      @Override
58      protected AbstractDefinition newDefinition()
59      {
60          return new NumberGeneratorDefinition();
61      }
62  
63      /**
64       * Definition for the NumberGenerator.
65       */
66      public class NumberGeneratorDefinition extends AbstractDefinition
67      {
68  
69          /**
70           * Start value for this NumberGenerator.
71           */
72          private long startvalue;
73  
74          /**
75           * {@inheritDoc}
76           * @throws EFapsException
77           */
78          @Override
79          protected void readXML(final List<String> _tags,
80                                 final Map<String, String> _attributes,
81                                 final String _text)
82              throws EFapsException
83          {
84              final String value = _tags.get(0);
85              if ("format".equals(value)) {
86                  addValue("Format", _text);
87              } else if ("startvalue".equals(value)) {
88                  this.startvalue = Long.valueOf(_text);
89              } else {
90                  super.readXML(_tags, _attributes, _text);
91              }
92          }
93  
94          /**
95           * {@inheritDoc}
96           */
97          @Override
98          protected void createInDB(final Insert _insert)
99              throws InstallationException
100         {
101             try  {
102                 _insert.add("Format", getValue("Format"));
103             } catch (final EFapsException e)  {
104                 throw new InstallationException("Format could not be defined", e);
105             }
106             super.createInDB(_insert);
107             final ConnectionResource con;
108             try {
109                 con = Context.getThreadContext().getConnectionResource();
110             } catch (final EFapsException e) {
111                 throw new InstallationException("Connection resource could not be fetched", e);
112             }
113             final String name = "numgen_" + ((Long) getInstance().getId()).toString() + "_seq";
114             try {
115                 if (!Context.getDbType().existsSequence(con.getConnection(), name)) {
116                     Context.getDbType().createSequence(con.getConnection(), name, this.startvalue);
117                 }
118             } catch (final SQLException e) {
119                 throw new InstallationException("Sequence '" + name + "' could not be created", e);
120             }
121         }
122     }
123 }