This project has retired. For details please refer to its
Attic page.
ObjectCreator xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.inmemory;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.math.BigInteger;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30
31 import junit.framework.Assert;
32 import org.apache.chemistry.opencmis.commons.PropertyIds;
33 import org.apache.chemistry.opencmis.commons.data.Acl;
34 import org.apache.chemistry.opencmis.commons.data.ContentStream;
35 import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
36 import org.apache.chemistry.opencmis.commons.data.Properties;
37 import org.apache.chemistry.opencmis.commons.data.PropertyData;
38 import org.apache.chemistry.opencmis.commons.data.PropertyString;
39 import org.apache.chemistry.opencmis.commons.enums.VersioningState;
40 import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
41 import org.apache.chemistry.opencmis.commons.spi.BindingsObjectFactory;
42 import org.apache.chemistry.opencmis.commons.spi.Holder;
43 import org.apache.chemistry.opencmis.commons.spi.ObjectService;
44
45 public class ObjectCreator {
46
47 private final BindingsObjectFactory fFactory;
48 private final ObjectService fObjSvc;
49 private final String fRepositoryId;
50
51 public ObjectCreator(BindingsObjectFactory factory, ObjectService objSvc, String repositoryId) {
52 fObjSvc = objSvc;
53 fFactory = factory;
54 fRepositoryId = repositoryId;
55 }
56
57 public String createDocument(String name, String typeId, String folderId, VersioningState versioningState,
58 Map<String, String> propsToSet) {
59 ContentStream contentStream = null;
60 List<String> policies = null;
61 Acl addACEs = null;
62 Acl removeACEs = null;
63 ExtensionsData extension = null;
64
65 Properties props = createStringDocumentProperties(name, typeId, propsToSet);
66
67 contentStream = createContent();
68
69 String id = null;
70 id = fObjSvc.createDocument(fRepositoryId, props, folderId, contentStream, versioningState, policies, addACEs,
71 removeACEs, extension);
72 if (null == id) {
73 Assert.fail("createDocument failed.");
74 }
75
76 return id;
77 }
78
79 public Properties createStringDocumentProperties(String name, String typeId, Map<String, String> propsToSet) {
80 List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
81 properties.add(fFactory.createPropertyIdData(PropertyIds.NAME, name));
82 properties.add(fFactory.createPropertyIdData(PropertyIds.OBJECT_TYPE_ID, typeId));
83 if (null != propsToSet) {
84 for (Entry<String, String> propToSet : propsToSet.entrySet()) {
85 properties.add(fFactory.createPropertyStringData(propToSet.getKey(), propToSet.getValue()));
86 }
87 }
88 Properties props = fFactory.createPropertiesData(properties);
89 return props;
90 }
91
92 public ContentStream createContent() {
93 ContentStreamImpl content = new ContentStreamImpl();
94 content.setFileName("data.txt");
95 content.setMimeType("text/plain");
96 int len = 32 * 1024;
97 byte[] b = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x0c, 0x0a,
98 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x0c, 0x0a };
99
100 ByteArrayOutputStream ba = new ByteArrayOutputStream(len);
101 try {
102 for (int i = 0; i < 1024; i++) {
103 ba.write(b);
104 }
105 } catch (IOException e) {
106 throw new RuntimeException("Failed to fill content stream with data", e);
107 }
108 content.setStream(new ByteArrayInputStream(ba.toByteArray()));
109 content.setLength(BigInteger.valueOf(len));
110 return content;
111 }
112
113 public ContentStream createAlternateContent() {
114 ContentStreamImpl content = new ContentStreamImpl();
115 content.setFileName("data.txt");
116 content.setMimeType("text/plain");
117 int len = 32 * 1024;
118 byte[] b = { 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
119 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61 };
120
121 ByteArrayOutputStream ba = new ByteArrayOutputStream(len);
122 try {
123 for (int i = 0; i < 1024; i++) {
124 ba.write(b);
125 }
126 } catch (IOException e) {
127 throw new RuntimeException("Failed to fill content stream with data", e);
128 }
129 content.setStream(new ByteArrayInputStream(ba.toByteArray()));
130 content.setLength(BigInteger.valueOf(len));
131 return content;
132 }
133
134
135
136
137 public boolean verifyContent(ContentStream csd1, ContentStream csd2) {
138 if (!csd1.getFileName().equals(csd2.getFileName())) {
139 return false;
140 }
141 if (!csd1.getBigLength().equals(csd2.getBigLength())) {
142 return false;
143 }
144 if (!csd1.getMimeType().equals(csd2.getMimeType())) {
145 return false;
146 }
147 long len = csd1.getBigLength().longValue();
148 InputStream s1 = csd1.getStream();
149 InputStream s2 = csd2.getStream();
150 try {
151 for (int i = 0; i < len; i++) {
152 int val1 = s1.read();
153 int val2 = s2.read();
154 if (val1 != val2) {
155 return false;
156 }
157 }
158 } catch (IOException e) {
159 e.printStackTrace();
160 return false;
161 }
162 return true;
163 }
164
165 public void updateProperty(String id, String propertyId, String propertyValue) {
166 Properties properties = getUpdatePropertyList(propertyId, propertyValue);
167
168 Holder<String> idHolder = new Holder<String>(id);
169 Holder<String> changeTokenHolder = new Holder<String>();
170 fObjSvc.updateProperties(fRepositoryId, idHolder, changeTokenHolder, properties, null);
171 }
172
173 public Properties getUpdatePropertyList(String propertyId, String propertyValue) {
174 List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
175 properties.add(fFactory.createPropertyStringData(propertyId, propertyValue));
176 Properties newProps = fFactory.createPropertiesData(properties);
177 return newProps;
178 }
179
180 public boolean verifyProperty(String id, String propertyId, String propertyValue) {
181 Properties props = fObjSvc.getProperties(fRepositoryId, id, "*", null);
182 Map<String, PropertyData<?>> propsMap = props.getProperties();
183 PropertyString pd = (PropertyString) propsMap.get(propertyId);
184 return propertyValue.equals(pd.getFirstValue());
185 }
186
187 }