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 * Author: tmo
17 * Revision: $Rev$
18 * Last Changed: $Date$
19 * Last Changed By: $Author$
20 */
21
22 package org.efaps.db;
23 import java.io.InputStream;
24
25 import org.efaps.admin.access.AccessCache;
26 import org.efaps.admin.access.AccessTypeEnums;
27 import org.efaps.admin.event.EventType;
28 import org.efaps.db.store.Resource;
29 import org.efaps.util.EFapsException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34 * The class is used to check in a file to a given attribute of an object.
35 *
36 * @author The eFaps Team
37 * @version $Id$
38 */
39 public class Checkin
40 extends AbstractAction
41 {
42 /**
43 * Logging instance used in this class.
44 */
45 private static final Logger LOG = LoggerFactory.getLogger(Checkin.class);
46
47 /**
48 * Constructor with a string as object id.
49 *
50 * @param _oid oid of object on which the check in is made
51 */
52 public Checkin(final String _oid)
53 {
54 this(Instance.get(_oid));
55 }
56
57 /**
58 * Constructor with an instance object as object id.
59 *
60 * @param _instance instance on which the check in is made
61 */
62 public Checkin(final Instance _instance)
63 {
64 super.setInstance(_instance);
65 }
66
67 /**
68 * @param _fileName file name to check in (could include also the path)
69 * @param _in input stream with the binary data
70 * @param _size size of file in stream to check in (negative size
71 * means that all from the stream must be written)
72 * @see #executeWithoutAccessCheck(String, InputStream, int)
73 *@throws EFapsException on error
74 */
75 public void execute(final String _fileName,
76 final InputStream _in,
77 final int _size)
78 throws EFapsException
79 {
80 AccessCache.registerUpdate(getInstance());
81 final boolean hasAccess = super.getInstance().getType().hasAccess(super.getInstance(),
82 AccessTypeEnums.CHECKIN.getAccessType());
83 if (!hasAccess) {
84 throw new EFapsException(this.getClass(), "execute.NoAccess");
85 }
86 executeWithoutAccessCheck(_fileName, _in, _size);
87 }
88
89 /**
90 * Executes the check in without checking the access rights (but with
91 * triggers).
92 * <ol>
93 * <li>executes the pre check in trigger (if exists)</li>
94 * <li>executes the check in trigger (if exists)</li>
95 * <li>executes if no check in trigger exists or the check in trigger is
96 * not executed the update ({@see #executeWithoutTrigger})</li>
97 * <li>executes the post check in trigger (if exists)</li>
98 * </ol>
99 *
100 * @param _fileName file name to check in (could include also the path)
101 * @param _in input stream with the binary data
102 * @param _size size of file in stream to check in (negative size
103 * means that all from the stream must be written)
104 * @throws EFapsException if checkout action fails
105 * TODO: history entries
106 */
107 public void executeWithoutAccessCheck(final String _fileName,
108 final InputStream _in,
109 final int _size)
110 throws EFapsException
111 {
112 executeEvents(EventType.CHECKIN_PRE);
113 if (!executeEvents(EventType.CHECKIN_OVERRIDE)) {
114 executeWithoutTrigger(_fileName, _in, _size);
115 }
116 executeEvents(EventType.CHECKIN_POST);
117 }
118
119 /**
120 * The check in is done without calling triggers and check of access
121 * rights. Executes the check in:
122 * <ul>
123 * <li>the file is checked in</li>
124 * <li>the file name and file length is stored in with
125 * {@link org.efaps.db.Update} (complete filename without path)</li>
126 * </ul>
127 *
128 * @param _fileName file name to check in (could include also the path)
129 * @param _in input stream with the binary data
130 * @param _size size of file in stream to check in (negative size means
131 * that all from the stream must be written)
132 * @throws EFapsException if checkout action fails
133 */
134 public void executeWithoutTrigger(final String _fileName,
135 final InputStream _in,
136 final int _size)
137 throws EFapsException
138 {
139 final Context context = Context.getThreadContext();
140 Resource storeRsrc = null;
141 boolean ok = false;
142 try {
143 getInstance().getType();
144 storeRsrc = context.getStoreResource(getInstance(), Resource.StoreEvent.WRITE);
145 storeRsrc.write(_in, _size, _fileName);
146 storeRsrc.commit();
147 storeRsrc = null;
148 ok = true;
149 } catch (final EFapsException e) {
150 Checkin.LOG.error("could not checkin " + super.getInstance(), e);
151 throw e;
152 } finally {
153 if (!ok) {
154 context.abort();
155 }
156 if ((storeRsrc != null) && (storeRsrc.isOpened())) {
157 storeRsrc.abort();
158 }
159 }
160 }
161 }