KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > TransientStorableAssociation


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdr.storagemodel;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import org.netbeans.mdr.persistence.Storage;
26 import org.netbeans.mdr.persistence.Index;
27 import org.netbeans.mdr.persistence.StorageBadRequestException;
28 import org.netbeans.mdr.persistence.StorageException;
29 import org.netbeans.mdr.storagemodel.transientimpl.*;
30 import org.netbeans.mdr.util.IOUtils;
31 import org.netbeans.mdr.util.DebugException;
32 /**
33  *
34  * @author Tomas Zezula
35  */

36 public class TransientStorableAssociation extends StorableAssociation implements Transient {
37     
38     private static final String JavaDoc FORMAT = "{0}-{1}-{2}"; // No I18N
39

40     
41     
42     private boolean uniqueA;
43     private boolean uniqueB;
44     
45     private transient Index atIndex;
46     private transient Index btIndex;
47     
48     /** Creates a new instance of TransientStorableAssociation */
49     public TransientStorableAssociation() {
50     }
51     
52     /** Creates new instance of association proxy providing all needed arguments.
53      * @param mdrStorage parent storage of created association proxy being created
54      * @param immediatePackage MOFID of association proxy immediate package
55      * @param MOFID of meta association proxy meta object
56      * @param endAMofid MOFID of the first association end
57      * @param endBMofid MOFID of the second association end
58      * @param multiValuedA <code>true</code> if the first end is multivalued
59      * @param multiValuedB <code>true</code> if the second end is multivalued
60      * @param orderedA <code>true</code> if the first end is ordered
61      * @param orderedB <code>true</code> if the second end is ordered
62      * @param uniqueA <code>true</code> if the first end is unique
63      * @param uniqueB <code>true</code> if the second end is unique
64      * @param aggrA <code>true</code> if the first end is aggregate
65      * @param aggrB <code>true</code> if the second end is aggregate
66      * @throws StorageException problem in storage occurred during association proxy creation
67      */

68     public TransientStorableAssociation(MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID immediatePackage, org.netbeans.mdr.persistence.MOFID meta,
69         String JavaDoc endA, org.netbeans.mdr.persistence.MOFID endAId, String JavaDoc endB, org.netbeans.mdr.persistence.MOFID endBId, Class JavaDoc typeA, Class JavaDoc typeB,
70         int minA, int maxA, int minB, int maxB, boolean orderedA, boolean orderedB,
71         boolean uniqueA, boolean uniqueB, boolean aggrA, boolean aggrB) throws StorageException {
72         super(mdrStorage, immediatePackage, meta, endA, endAId, endB, endBId, typeA, typeB,
73         minA, maxA, minB, maxB, orderedA, orderedB, uniqueA, uniqueB, aggrA, aggrB, false, false);
74         this.uniqueA = uniqueA;
75         this.uniqueB = uniqueB;
76     }
77     
78     public void write(java.io.OutputStream JavaDoc outputStream) {
79         try {
80             IOUtils.writeBoolean (outputStream, this.uniqueA);
81             IOUtils.writeBoolean (outputStream, this.uniqueB);
82         } catch (java.io.IOException JavaDoc ioe) {
83             throw new DebugException ();
84         }
85         super.write(outputStream);
86     }
87     
88     public void read(java.io.InputStream JavaDoc inputStream) {
89         try {
90             this.uniqueA = IOUtils.readBoolean (inputStream);
91             this.uniqueB = IOUtils.readBoolean (inputStream);
92         } catch (java.io.IOException JavaDoc ioe) {
93             throw new DebugException ();
94         }
95         super.read(inputStream);
96     }
97     
98     /**
99      * @param multi
100      * @param ordered
101      * @param unique
102      * @param end
103      * @throws StorageException
104      * @return
105      */

106     protected Index createIndex(boolean multi, boolean ordered, boolean unique, int end) throws StorageException {
107         Index result = null;
108         String JavaDoc name = this.getIndexName (end);
109         if (multi) {
110             if (ordered) {
111                 result = this.getMdrStorage().getTransientStorage().createMultivaluedOrderedIndex (name, Storage.EntryType.MOFID, Storage.EntryType.MOFID, unique);
112             }
113             else {
114                 result = this.getMdrStorage().getTransientStorage().createMultivaluedIndex (name, Storage.EntryType.MOFID, Storage.EntryType.MOFID, unique);
115             }
116         }
117         else {
118             result = this.getMdrStorage().getTransientStorage().createSinglevaluedIndex (name, Storage.EntryType.MOFID, Storage.EntryType.MOFID);
119         }
120         
121         if (end == 1) {
122             this.atIndex = result;
123         }
124         else {
125             this.btIndex = result;
126         }
127         return result;
128     }
129     
130     
131     /** Find the appropriate index in the storage.
132      */

133     protected Index findIndex(int end) throws StorageException {
134         if (end == 1) {
135             if (this.atIndex == null) {
136                 String JavaDoc name = this.getIndexName (end);
137                 this.atIndex = this.getMdrStorage ().getTransientStorage ().getIndex (name);
138                 // Is MDR restart or only drop of StorableAssociation from memory
139
if (this.atIndex == null) {
140                     // Reinitialize after MDR restart
141
this.atIndex = this.createIndex (this.isMultivaluedA (), this.isOrderedA (), this.isUniqueA (), end);
142                 }
143             }
144             return this.atIndex;
145         }
146         else {
147             if (this.btIndex == null) {
148                 String JavaDoc name = this.getIndexName (end);
149                 this.btIndex = this.getMdrStorage ().getTransientStorage ().getIndex (name);
150                 // Is MDR restart or only drop of StorableAssociation from memory
151
if (this.btIndex == null) {
152                     // Reinitialize after MDR restart
153
this.btIndex = this.createIndex (this.isMultivaluedB (), this.isOrderedB (), this.isUniqueB (), end);
154                 }
155             }
156             return this.btIndex;
157         }
158     }
159     
160     
161     protected void deleteIndex(String JavaDoc opkgId, String JavaDoc mofId, int end) throws StorageException {
162         String JavaDoc name = this.getIndexName (end);
163         this.getMdrStorage ().getTransientStorage ().dropIndex (name);
164         if (end == 1)
165             this.atIndex = null;
166         else
167             this.btIndex = null;
168     }
169     
170     protected boolean isUniqueA() {
171         return this.uniqueA;
172     }
173     
174     protected boolean isUniqueB() {
175         return this.uniqueB;
176     }
177     
178     private String JavaDoc getIndexName (int end) {
179         return MessageFormat.format(FORMAT, new Object JavaDoc[] {this.getOutermostPackageId(), this.getMofId(), new Integer JavaDoc(end)});
180     }
181     
182     
183 }
184
Popular Tags