KickJava   Java API By Example, From Geeks To Geeks.

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


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.repository.UpdateResult;
20 import org.alfresco.webservice.types.CML;
21 import org.alfresco.webservice.types.CMLCreate;
22 import org.alfresco.webservice.types.ContentFormat;
23 import org.alfresco.webservice.types.NamedValue;
24 import org.alfresco.webservice.types.ParentReference;
25 import org.alfresco.webservice.types.Predicate;
26 import org.alfresco.webservice.types.Reference;
27 import org.alfresco.webservice.types.Store;
28 import org.alfresco.webservice.types.StoreEnum;
29 import org.alfresco.webservice.util.Constants;
30 import org.alfresco.webservice.util.WebServiceFactory;
31
32 /**
33  * @author Roy Wetherall
34  */

35 public class WebServiceSampleBase
36 {
37     /** Admin user name and password used to connect to the repository */
38     protected static final String JavaDoc USERNAME = "admin";
39     protected static final String JavaDoc PASSWORD = "admin";
40     
41     /** The store used throughout the samples */
42     protected static final Store STORE = new Store(StoreEnum.workspace, "SpacesStore");
43     
44     protected static final Reference SAMPLE_FOLDER = new Reference(STORE, null, "/app:company_home/cm:sample_folder");
45     
46     protected static void createSampleData() throws Exception JavaDoc
47     {
48         try
49         {
50             // Check to see if the sample folder has already been created or not
51
WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[]{SAMPLE_FOLDER}, STORE, null));
52         }
53         catch (Exception JavaDoc exception)
54         {
55             // Create parent reference to company home
56
ParentReference parentReference = new ParentReference(
57                     STORE,
58                     null,
59                     "/app:company_home",
60                     Constants.ASSOC_CONTAINS,
61                     Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, "sample_folder"));
62
63             // Create folder
64
NamedValue[] properties = new NamedValue[]{new NamedValue(Constants.PROP_NAME, "Web Service Sample Folder")};
65             CMLCreate create = new CMLCreate("1", parentReference, Constants.TYPE_FOLDER, properties);
66             CML cml = new CML();
67             cml.setCreate(new CMLCreate[]{create});
68             UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);
69             
70             // Create parent reference to sample folder
71
Reference sampleFolder = results[0].getDestination();
72             ParentReference parentReference2 = new ParentReference(
73                     STORE,
74                     sampleFolder.getUuid(),
75                     null,
76                     Constants.ASSOC_CONTAINS,
77                     Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, "sample_content"));
78             
79             // Create content
80
NamedValue[] properties2 = new NamedValue[]{new NamedValue(Constants.PROP_NAME, "SampleContent.txt")};
81             CMLCreate create2 = new CMLCreate("1", parentReference2, Constants.TYPE_CONTENT, properties2);
82             CML cml2 = new CML();
83             cml2.setCreate(new CMLCreate[]{create2});
84             UpdateResult[] results2 = WebServiceFactory.getRepositoryService().update(cml2);
85             
86             // Set content
87
ContentFormat format = new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, "UTF-8");
88             byte[] content = "This is some test content provided by the Alfresco development team!".getBytes();
89             WebServiceFactory.getContentService().write(results2[0].getDestination(), Constants.PROP_CONTENT, content, format);
90             
91         }
92     }
93 }
94
Popular Tags