1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
37
38
39
40
41
42 public class NumberGeneratorUpdate
43 extends AbstractUpdate
44 {
45
46
47
48 public NumberGeneratorUpdate(final URL _url)
49 {
50 super(_url, "Admin_Common_NumGen");
51 }
52
53
54
55
56
57 @Override
58 protected AbstractDefinition newDefinition()
59 {
60 return new NumberGeneratorDefinition();
61 }
62
63
64
65
66 public class NumberGeneratorDefinition extends AbstractDefinition
67 {
68
69
70
71
72 private long startvalue;
73
74
75
76
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
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 }