001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.userobjects;
016:
017: import com.metaboss.applications.designstudio.Application;
018: import com.metaboss.applications.designstudio.BaseUserObject;
019: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
020: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
021: import com.metaboss.sdlctools.models.metabossmodel.ModelElementAttachment;
022: import com.metaboss.sdlctools.models.metabossmodel.ModelElementAttachmentUtils;
023:
024: /* ModelElementAttachment user object */
025:
026: public class ModelElementAttachmentUserObject extends BaseUserObject {
027: protected ModelElementAttachment mModelElementAttachment = null;
028:
029: public ModelElementAttachmentUserObject(
030: ModelElementAttachment pModelElementAttachment) {
031: super (pModelElementAttachment, Application.VIEWSOURCE_ICON);
032: mModelElementAttachment = pModelElementAttachment;
033: }
034:
035: // create new Attachment
036: public static ModelElementAttachmentUserObject addNewAttachment(
037: ModelElement pElement) throws Exception {
038: return (ModelElementAttachmentUserObject) new ModelElementAttachmentUserObject(
039: null).addNewObject(getObjectPackage(pElement), pElement
040: .getAttachments());
041: }
042:
043: // create new source attachment
044: public static ModelElementAttachmentUserObject addSourceAttachment(
045: ModelElement pElement) throws Exception {
046: boolean lNeedTransaction = !Application.isInTransaction();
047:
048: if (lNeedTransaction)
049: Application.beginTransaction();
050: try {
051: ModelElementAttachmentUserObject lObject = (ModelElementAttachmentUserObject) new ModelElementAttachmentUserObject(
052: null).createNewObject(getObjectPackage(pElement));
053: if (lObject != null) {
054: ModelElementAttachment lAttachment = lObject
055: .getAttachment();
056: pElement.getAttachments().add(lAttachment);
057: lAttachment.setName(BaseUserObject.SOURCE_ATTACH);
058: lAttachment.setDescription(Application
059: .getString("sourceattachment_description"));
060: }
061: if (lNeedTransaction)
062: Application.commit();
063: } finally {
064: if (lNeedTransaction)
065: Application.checkAndRollback();
066: }
067:
068: return null;
069: }
070:
071: public BaseUserObject createNewObject(MetaBossModelPackage pPackage) {
072: return new ModelElementAttachmentUserObject(pPackage
073: .getModelElementAttachment()
074: .createModelElementAttachment());
075: }
076:
077: public boolean addToTree() {
078: return false;
079: }
080:
081: public ModelElementAttachment getAttachment() {
082: return mModelElementAttachment;
083: }
084:
085: // get element source from attachments
086: public static String getSourceFromAttachment(
087: ModelElementAttachment pAttachment) {
088: String lResult = null;
089:
090: if (pAttachment != null) {
091: try {
092: byte[] lData = ModelElementAttachmentUtils
093: .getAttachmentData(pAttachment);
094: if (lData != null) {
095: String lStr = new String(lData);
096: lResult = lStr;
097: }
098: } catch (Exception e) {
099: e.printStackTrace();
100: }
101: }
102: return lResult;
103: }
104:
105: // spcify source to attachment
106: public static void setSourceToAttachment(
107: ModelElementAttachment pAttachment, String pSource)
108: throws Exception {
109: if (pAttachment != null) {
110: boolean lNeedTransaction = !Application.isInTransaction();
111:
112: if (lNeedTransaction)
113: Application.beginTransaction();
114: try {
115: byte[] lSource = (pSource != null) ? pSource.getBytes()
116: : null;
117: ModelElementAttachmentUtils.setAttachmentData(
118: pAttachment, lSource);
119:
120: if (lNeedTransaction)
121: Application.commit();
122: } finally {
123: if (lNeedTransaction)
124: Application.checkAndRollback();
125: }
126: }
127: }
128: }
|