KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > example > SimpleExampleWithContent


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.example;
18
19 import java.io.File JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.content.MimetypeMap;
26 import org.alfresco.repo.transaction.TransactionUtil;
27 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
28 import org.alfresco.service.ServiceRegistry;
29 import org.alfresco.service.cmr.repository.ChildAssociationRef;
30 import org.alfresco.service.cmr.repository.ContentReader;
31 import org.alfresco.service.cmr.repository.ContentService;
32 import org.alfresco.service.cmr.repository.ContentWriter;
33 import org.alfresco.service.cmr.repository.NodeRef;
34 import org.alfresco.service.cmr.repository.NodeService;
35 import org.alfresco.service.cmr.repository.StoreRef;
36 import org.alfresco.service.cmr.security.AuthenticationService;
37 import org.alfresco.service.namespace.QName;
38 import org.alfresco.service.transaction.TransactionService;
39 import org.alfresco.util.ApplicationContextHelper;
40 import org.alfresco.util.GUID;
41 import org.alfresco.util.TempFileProvider;
42 import org.alfresco.util.debug.NodeStoreInspector;
43 import org.springframework.context.ApplicationContext;
44
45 /**
46  * A quick example of how to
47  * <ul>
48  * <li>get hold of the repository service</li>
49  * <li>initialise a model</li>
50  * <li>create nodes</li>
51  * <li>load in some content</li>
52  * </ul>
53  * <p>
54  * <i>
55  * All the normal checks for missing resources and so forth have been left out in the interests
56  * of clarity of demonstration.
57  * </i>
58  * <p>
59  * To change the model being used, make changes to the <b>dictionaryDAO</b> bean in the
60  * application contenxt XML file. For now, this example is written against the
61  * generic <code>alfresco/model/contentModel.xml</code>.
62  * <p>
63  * The content store location can also be set in the application context.
64  *
65  *
66  * @author Derek Hulley
67  */

68 public class SimpleExampleWithContent
69 {
70     private static final String JavaDoc NAMESPACE = "http://www.alfresco.org/test/SimpleExampleWithContent";
71     
72     public static void main(String JavaDoc[] args)
73     {
74         // initialise app content
75
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
76         // get registry of services
77
final ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
78         
79         // begin a UserTransaction
80
// All the services are set to create or propogate the transaction.
81
// This transaction will be recognised and propogated
82
// The TransactionUtil takes care of the catching and rollback, etc
83
TransactionService transactionService = serviceRegistry.getTransactionService();
84         TransactionWork<Object JavaDoc> exampleWork = new TransactionWork<Object JavaDoc>()
85         {
86             public Object JavaDoc doWork() throws Exception JavaDoc
87             {
88                 doExample(serviceRegistry);
89                 return null;
90             }
91         };
92         TransactionUtil.executeInUserTransaction(transactionService, exampleWork);
93         System.exit(0);
94     }
95
96     private static void doExample(ServiceRegistry serviceRegistry) throws Exception JavaDoc
97     {
98         // get individual, required services
99
NodeService nodeService = serviceRegistry.getNodeService();
100         ContentService contentService = serviceRegistry.getContentService();
101
102         // authenticate
103
AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
104         authenticationService.authenticate("admin", "admin".toCharArray());
105         
106         // create a store, if one doesn't exist
107
StoreRef storeRef = new StoreRef(
108                 StoreRef.PROTOCOL_WORKSPACE,
109                 "SimpleExampleWithContent-" + GUID.generate());
110         if (!nodeService.exists(storeRef))
111         {
112             nodeService.createStore(storeRef.getProtocol(), storeRef.getIdentifier());
113         }
114         
115         // get the root node from which to hang the next level of nodes
116
NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
117         
118         Map JavaDoc<QName, Serializable JavaDoc> nodeProperties = new HashMap JavaDoc<QName, Serializable JavaDoc>(7);
119         
120         // add a simple folder to the root node
121
nodeProperties.clear();
122         nodeProperties.put(ContentModel.PROP_NAME, "My First Folder");
123         ChildAssociationRef assocRef = nodeService.createNode(
124                 rootNodeRef,
125                 ContentModel.ASSOC_CHILDREN,
126                 QName.createQName(NAMESPACE, QName.createValidLocalName("My First Folder")),
127                 ContentModel.TYPE_FOLDER,
128                 nodeProperties);
129         NodeRef folderRef = assocRef.getChildRef();
130         
131         // create a file
132
nodeProperties.clear();
133         nodeProperties.put(ContentModel.PROP_NAME, "My First File");
134         assocRef = nodeService.createNode(
135                 folderRef,
136                 ContentModel.ASSOC_CONTAINS,
137                 QName.createQName(NAMESPACE, QName.createValidLocalName("My First File")),
138                 ContentModel.TYPE_CONTENT,
139                 nodeProperties);
140         NodeRef fileRef = assocRef.getChildRef();
141         
142         ContentWriter writer = contentService.getWriter(fileRef, ContentModel.PROP_CONTENT, true);
143         // the mimetype will up pushed onto the node automatically once the stream closes
144
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
145         // store string content as UTF-8
146
writer.setEncoding("UTF-8");
147         
148         // write some content - this API allows streaming and direct loading,
149
// but for now we'll just upload a string
150
// The writer, being updating, will take care of updating the node once the stream
151
// closes.
152
String JavaDoc content = "The quick brown fox jumps over the lazy dog";
153         writer.putContent(content);
154         
155         // dump the content to a file
156
File JavaDoc file = TempFileProvider.createTempFile("sample", ".txt");
157         ContentReader reader = contentService.getReader(fileRef, ContentModel.PROP_CONTENT);
158         reader.getContent(file);
159         
160         // just to demonstrate the node structure, dump it to the file
161
String JavaDoc dump = NodeStoreInspector.dumpNodeStore(nodeService, storeRef);
162         System.out.println("Node Store: \n" + dump);
163         
164         // and much, much more ...
165
}
166 }
167
Popular Tags