KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > editor > EntityEditorImpl


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.editor;
25
26 import org.riotfamily.cachius.Cache;
27 import org.riotfamily.common.beans.PropertyUtils;
28 import org.riotfamily.riot.dao.RiotDao;
29 import org.riotfamily.riot.list.RiotDaoService;
30 import org.riotfamily.riot.security.AccessController;
31 import org.riotfamily.website.cache.CacheInvalidationUtils;
32 import org.springframework.beans.BeanWrapper;
33 import org.springframework.beans.BeanWrapperImpl;
34
35 /**
36  * @author Felix Gnass [fgnass at neteye dot de]
37  * @since 6.5
38  */

39 public class EntityEditorImpl implements EntityEditor {
40
41     private RiotDaoService daoService;
42     
43     private Cache cache;
44     
45     public EntityEditorImpl(RiotDaoService daoService) {
46         this.daoService = daoService;
47     }
48     
49     public void setCache(Cache cache) {
50         this.cache = cache;
51     }
52
53     public String JavaDoc createObject(String JavaDoc listId) {
54         RiotDao dao = daoService.getDao(listId);
55         BeanWrapper wrapper = new BeanWrapperImpl(dao.getEntityClass());
56         Object JavaDoc entity = wrapper.getWrappedInstance();
57         AccessController.checkPermission("edit", entity);
58         dao.save(entity, null);
59         CacheInvalidationUtils.invalidate(cache, dao);
60         return dao.getObjectId(entity);
61     }
62     
63     public void deleteObject(String JavaDoc listId, String JavaDoc objectId) {
64         RiotDao dao = daoService.getDao(listId);
65         Object JavaDoc entity = dao.load(objectId);
66         AccessController.checkPermission("delete", entity);
67         dao.delete(entity, null);
68         CacheInvalidationUtils.invalidate(cache, dao, entity);
69     }
70     
71     public String JavaDoc getText(String JavaDoc listId, String JavaDoc objectId, String JavaDoc property) {
72         RiotDao dao = daoService.getDao(listId);
73         Object JavaDoc entity = dao.load(objectId);
74         return PropertyUtils.getPropertyAsString(entity, property);
75     }
76     
77     public void updateText(String JavaDoc listId, String JavaDoc objectId, String JavaDoc property,
78             String JavaDoc value) {
79         
80         RiotDao dao = daoService.getDao(listId);
81         Object JavaDoc entity = dao.load(objectId);
82         AccessController.checkPermission("edit", entity);
83         BeanWrapper wrapper = new BeanWrapperImpl(entity);
84         wrapper.setPropertyValue(property, value);
85         dao.update(entity);
86         CacheInvalidationUtils.invalidate(cache, dao, entity);
87     }
88 }
89
Popular Tags