This project has retired. For details please refer to its Attic page.
DataObjectCreator 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.inmemory;
20  
21  import java.math.BigInteger;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.chemistry.opencmis.commons.data.Ace;
27  import org.apache.chemistry.opencmis.commons.data.Acl;
28  import org.apache.chemistry.opencmis.commons.data.AllowableActions;
29  import org.apache.chemistry.opencmis.commons.data.ChangeEventInfo;
30  import org.apache.chemistry.opencmis.commons.data.ObjectData;
31  import org.apache.chemistry.opencmis.commons.data.ObjectList;
32  import org.apache.chemistry.opencmis.commons.data.PolicyIdList;
33  import org.apache.chemistry.opencmis.commons.data.RenditionData;
34  import org.apache.chemistry.opencmis.commons.enums.Action;
35  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
36  import org.apache.chemistry.opencmis.commons.enums.RelationshipDirection;
37  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlListImpl;
38  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AllowableActionsImpl;
39  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ChangeEventInfoDataImpl;
40  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PolicyIdListImpl;
41  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Content;
42  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Filing;
43  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Folder;
44  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
45  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Version;
46  import org.apache.chemistry.opencmis.inmemory.storedobj.api.VersionedDocument;
47  
48  /**
49   * @author Jens A collection of utility functions to fill the data objects used
50   *         as return values for the service object calls
51   */
52  public class DataObjectCreator {
53  
54  	 public static BigInteger MINUS_ONE = BigInteger.valueOf(-1L);
55  
56      // Utility class
57      private DataObjectCreator() {
58      }
59  
60      public static AllowableActions fillAllowableActions(StoredObject so, String user) {
61  
62          boolean isFolder = so instanceof Folder;
63          boolean isDocument = so instanceof Content;
64          boolean isCheckedOut = false;
65          boolean canCheckOut = false;
66          boolean canCheckIn = false;
67          boolean isVersioned = so instanceof Version || so instanceof VersionedDocument;
68          boolean hasContent = so instanceof Content && ((Content) so).hasContent();
69          boolean isRootFolder = isFolder && ((Folder)so).getParent() == null;
70          
71          if (so instanceof Version) {
72              isCheckedOut = ((Version) so).isPwc();
73              canCheckIn = isCheckedOut && ((Version) so).getParentDocument().getCheckedOutBy().equals(user);
74              canCheckOut = !((Version) so).getParentDocument().isCheckedOut();
75          } else if (so instanceof VersionedDocument) {
76              isCheckedOut = ((VersionedDocument) so).isCheckedOut();
77              canCheckOut = !((VersionedDocument) so).isCheckedOut();
78              canCheckIn = isCheckedOut && ((VersionedDocument) so).getCheckedOutBy().equals(user);
79          }
80          
81          AllowableActionsImpl allowableActions = new AllowableActionsImpl();
82          Set<Action> set = allowableActions.getAllowableActions();
83  
84          if (!isRootFolder) {
85              set.add(Action.CAN_DELETE_OBJECT);
86              set.add(Action.CAN_UPDATE_PROPERTIES);
87          }
88  
89          if (isFolder || isDocument) {
90              set.add(Action.CAN_GET_PROPERTIES);
91              if (!isRootFolder) {
92                  set.add(Action.CAN_GET_OBJECT_PARENTS);
93              }
94              set.add(Action.CAN_MOVE_OBJECT);
95          }
96  
97          if (isFolder) {
98              if (!isRootFolder) {
99                  set.add(Action.CAN_GET_FOLDER_PARENT);
100                 set.add(Action.CAN_DELETE_TREE);
101             }
102             set.add(Action.CAN_GET_FOLDER_TREE);
103             set.add(Action.CAN_GET_DESCENDANTS);
104 
105             set.add(Action.CAN_CREATE_DOCUMENT);
106             set.add(Action.CAN_CREATE_FOLDER);
107             set.add(Action.CAN_GET_CHILDREN);
108         }
109 
110         if (hasContent) {
111             set.add(Action.CAN_DELETE_CONTENT_STREAM);
112             set.add(Action.CAN_GET_CONTENT_STREAM);
113         }
114 
115         if (isVersioned) {
116             if (canCheckOut) {
117                 set.add(Action.CAN_CHECK_OUT);
118             }
119             if (isCheckedOut) {
120                 set.add(Action.CAN_CANCEL_CHECK_OUT);
121             }
122             if (canCheckIn) {
123                 set.add(Action.CAN_CHECK_IN);
124             }
125             set.add(Action.CAN_GET_ALL_VERSIONS);
126         }
127 
128         if (isDocument) {
129             if (so instanceof Filing && ((Filing)so).hasParent()) {
130                 set.add(Action.CAN_ADD_OBJECT_TO_FOLDER);
131                 set.add(Action.CAN_REMOVE_OBJECT_FROM_FOLDER);
132             }
133             if (isVersioned) {
134                 if (canCheckIn)
135                     set.add(Action.CAN_SET_CONTENT_STREAM);
136             } else
137                 set.add(Action.CAN_SET_CONTENT_STREAM);
138         }
139 
140         allowableActions.setAllowableActions(set);
141         return allowableActions;
142     }
143 
144     public static Acl fillACL(StoredObject so) {
145         AccessControlListImpl acl = new AccessControlListImpl();
146         List<Ace> aces = new ArrayList<Ace>();
147         // TODO to be completed if ACLs are implemented
148         acl.setAces(aces);
149         return acl;
150     }
151 
152     public static PolicyIdList fillPolicyIds(StoredObject so) {
153         // TODO: to be completed if policies are implemented
154         PolicyIdListImpl polIds = new PolicyIdListImpl();
155         // polIds.setPolicyIds(...);
156         return polIds;
157     }
158 
159     public static List<ObjectData> fillRelationships(IncludeRelationships includeRelationships, StoredObject so, String user) {
160         return getRelationships(includeRelationships, so, user);
161     }
162 
163     public static List<RenditionData> fillRenditions(StoredObject so) {
164         // TODO: to be completed if renditions are implemented
165         List<RenditionData> renditions = new ArrayList<RenditionData>();
166         return renditions;
167     }
168 
169     public static ChangeEventInfo fillChangeEventInfo(StoredObject so) {
170         // TODO: to be completed if change information is implemented
171         ChangeEventInfo changeEventInfo = new ChangeEventInfoDataImpl();
172         return changeEventInfo;
173     }
174     
175     public static List<ObjectData> getRelationships(IncludeRelationships includeRelationships,
176     		StoredObject spo, String user)
177     {
178          if (includeRelationships != IncludeRelationships.NONE) 
179         {
180         	RelationshipDirection relationshipDirection = RelationshipDirection.SOURCE;
181         	// source is default
182         	if (includeRelationships == IncludeRelationships.TARGET)
183         		relationshipDirection = RelationshipDirection.TARGET;
184         	else if (includeRelationships == IncludeRelationships.BOTH)
185         		relationshipDirection = RelationshipDirection.EITHER;  // either and both!!
186         	
187             ObjectList relationships = spo.getObjectRelationships(false, relationshipDirection,
188             		null, null, false, MINUS_ONE, MINUS_ONE, null, user);
189            return (relationships == null? null : relationships.getObjects());
190         }
191          return null;
192     }
193 }