KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > PartImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.repository.commonimpl;
17
18 import org.outerj.daisy.repository.Part;
19 import org.outerj.daisy.repository.RepositoryException;
20 import org.outerj.daisy.repository.PartHelper;
21 import org.outerj.daisy.repository.PartDataSource;
22 import org.outerj.daisy.repository.schema.PartType;
23 import org.outerx.daisy.x10.PartDocument;
24
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 public class PartImpl implements Part {
29     private long partTypeId;
30     private String JavaDoc blobKey;
31     private long size = 0;
32     private String JavaDoc mimeType;
33     private String JavaDoc fileName;
34     private PartDataSource source;
35     private DocumentVariantImpl.IntimateAccess ownerVariantInt;
36     private boolean newOrUpdated = false;
37     private boolean dataUpdated = false;
38
39     /**
40      * The ID of the version to which this part belongs, or in case the part belongs
41      * directly to the document variant object, the last version ID (which could be -1 for a
42      * new document variant).
43      * */

44     private long versionId;
45     private IntimateAccess intimateAccess = new IntimateAccess();
46
47     public PartImpl(DocumentVariantImpl.IntimateAccess ownerVariantInt, long partTypeId, long versionId) {
48         this.ownerVariantInt = ownerVariantInt;
49         this.partTypeId = partTypeId;
50         this.versionId = versionId;
51     }
52
53     public IntimateAccess getIntimateAccess(DocumentStrategy documentStrategy) {
54         if (ownerVariantInt.getDocumentStrategy() == documentStrategy)
55             return intimateAccess;
56         else
57             return null;
58     }
59
60     public String JavaDoc getMimeType() {
61         return mimeType;
62     }
63
64     public String JavaDoc getFileName() {
65         return fileName;
66     }
67
68     public long getSize() {
69         return size;
70     }
71
72     public byte[] getData() throws RepositoryException {
73         if (source != null) {
74             try {
75                 return PartHelper.streamToByteArrayAndClose(source.createInputStream(), (int)source.getSize());
76             } catch (IOException JavaDoc e) {
77                 throw new RepositoryException("Error creating part input stream", e);
78             }
79         } else {
80             if (versionId == -1 && blobKey == null) {
81                 throw new RuntimeException JavaDoc("Strange situation: versionId is -1 and blobKey is null in PartImpl.getData(). This should never be the case.");
82             }
83             try {
84                 if (blobKey != null) {
85                     // if we know the blobkey (yes in local impl, no in remote impl),
86
// directly retrieve the blob using the blobkey, which is faster
87
return PartHelper.streamToByteArrayAndClose(ownerVariantInt.getDocumentStrategy().getBlob(blobKey), (int)getSize());
88                 } else {
89                     DocumentVariantImpl variant = ownerVariantInt.getVariant();
90                     return PartHelper.streamToByteArrayAndClose(ownerVariantInt.getDocumentStrategy().getBlob(
91                             variant.getDocumentId(), variant.getBranchId(), variant.getLanguageId(),
92                             versionId, partTypeId, ownerVariantInt.getCurrentUser()), (int)getSize());
93                 }
94             } catch (IOException JavaDoc e) {
95                 throw new RepositoryException("Error reading input stream", e);
96             }
97         }
98     }
99
100     public InputStream JavaDoc getDataStream() throws RepositoryException {
101         if (source != null) {
102             try {
103                 return source.createInputStream();
104             } catch (IOException JavaDoc e) {
105                 throw new RepositoryException("Error creating part input stream", e);
106             }
107         } else {
108             if (versionId == -1 && blobKey == null) {
109                 throw new RuntimeException JavaDoc("Strange situation: versionId is -1 and blobKey is null in PartImpl.getDataStream(). This should never be the case.");
110             }
111             if (blobKey != null) {
112                 // if we know the blobkey (yes in local impl, no in remote impl),
113
// directly retrieve the blob using the blobkey, which is faster
114
return ownerVariantInt.getDocumentStrategy().getBlob(blobKey);
115             } else {
116                 DocumentVariantImpl variant = ownerVariantInt.getVariant();
117                 return ownerVariantInt.getDocumentStrategy().getBlob(variant.getDocumentId(), variant.getBranchId(),
118                         variant.getLanguageId(), versionId, partTypeId, ownerVariantInt.getCurrentUser());
119             }
120         }
121     }
122
123     public String JavaDoc getTypeName() {
124         PartType partType = null;
125         try {
126             partType = ownerVariantInt.getRepositorySchema().getPartTypeById(partTypeId, false, ownerVariantInt.getCurrentUser());
127         } catch (RepositoryException e) {
128             throw new RuntimeException JavaDoc(DocumentImpl.ERROR_ACCESSING_REPOSITORY_SCHEMA, e);
129         }
130         return partType.getName();
131     }
132
133     public long getTypeId() {
134         return partTypeId;
135     }
136
137     public PartDocument getXml() {
138         PartDocument partDocument = PartDocument.Factory.newInstance();
139         PartDocument.Part partXml = partDocument.addNewPart();
140         partXml.setTypeId(getTypeId());
141         partXml.setSize(getSize());
142         partXml.setMimeType(getMimeType());
143         if (fileName != null)
144             partXml.setFileName(fileName);
145         return partDocument;
146     }
147
148     public class IntimateAccess {
149         private IntimateAccess() {
150         }
151
152         public PartDataSource getPartDataSource() {
153             return PartImpl.this.source;
154         }
155
156         public void setMimeType(String JavaDoc mimeType) {
157             PartImpl.this.mimeType = mimeType;
158         }
159
160         public void setFileName(String JavaDoc fileName) {
161             PartImpl.this.fileName = fileName;
162         }
163
164         public void setData(PartDataSource partDataSource) {
165             if (partDataSource != null) {
166                 PartImpl.this.source = partDataSource;
167                 PartImpl.this.size = partDataSource.getSize();
168             } else {
169                 PartImpl.this.source = null;
170             }
171         }
172
173         public void setSize(long size) {
174             PartImpl.this.size = size;
175         }
176
177         public void setBlobKey(String JavaDoc blobKey) {
178             PartImpl.this.blobKey = blobKey;
179         }
180
181         public String JavaDoc getBlobKey() {
182             return blobKey;
183         }
184
185         public boolean isNewOrUpdated() {
186             return newOrUpdated;
187         }
188
189         public boolean isDataUpdated() {
190             return dataUpdated;
191         }
192
193         public void setVersionId(long versionId) {
194             PartImpl.this.versionId = versionId;
195         }
196
197         public void setNewOrUpdated(boolean newOrUpdated, boolean dataUpdated) {
198             if (dataUpdated && !newOrUpdated)
199                 throw new IllegalArgumentException JavaDoc("If dataUpdated is true, newOrUpdated should also be true.");
200
201             PartImpl.this.newOrUpdated = newOrUpdated;
202             PartImpl.this.dataUpdated = dataUpdated;
203         }
204
205         public PartImpl internalDuplicate(DocumentVariantImpl.IntimateAccess newOwnerVariantInt) {
206             PartImpl partImpl = new PartImpl(newOwnerVariantInt, partTypeId, -1);
207             partImpl.blobKey = blobKey;
208             partImpl.size = size;
209             partImpl.partTypeId = partTypeId;
210             partImpl.mimeType = mimeType;
211             partImpl.newOrUpdated = false;
212             partImpl.dataUpdated = false;
213             return partImpl;
214         }
215     }
216 }
217
Popular Tags