01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.metaboss.sdlctools.models.metabossmodel;
16:
17: import com.metaboss.sdlctools.models.ModelRepositoryException;
18: import com.metaboss.sdlctools.models.impl.ModelRepositoryImpl;
19:
20: /** Set of ModelElementAttachment utility and helper methods realising some facilities seemingly not provided by MDR. */
21: public class ModelElementAttachmentUtils {
22: /** This class is a "toolbox" with static utilities and can not be instantiated */
23: private ModelElementAttachmentUtils() {
24: }
25:
26: /** Sets the data to the specified attachment. This method will use active transaction
27: * if there is one or will execute within its own transaction if repository is not in transaction
28: * at the time when this method is called.
29: * @param pAttachment - the attachment element to save data for
30: * @param pAtachmentData - the data to save. Setting this parameter to null or zero-length array will simply
31: * truncate the attachment data, but it will not delete attachment from the model. To completely
32: * remove attachment - the ModelElementAttachment object itself must be deleted from the model. */
33: public static void setAttachmentData(
34: ModelElementAttachment pAttachment, byte[] pAtachmentData)
35: throws ModelRepositoryException {
36: ModelRepositoryImpl lRepository = ModelRepositoryImpl
37: .getRepository();
38: // Selfmanage transaction if necessary
39: boolean lSuccess = false;
40: boolean lNeedToManageTransaction = lRepository
41: .isInTransaction() == false;
42: if (lNeedToManageTransaction)
43: lRepository.beginTransaction();
44: try {
45: lRepository.setDataAttachedToObject(pAttachment,
46: pAtachmentData);
47: } finally {
48: if (lNeedToManageTransaction) {
49: if (lSuccess)
50: lRepository.commitTransaction();
51: else
52: lRepository.rollbackTransaction();
53: }
54: }
55: }
56:
57: /** Gets the data from the specified attachment. This method is aware of
58: * changes to the data during transaction and since the last save. In other words if there is
59: * unsaved or even uncomitted data for this attachment it will be returned instead of
60: * last saved data.
61: * @param pAttachment - the attachment element to save data for */
62: public static byte[] getAttachmentData(
63: ModelElementAttachment pAttachment)
64: throws ModelRepositoryException {
65: ModelRepositoryImpl lRepository = ModelRepositoryImpl
66: .getRepository();
67: return lRepository.getDataAttachedToObject(pAttachment);
68: }
69:
70: /** Deletes the data from the specified attachment.
71: * @param pAttachment - the attachment element to delete the data from */
72: public static void deleteAttachmentData(
73: ModelElementAttachment pAttachment)
74: throws ModelRepositoryException {
75: ModelRepositoryImpl lRepository = ModelRepositoryImpl
76: .getRepository();
77: // Selfmanage transaction if necessary
78: boolean lSuccess = false;
79: boolean lNeedToManageTransaction = lRepository
80: .isInTransaction() == false;
81: if (lNeedToManageTransaction)
82: lRepository.beginTransaction();
83: try {
84: lRepository.clearDataAttachedToObject(pAttachment);
85: } finally {
86: if (lNeedToManageTransaction) {
87: if (lSuccess)
88: lRepository.commitTransaction();
89: else
90: lRepository.rollbackTransaction();
91: }
92: }
93: }
94: }
|