This project has retired. For details please refer to its Attic page.
ClientModel xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.workbench.model;
20  
21  import java.io.BufferedInputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.InputStream;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Comparator;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.swing.event.EventListenerList;
33  
34  import org.apache.chemistry.opencmis.client.api.CmisObject;
35  import org.apache.chemistry.opencmis.client.api.FileableCmisObject;
36  import org.apache.chemistry.opencmis.client.api.Folder;
37  import org.apache.chemistry.opencmis.client.api.ItemIterable;
38  import org.apache.chemistry.opencmis.client.api.ObjectId;
39  import org.apache.chemistry.opencmis.client.api.ObjectType;
40  import org.apache.chemistry.opencmis.client.api.OperationContext;
41  import org.apache.chemistry.opencmis.client.api.QueryResult;
42  import org.apache.chemistry.opencmis.client.api.Session;
43  import org.apache.chemistry.opencmis.client.api.Tree;
44  import org.apache.chemistry.opencmis.client.runtime.OperationContextImpl;
45  import org.apache.chemistry.opencmis.commons.PropertyIds;
46  import org.apache.chemistry.opencmis.commons.data.ContentStream;
47  import org.apache.chemistry.opencmis.commons.data.RepositoryCapabilities;
48  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
49  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
50  import org.apache.chemistry.opencmis.commons.enums.CapabilityChanges;
51  import org.apache.chemistry.opencmis.commons.enums.CapabilityQuery;
52  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
53  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
54  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
55  import org.apache.chemistry.opencmis.commons.impl.MimeTypes;
56  import org.apache.chemistry.opencmis.workbench.RandomInputStream;
57  
58  public class ClientModel {
59  
60      // object details must not be older than 60 seconds
61      private static final long OLD = 60 * 1000;
62  
63      private ClientSession clientSession;
64  
65      private Folder currentFolder = null;
66      private List<CmisObject> currentChildren = Collections.emptyList();
67      private CmisObject currentObject = null;
68  
69      private final EventListenerList listenerList = new EventListenerList();
70  
71      public ClientModel() {
72      }
73  
74      public void addFolderListener(FolderListener listener) {
75          listenerList.add(FolderListener.class, listener);
76      }
77  
78      public void removeFolderListener(FolderListener listener) {
79          listenerList.remove(FolderListener.class, listener);
80      }
81  
82      public void addObjectListener(ObjectListener listener) {
83          listenerList.add(ObjectListener.class, listener);
84      }
85  
86      public void removeObjectListener(ObjectListener listener) {
87          listenerList.remove(ObjectListener.class, listener);
88      }
89  
90      public synchronized void setClientSession(ClientSession clientSession) {
91          this.clientSession = clientSession;
92      }
93  
94      public synchronized ClientSession getClientSession() {
95          return clientSession;
96      }
97  
98      public synchronized RepositoryInfo getRepositoryInfo() throws Exception {
99          Session session = clientSession.getSession();
100         return session.getRepositoryInfo();
101     }
102 
103     public synchronized String getRepositoryName() {
104         try {
105             return getRepositoryInfo().getName();
106         } catch (Exception e) {
107             return "?";
108         }
109     }
110 
111     public synchronized boolean supportsQuery() {
112         try {
113             RepositoryCapabilities cap = getRepositoryInfo().getCapabilities();
114             if (cap == null) {
115                 return true;
116             }
117 
118             return (cap.getQueryCapability() != null) && (cap.getQueryCapability() != CapabilityQuery.NONE);
119         } catch (Exception e) {
120             return false;
121         }
122     }
123 
124     public synchronized boolean supportsChangeLog() {
125         try {
126             RepositoryCapabilities cap = getRepositoryInfo().getCapabilities();
127             if (cap == null) {
128                 return true;
129             }
130 
131             return (cap.getChangesCapability() != null) && (cap.getChangesCapability() != CapabilityChanges.NONE);
132         } catch (Exception e) {
133             return false;
134         }
135     }
136 
137     public synchronized boolean supportsRelationships() {
138         try {
139             ObjectType relType = clientSession.getSession().getTypeDefinition(BaseTypeId.CMIS_RELATIONSHIP.value());
140             return relType != null;
141         } catch (Exception e) {
142             return false;
143         }
144     }
145 
146     public synchronized ObjectId loadFolder(String folderId, boolean byPath) throws Exception {
147         try {
148             Session session = clientSession.getSession();
149             CmisObject selectedObject = null;
150             CmisObject folderObject = null;
151 
152             if (byPath) {
153                 selectedObject = session.getObjectByPath(folderId);
154             } else {
155                 selectedObject = session.getObject(folderId);
156             }
157 
158             if (selectedObject instanceof Folder) {
159                 folderObject = selectedObject;
160             } else {
161                 if (selectedObject instanceof FileableCmisObject) {
162                     List<Folder> parents = ((FileableCmisObject) selectedObject).getParents();
163                     if (parents != null && parents.size() > 0) {
164                         folderObject = parents.get(0);
165                     } else {
166                         setCurrentFolder(null, new ArrayList<CmisObject>(0));
167                         return selectedObject;
168                     }
169                 } else {
170                     setCurrentFolder(null, new ArrayList<CmisObject>(0));
171                     return selectedObject;
172                 }
173             }
174 
175             List<CmisObject> children = new ArrayList<CmisObject>();
176             ItemIterable<CmisObject> iter = ((Folder) folderObject).getChildren(clientSession
177                     .getFolderOperationContext());
178             for (CmisObject child : iter) {
179                 children.add(child);
180             }
181 
182             setCurrentFolder((Folder) folderObject, children);
183 
184             return selectedObject;
185         } catch (Exception ex) {
186             setCurrentFolder(null, new ArrayList<CmisObject>(0));
187             throw ex;
188         }
189     }
190 
191     public synchronized void reloadFolder() throws Exception {
192         loadFolder(currentFolder.getId(), false);
193     }
194 
195     public synchronized void loadObject(String objectId) throws Exception {
196         try {
197             Session session = clientSession.getSession();
198             CmisObject object = session.getObject(objectId, clientSession.getObjectOperationContext());
199             object.refreshIfOld(OLD);
200 
201             setCurrentObject(object);
202         } catch (Exception ex) {
203             setCurrentObject(null);
204             throw ex;
205         }
206     }
207 
208     public synchronized void reloadObject() throws Exception {
209         if (currentObject == null) {
210             return;
211         }
212 
213         try {
214             Session session = clientSession.getSession();
215             CmisObject object = session.getObject(currentObject, clientSession.getObjectOperationContext());
216             object.refresh();
217 
218             setCurrentObject(object);
219         } catch (Exception ex) {
220             setCurrentObject(null);
221             throw ex;
222         }
223     }
224 
225     public synchronized ItemIterable<QueryResult> query(String q, boolean searchAllVersions, int maxHits)
226             throws Exception {
227         OperationContext queryContext = new OperationContextImpl(null, false, false, false, IncludeRelationships.NONE,
228                 null, false, null, false, maxHits);
229 
230         Session session = clientSession.getSession();
231         return session.query(q, searchAllVersions, queryContext);
232     }
233 
234     public synchronized List<Tree<ObjectType>> getTypeDescendants() throws Exception {
235         Session session = clientSession.getSession();
236         return session.getTypeDescendants(null, -1, true);
237     }
238 
239     public ContentStream createContentStream(String filename) throws Exception {
240         ContentStream content = null;
241         if ((filename != null) && (filename.length() > 0)) {
242             File file = new File(filename);
243             InputStream stream = new BufferedInputStream(new FileInputStream(file));
244 
245             content = clientSession.getSession().getObjectFactory()
246                     .createContentStream(file.getName(), file.length(), MimeTypes.getMIMEType(file), stream);
247         }
248 
249         return content;
250     }
251 
252     public synchronized ObjectId createDocument(String name, String type, String filename,
253             VersioningState versioningState, boolean unfiled) throws Exception {
254         Map<String, Object> properties = new HashMap<String, Object>();
255         properties.put(PropertyIds.NAME, name);
256         properties.put(PropertyIds.OBJECT_TYPE_ID, type);
257 
258         ContentStream content = createContentStream(filename);
259         return clientSession.getSession().createDocument(properties, (unfiled ? null : currentFolder), content,
260                 versioningState, null, null, null);
261     }
262 
263     public ContentStream createContentStream(String name, long length, long seed) throws Exception {
264         return clientSession.getSession().getObjectFactory()
265                 .createContentStream(name, length, "application/octet-stream", new RandomInputStream(length, seed));
266     }
267 
268     public synchronized ObjectId createDocument(String name, String type, long length, long seed,
269             VersioningState versioningState, boolean unfiled) throws Exception {
270         Map<String, Object> properties = new HashMap<String, Object>();
271         properties.put(PropertyIds.NAME, name);
272         properties.put(PropertyIds.OBJECT_TYPE_ID, type);
273 
274         ContentStream content = createContentStream(name, length, seed);
275         return clientSession.getSession().createDocument(properties, (unfiled ? null : currentFolder), content,
276                 versioningState, null, null, null);
277     }
278 
279     public synchronized ObjectId createFolder(String name, String type) throws Exception {
280         Map<String, Object> properties = new HashMap<String, Object>();
281         properties.put(PropertyIds.NAME, name);
282         properties.put(PropertyIds.OBJECT_TYPE_ID, type);
283 
284         return clientSession.getSession().createFolder(properties, currentFolder, null, null, null);
285     }
286 
287     public synchronized ObjectId createRelationship(String name, String type, String sourceId, String targetId)
288             throws Exception {
289         Map<String, Object> properties = new HashMap<String, Object>();
290         properties.put(PropertyIds.NAME, name);
291         properties.put(PropertyIds.OBJECT_TYPE_ID, type);
292         properties.put(PropertyIds.SOURCE_ID, sourceId);
293         properties.put(PropertyIds.TARGET_ID, targetId);
294 
295         return clientSession.getSession().createRelationship(properties, null, null, null);
296     }
297 
298     public synchronized List<ObjectType> getCreateableTypes(String rootTypeId) {
299         List<ObjectType> result = new ArrayList<ObjectType>();
300 
301         ObjectType rootType = null;
302         try {
303             rootType = clientSession.getSession().getTypeDefinition(rootTypeId);
304         } catch (CmisObjectNotFoundException e) {
305             return result;
306         }
307 
308         List<Tree<ObjectType>> types = clientSession.getSession().getTypeDescendants(rootTypeId, -1, false);
309         addType(types, result);
310 
311         boolean isCreatable = (rootType.isCreatable() == null ? true : rootType.isCreatable().booleanValue());
312         if (isCreatable) {
313             result.add(rootType);
314         }
315 
316         Collections.sort(result, new Comparator<ObjectType>() {
317             public int compare(ObjectType ot1, ObjectType ot2) {
318                 return ot1.getDisplayName().compareTo(ot2.getDisplayName());
319             }
320         });
321 
322         return result;
323     }
324 
325     private void addType(List<Tree<ObjectType>> types, List<ObjectType> resultList) {
326         for (Tree<ObjectType> tt : types) {
327             if (tt.getItem() != null) {
328                 boolean isCreatable = (tt.getItem().isCreatable() == null ? true : tt.getItem().isCreatable()
329                         .booleanValue());
330 
331                 if (isCreatable) {
332                     resultList.add(tt.getItem());
333                 }
334 
335                 addType(tt.getChildren(), resultList);
336             }
337         }
338     }
339 
340     public synchronized Folder getCurrentFolder() {
341         return currentFolder;
342     }
343 
344     public synchronized List<CmisObject> getCurrentChildren() {
345         return currentChildren;
346     }
347 
348     public synchronized CmisObject getFromCurrentChildren(String id) {
349         if ((currentChildren == null) || (currentChildren.isEmpty())) {
350             return null;
351         }
352 
353         for (CmisObject o : currentChildren) {
354             if (o.getId().equals(id)) {
355                 return o;
356             }
357         }
358 
359         return null;
360     }
361 
362     private synchronized void setCurrentFolder(Folder folder, List<CmisObject> children) {
363         currentFolder = folder;
364         currentChildren = children;
365 
366         for (FolderListener fl : listenerList.getListeners(FolderListener.class)) {
367             fl.folderLoaded(new ClientModelEvent(this));
368         }
369     }
370 
371     public synchronized CmisObject getCurrentObject() {
372         return currentObject;
373     }
374 
375     private synchronized void setCurrentObject(CmisObject object) {
376         currentObject = object;
377 
378         for (ObjectListener ol : listenerList.getListeners(ObjectListener.class)) {
379             ol.objectLoaded(new ClientModelEvent(this));
380         }
381     }
382 }