KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > example > MixedExample


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.jcr.example;
18
19 import javax.jcr.Node;
20 import javax.jcr.Property;
21 import javax.jcr.Repository;
22 import javax.jcr.Session;
23 import javax.jcr.SimpleCredentials;
24
25 import org.alfresco.jcr.api.JCRNodeRef;
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.service.ServiceRegistry;
28 import org.alfresco.service.cmr.repository.NodeRef;
29 import org.alfresco.service.cmr.repository.NodeService;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.context.support.ClassPathXmlApplicationContext;
32
33
34
35 /**
36  * Example that demonstrate use of JCR and Alfresco API calls.
37  *
38  * @author David Caruana
39  */

40 public class MixedExample
41 {
42
43     public static void main(String JavaDoc[] args)
44         throws Exception JavaDoc
45     {
46         // Setup Spring and Transaction Service
47
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/jcr-context.xml");
48         ServiceRegistry registry = (ServiceRegistry)context.getBean(ServiceRegistry.SERVICE_REGISTRY);
49         NodeService nodeService = (NodeService)registry.getNodeService();
50         
51         // Retrieve Repository
52
Repository repository = (Repository)context.getBean("JCR.Repository");
53
54         // Login to workspace
55
// Note: Default workspace is the one used by Alfresco Web Client which contains all the Spaces
56
// and their documents
57
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
58
59         try
60         {
61             // Retrieve Company Home
62
Node root = session.getRootNode();
63             Node companyHome = root.getNode("app:company_home");
64
65             // Read Company Home Name
66
Property name = companyHome.getProperty("cm:name");
67             System.out.println("Name = " + name.getString());
68             
69             // Update Node via Alfresco Node Service API
70
NodeRef companyHomeRef = JCRNodeRef.getNodeRef(companyHome);
71             nodeService.setProperty(companyHomeRef, ContentModel.PROP_NAME, "Updated Company Home Name");
72             
73             // Re-read via JCR
74
System.out.println("Updated name = " + name.getString());
75         }
76         finally
77         {
78             session.logout();
79             System.exit(0);
80         }
81     }
82     
83 }
84
Popular Tags