1 17 package org.alfresco.example; 18 19 import java.io.File ; 20 import java.io.Serializable ; 21 import java.util.HashMap ; 22 import java.util.Map ; 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 68 public class SimpleExampleWithContent 69 { 70 private static final String NAMESPACE = "http://www.alfresco.org/test/SimpleExampleWithContent"; 71 72 public static void main(String [] args) 73 { 74 ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); 76 final ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY); 78 79 TransactionService transactionService = serviceRegistry.getTransactionService(); 84 TransactionWork<Object > exampleWork = new TransactionWork<Object >() 85 { 86 public Object doWork() throws Exception 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 97 { 98 NodeService nodeService = serviceRegistry.getNodeService(); 100 ContentService contentService = serviceRegistry.getContentService(); 101 102 AuthenticationService authenticationService = serviceRegistry.getAuthenticationService(); 104 authenticationService.authenticate("admin", "admin".toCharArray()); 105 106 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 NodeRef rootNodeRef = nodeService.getRootNode(storeRef); 117 118 Map <QName, Serializable > nodeProperties = new HashMap <QName, Serializable >(7); 119 120 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 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 writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); 145 writer.setEncoding("UTF-8"); 147 148 String content = "The quick brown fox jumps over the lazy dog"; 153 writer.putContent(content); 154 155 File file = TempFileProvider.createTempFile("sample", ".txt"); 157 ContentReader reader = contentService.getReader(fileRef, ContentModel.PROP_CONTENT); 158 reader.getContent(file); 159 160 String dump = NodeStoreInspector.dumpNodeStore(nodeService, storeRef); 162 System.out.println("Node Store: \n" + dump); 163 164 } 166 } 167 | Popular Tags |