KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > sample > WebServiceSample5


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.webservice.sample;
18
19 import org.alfresco.webservice.authoring.AuthoringServiceSoapBindingStub;
20 import org.alfresco.webservice.authoring.CheckoutResult;
21 import org.alfresco.webservice.content.Content;
22 import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
23 import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
24 import org.alfresco.webservice.types.CML;
25 import org.alfresco.webservice.types.CMLAddAspect;
26 import org.alfresco.webservice.types.ContentFormat;
27 import org.alfresco.webservice.types.NamedValue;
28 import org.alfresco.webservice.types.Predicate;
29 import org.alfresco.webservice.types.Reference;
30 import org.alfresco.webservice.types.Store;
31 import org.alfresco.webservice.types.StoreEnum;
32 import org.alfresco.webservice.types.Version;
33 import org.alfresco.webservice.types.VersionHistory;
34 import org.alfresco.webservice.util.AuthenticationUtils;
35 import org.alfresco.webservice.util.Constants;
36 import org.alfresco.webservice.util.ContentUtils;
37 import org.alfresco.webservice.util.WebServiceFactory;
38
39 /**
40  * Web service sample 5
41  * <p>
42  * This sample shows how to check documents out, check them back in and then view the
43  * version history.
44  *
45  * @author Roy Wetherall
46  */

47 public class WebServiceSample5 extends WebServiceSampleBase
48 {
49     private final static String JavaDoc INITIAL_CONTENT = "This is the content pror to checkout";
50     private final static String JavaDoc UPDATED_CONTENT = "This is the updated content";
51     
52     /**
53      * Main function
54      */

55     public static void main(String JavaDoc[] args)
56         throws Exception JavaDoc
57     {
58         AuthenticationUtils.startSession(USERNAME, PASSWORD);
59         try
60         {
61             // Make sure smaple data has been created
62
createSampleData();
63             
64             // Get the content and authoring service
65
RepositoryServiceSoapBindingStub repositoryService = WebServiceFactory.getRepositoryService();
66             ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
67             AuthoringServiceSoapBindingStub authoringService = WebServiceFactory.getAuthoringService();
68             
69             // Get a reference to a newly created content
70
Reference contentReference = WebServiceSample3.createNewContent(contentService, "SampleFiveFileOne.txt", INITIAL_CONTENT);
71             
72             // Add the versionable aspect to the newly created content. This will allows the content to be versioned
73
makeVersionable(repositoryService, contentReference);
74             
75             // Checkout the newly created content, placing the working document in the same folder
76
Predicate itemsToCheckOut = new Predicate(new Reference[]{contentReference}, null, null);
77             CheckoutResult checkOutResult = authoringService.checkout(itemsToCheckOut, null);
78             
79             // Get a reference to the working copy
80
Reference workingCopyReference = checkOutResult.getWorkingCopies()[0];
81             
82             // Update the content of the working copy
83
ContentFormat format = new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, "UTF-8");
84             contentService.write(workingCopyReference, Constants.PROP_CONTENT, UPDATED_CONTENT.getBytes(), format);
85             
86             // Now check the working copy in with a description of the change made that will be recorded in the version history
87
Predicate predicate = new Predicate(new Reference[]{workingCopyReference}, null, null);
88             NamedValue[] comments = new NamedValue[]{new NamedValue("description", "The content has been updated")};
89             authoringService.checkin(predicate, comments, false);
90             
91             // Output the updated content
92
Store store = new Store(StoreEnum.workspace, "SpacesStore");
93             Content[] readResult = contentService.read(
94                             new Predicate(new Reference[]{contentReference}, store, null),
95                             Constants.PROP_CONTENT);
96             Content content = readResult[0];
97             System.out.println("This is the checked-in content:");
98             System.out.println(ContentUtils.getContentAsString(content));
99             
100             // Get the version history
101
System.out.println("The version history:");
102             VersionHistory versionHistory = authoringService.getVersionHistory(contentReference);
103             for (Version version : versionHistory.getVersions())
104             {
105                 // Output the version details
106
outputVersion(version);
107             }
108         }
109         finally
110         {
111             // End the session
112
AuthenticationUtils.endSession();
113         }
114     }
115     
116     /**
117      * Helper method to make apply the versionable aspect to a given reference
118      * <p>
119      * See sample 4 for more CML examples
120      *
121      * @param respositoryService the respository service
122      * @param reference the reference
123      * @throws Exception
124      */

125     public static void makeVersionable(RepositoryServiceSoapBindingStub respositoryService, Reference reference)
126         throws Exception JavaDoc
127     {
128         // Create the add aspect query object
129
Predicate predicate = new Predicate(new Reference[]{reference}, null, null);
130         CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_VERSIONABLE, null, predicate, null);
131         
132         // Create the content management language query
133
CML cml = new CML();
134         cml.setAddAspect(new CMLAddAspect[]{addAspect});
135         
136         // Execute the query, which will add the versionable aspect to the node is question
137
respositoryService.update(cml);
138     }
139     
140     /**
141      * Helper to output the version details
142      *
143      * @param version the version
144      */

145     private static void outputVersion(Version version)
146     {
147         String JavaDoc description = "none";
148         for (NamedValue namedValue : version.getCommentaries())
149         {
150             if (namedValue.getName().equals("description") == true)
151             {
152                 description = namedValue.getValue();
153             }
154         }
155         System.out.println("Version label = " + version.getLabel() + "; Version description = " + description);
156     }
157 }
158
Popular Tags