1 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 ; 26 import java.io.IOException ; 27 28 public class PartImpl implements Part { 29 private long partTypeId; 30 private String blobKey; 31 private long size = 0; 32 private String mimeType; 33 private String fileName; 34 private PartDataSource source; 35 private DocumentVariantImpl.IntimateAccess ownerVariantInt; 36 private boolean newOrUpdated = false; 37 private boolean dataUpdated = false; 38 39 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 getMimeType() { 61 return mimeType; 62 } 63 64 public String 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 e) { 77 throw new RepositoryException("Error creating part input stream", e); 78 } 79 } else { 80 if (versionId == -1 && blobKey == null) { 81 throw new RuntimeException ("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 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 e) { 95 throw new RepositoryException("Error reading input stream", e); 96 } 97 } 98 } 99 100 public InputStream getDataStream() throws RepositoryException { 101 if (source != null) { 102 try { 103 return source.createInputStream(); 104 } catch (IOException e) { 105 throw new RepositoryException("Error creating part input stream", e); 106 } 107 } else { 108 if (versionId == -1 && blobKey == null) { 109 throw new RuntimeException ("Strange situation: versionId is -1 and blobKey is null in PartImpl.getDataStream(). This should never be the case."); 110 } 111 if (blobKey != null) { 112 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 getTypeName() { 124 PartType partType = null; 125 try { 126 partType = ownerVariantInt.getRepositorySchema().getPartTypeById(partTypeId, false, ownerVariantInt.getCurrentUser()); 127 } catch (RepositoryException e) { 128 throw new RuntimeException (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 mimeType) { 157 PartImpl.this.mimeType = mimeType; 158 } 159 160 public void setFileName(String 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 blobKey) { 178 PartImpl.this.blobKey = blobKey; 179 } 180 181 public String 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 ("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 |