KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > webservices > RemoteUserPropertiesServiceImpl


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.cms.webservices;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.dom4j.Document;
36 import org.dom4j.Element;
37 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController;
38 import org.infoglue.cms.controllers.kernel.impl.simple.ContentControllerProxy;
39 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController;
40 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionControllerProxy;
41 import org.infoglue.cms.controllers.kernel.impl.simple.DigitalAssetController;
42 import org.infoglue.cms.controllers.kernel.impl.simple.UserControllerProxy;
43 import org.infoglue.cms.controllers.kernel.impl.simple.UserPropertiesController;
44 import org.infoglue.cms.entities.content.ContentVO;
45 import org.infoglue.cms.entities.content.ContentVersionVO;
46 import org.infoglue.cms.entities.content.DigitalAssetVO;
47 import org.infoglue.cms.entities.management.UserPropertiesVO;
48 import org.infoglue.cms.exception.SystemException;
49 import org.infoglue.cms.security.InfoGluePrincipal;
50 import org.infoglue.cms.util.dom.DOMBuilder;
51 import org.infoglue.cms.webservices.elements.RemoteAttachment;
52 import org.infoglue.deliver.util.webservices.DynamicWebserviceSerializer;
53
54
55 /**
56  * This class is responsible for letting an external application call InfoGlue
57  * API:s remotely. It handles api:s to manage user properties.
58  *
59  * @author Mattias Bogeblad
60  */

61
62 public class RemoteUserPropertiesServiceImpl extends RemoteInfoGlueService
63 {
64     private final static Logger logger = Logger.getLogger(RemoteUserPropertiesServiceImpl.class.getName());
65
66     /**
67      * The principal executing the workflow.
68      */

69     private InfoGluePrincipal principal;
70
71     private static UserPropertiesController userPropertiesController = UserPropertiesController.getController();
72     
73     
74     /**
75      * Inserts a new UserProperty.
76      */

77     
78     public int updateUserProperties(final String JavaDoc principalName, UserPropertiesVO userPropertiesVO)
79     {
80         int newUserPropertiesId = 0;
81         
82         logger.info("***********************************************");
83         logger.info("Creating user properties through webservice....");
84         logger.info("***********************************************");
85         
86         try
87         {
88             initializePrincipal(principalName);
89             UserPropertiesVO newUserPropertiesVO = userPropertiesController.update(userPropertiesVO.getLanguageId(), userPropertiesVO.getContentTypeDefinitionId(), userPropertiesVO);
90             newUserPropertiesId = newUserPropertiesVO.getId().intValue();
91         }
92         catch(Exception JavaDoc e)
93         {
94             logger.error("En error occurred when we tried to create a new userProperty:" + e.getMessage(), e);
95         }
96         
97         updateCaches();
98
99         return newUserPropertiesId;
100     }
101
102     /**
103      * Inserts a new UserProperty.
104      */

105     
106     public Boolean JavaDoc updateUserProperties(final String JavaDoc principalName, int languageId, int contentTypeDefinitionId, final Object JavaDoc[] inputsArray)
107     {
108         int newUserPropertiesId = 0;
109         
110         logger.info("***********************************************");
111         logger.info("Creating user properties through webservice....");
112         logger.info("***********************************************");
113         
114         try
115         {
116             final DynamicWebserviceSerializer serializer = new DynamicWebserviceSerializer();
117             Map JavaDoc userPropertiesAttributesMap = (Map JavaDoc)serializer.deserialize(inputsArray);
118             
119             initializePrincipal(principalName);
120             logger.info("principalName:" + principalName);
121             logger.info("principal:" + principal);
122             logger.info("languageId:" + languageId);
123             logger.info("contentTypeDefinitionId:" + contentTypeDefinitionId);
124             
125             UserPropertiesVO userPropertiesVO = new UserPropertiesVO();
126             userPropertiesVO.setUserName(principal.getName());
127             
128             logger.info("userPropertiesAttributesMap:" + userPropertiesAttributesMap.size());
129             
130             DOMBuilder domBuilder = new DOMBuilder();
131             Document document = domBuilder.createDocument();
132             
133             Element rootElement = domBuilder.addElement(document, "article");
134             Element attributesRoot = domBuilder.addElement(rootElement, "attributes");
135             
136             Iterator JavaDoc attributesIterator = userPropertiesAttributesMap.keySet().iterator();
137             while(attributesIterator.hasNext())
138             {
139                 String JavaDoc attributeName = (String JavaDoc)attributesIterator.next();
140                 String JavaDoc attributeValue = (String JavaDoc)userPropertiesAttributesMap.get(attributeName);
141                 
142                 logger.info(attributeName + "=" + attributeValue);
143                 
144                 Element attribute = domBuilder.addElement(attributesRoot, attributeName);
145                 domBuilder.addCDATAElement(attribute, attributeValue);
146             }
147
148             userPropertiesVO.setValue(document.asXML());
149
150             
151             UserPropertiesVO newUserPropertiesVO = userPropertiesController.update(new Integer JavaDoc(languageId), new Integer JavaDoc(contentTypeDefinitionId), userPropertiesVO);
152             newUserPropertiesId = newUserPropertiesVO.getId().intValue();
153         }
154         catch(Throwable JavaDoc e)
155         {
156             logger.error("En error occurred when we tried to create a new userProperty:" + e.getMessage(), e);
157         }
158         
159         updateCaches();
160
161         return new Boolean JavaDoc(true);
162     }
163
164     
165     /**
166      * Deletes a digital asset.
167      */

168     
169     public Boolean JavaDoc deleteDigitalAsset(final String JavaDoc principalName, final Object JavaDoc[] inputsArray)
170     {
171         List JavaDoc newContentIdList = new ArrayList JavaDoc();
172         
173         logger.info("****************************************");
174         logger.info("Updating content through webservice....");
175         logger.info("****************************************");
176         
177         logger.info("principalName:" + principalName);
178         logger.info("inputsArray:" + inputsArray);
179         //logger.warn("contents:" + contents);
180

181         try
182         {
183             final DynamicWebserviceSerializer serializer = new DynamicWebserviceSerializer();
184             Map JavaDoc digitalAsset = (Map JavaDoc) serializer.deserialize(inputsArray);
185             logger.info("digitalAsset:" + digitalAsset);
186
187             initializePrincipal(principalName);
188             
189             Integer JavaDoc contentVersionId = (Integer JavaDoc)digitalAsset.get("contentVersionId");
190             Integer JavaDoc contentId = (Integer JavaDoc)digitalAsset.get("contentId");
191             Integer JavaDoc languageId = (Integer JavaDoc)digitalAsset.get("languageId");
192             String JavaDoc assetKey = (String JavaDoc)digitalAsset.get("assetKey");
193             
194             logger.info("contentVersionId:" + contentVersionId);
195             logger.info("contentId:" + contentId);
196             logger.info("languageId:" + languageId);
197             logger.info("assetKey:" + assetKey);
198             
199             ContentVersionController.getContentVersionController().deleteDigitalAsset(contentId, languageId, assetKey);
200                
201             logger.info("Done with contents..");
202
203         }
204         catch(Throwable JavaDoc e)
205         {
206             logger.error("En error occurred when we tried to delete a digitalAsset:" + e.getMessage(), e);
207         }
208         
209         updateCaches();
210
211         return new Boolean JavaDoc(true);
212     }
213
214
215     
216     /**
217      * Checks if the principal exists and if the principal is allowed to create the workflow.
218      *
219      * @param userName the name of the user.
220      * @param workflowName the name of the workflow to create.
221      * @throws SystemException if the principal doesn't exists or doesn't have permission to create the workflow.
222      */

223     private void initializePrincipal(final String JavaDoc userName) throws SystemException
224     {
225         try
226         {
227             principal = UserControllerProxy.getController().getUser(userName);
228         }
229         catch(SystemException e)
230         {
231             throw e;
232         }
233         catch(Exception JavaDoc e)
234         {
235             throw new SystemException(e);
236         }
237         if(principal == null)
238         {
239             throw new SystemException("No such principal [" + userName + "].");
240         }
241     }
242
243
244 }
245
Popular Tags