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.schema;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.commons.lang3.builder.ToStringBuilder;
32 import org.efaps.db.Checkin;
33 import org.efaps.update.AbstractUpdate;
34 import org.efaps.update.UpdateLifecycle;
35 import org.efaps.update.util.InstallationException;
36 import org.efaps.util.EFapsException;
37
38
39 /**
40 * Abstract class for the different image updates/inserts.
41 *
42 * @author The eFaps Team
43 * @version $Id$
44 */
45 public abstract class AbstractFileUpdate
46 extends AbstractUpdate
47 {
48 /**
49 * Name of the root path used to initialize the path for the image.
50 */
51 private final String root;
52
53 /**
54 * @param _url URL to the import definition file
55 * @param _dataModelTypeName name of the datamodel type
56 */
57 protected AbstractFileUpdate(final URL _url,
58 final String _dataModelTypeName)
59 {
60 this(_url, _dataModelTypeName, null);
61 }
62
63 /**
64 * @param _url URL to the import definition file
65 * @param _dataModelTypeName name of the datamodel type
66 * @param _links set of links
67 */
68 protected AbstractFileUpdate(final URL _url,
69 final String _dataModelTypeName,
70 final Set <Link> _links)
71 {
72 super(_url, _dataModelTypeName, _links);
73 final String urlStr = _url.toString();
74 final int i = urlStr.lastIndexOf("/");
75 this.root = urlStr.substring(0, i + 1);
76 }
77
78 /**
79 * Abstract image definition.
80 */
81 protected abstract class AbstractFileDefinition
82 extends AbstractDefinition
83 {
84 /**
85 * Name of the Image file (including the path) to import.
86 */
87 private String file;
88
89 /**
90 * Interprets the image specific part of the XML configuration item
91 * file. Following information is read:
92 * <ul>
93 * <li>name of the {@link #file}</li>
94 * </ul>
95 *
96 * @param _tags current path as list of single tags
97 * @param _attributes attributes for current path
98 * @param _text content for current path
99 * @throws EFapsException on error
100 */
101 @Override
102 protected void readXML(final List<String> _tags,
103 final Map<String, String> _attributes,
104 final String _text)
105 throws EFapsException
106 {
107 final String value = _tags.get(0);
108 if ("file".equals(value)) {
109 this.file = _text;
110 } else {
111 super.readXML(_tags, _attributes, _text);
112 }
113 }
114
115 /**
116 * Updates / creates the instance in the database. If a
117 * {@link #file file name} is given, this file is checked in the
118 * created image instance.
119 *
120 * @param _step current update step
121 * @param _allLinkTypes set of all type of links
122 * @throws InstallationException if update failed
123 */
124 @Override
125 protected void updateInDB(final UpdateLifecycle _step,
126 final Set<Link> _allLinkTypes)
127 throws InstallationException
128 {
129 super.updateInDB(_step, _allLinkTypes);
130
131 if ((_step == UpdateLifecycle.EFAPS_UPDATE) && (this.file != null)) {
132 try {
133 final InputStream in = new URL(AbstractFileUpdate.this.root + this.file).openStream();
134 try {
135 final Checkin checkin = new Checkin(getInstance());
136 checkin.executeWithoutAccessCheck(this.file, in, in.available());
137 } finally {
138 in.close();
139 }
140 } catch (final EFapsException e) {
141 throw new InstallationException("Check of file '" + AbstractFileUpdate.this.root + this.file
142 + "' failed", e);
143 } catch (final IOException e) {
144 throw new InstallationException("It seems that file '" + AbstractFileUpdate.this.root + this.file
145 + "' does not exists or is not accessable.", e);
146 }
147 }
148 }
149
150 /**
151 * Returns a string representation with values of all instance variables
152 * of an image definition.
153 *
154 * @return string representation of this definition of a column
155 */
156 @Override
157 public String toString()
158 {
159 return new ToStringBuilder(this)
160 .appendSuper(super.toString())
161 .append("file", this.file).toString();
162 }
163 }
164 }