working on struts/hibernate for content-admin
[mir.git] / source / mir / core / ui / action / admin / ContentAction.java
1 /*
2  * ContentAction.java created on 05.09.2003
3  * 
4  * Copyright (C) 2001, 2002, 2003 The Mir-coders group
5  *
6  * This file is part of Mir.
7  *
8  * Mir is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Mir is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Mir; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * In addition, as a special exception, The Mir-coders gives permission to link
23  * the code of this program with  any library licensed under the Apache Software License,
24  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
25  * (or with modified versions of the above that use the same license as the above),
26  * and distribute linked combinations including the two.  You must obey the
27  * GNU General Public License in all respects for all of the code used other than
28  * the above mentioned libraries.  If you modify this file, you may extend this
29  * exception to your version of the file, but you are not obligated to do so.
30  * If you do not wish to do so, delete this exception statement from your version.
31  */
32 package mir.core.ui.action.admin;
33
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.List;
37
38 import javax.servlet.ServletContext;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.servlet.http.HttpSession;
42
43 import mir.config.MirPropertiesConfiguration;
44 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
45 import mir.core.model.ArticleType;
46 import mir.core.model.Content;
47 import mir.core.model.IContent;
48 import mir.core.model.Language;
49 import mir.core.model.MirUser;
50 import mir.core.service.storage.ArticleTypeService;
51 import mir.core.service.storage.ContentService;
52 import mir.core.service.storage.LanguageService;
53 import mir.core.ui.action.DispatchAction;
54 import mir.core.ui.servlet.ServletConstants;
55 import multex.Failure;
56 import net.sf.hibernate.SessionFactory;
57 import net.sf.hibernate.expression.Expression;
58 import net.sf.hibernate.expression.Order;
59
60 import org.apache.struts.action.ActionForm;
61 import org.apache.struts.action.ActionForward;
62 import org.apache.struts.action.ActionMapping;
63 import org.apache.struts.action.DynaActionForm;
64
65 /**
66  * TopicAction
67  * @author idefix
68  * @version $Id: ContentAction.java,v 1.1 2003/12/20 20:27:09 idfx Exp $
69  */
70 public class ContentAction extends DispatchAction {
71         private MirPropertiesConfiguration _configuration;
72
73         public ContentAction(){
74                 super();
75                 try {
76                         _configuration = MirPropertiesConfiguration.instance();
77                 } catch (PropertiesConfigExc e) {
78                         throw new Failure("could not load config", e);
79                 }               
80         }
81
82         private ActionForward add(ActionMapping actionMapping, ActionForm actionForm, 
83                 HttpServletRequest request, HttpServletResponse response)
84                 throws Exception {
85                 //access to persistence
86                 ServletContext context = getServlet().getServletContext();
87                 ContentService contentService = 
88                         new ContentService((SessionFactory)context
89                                 .getAttribute(ServletConstants.SESSION_FACTORY));
90                                                         
91                 request.setAttribute(ServletConstants.NEW, new Boolean(true));
92                 request.setAttribute(ServletConstants.ID, "");
93                 request.setAttribute(ServletConstants.OFFSET, "");
94                 
95                 //setting standard-values
96                 Content content = new Content();
97                 request.setAttribute("article", content);
98                 
99                 //              show the view
100                 return actionMapping.findForward("success");
101         }
102         
103         private ActionForward save(ActionMapping actionMapping, ActionForm actionForm, 
104                 HttpServletRequest request, HttpServletResponse response)
105                 throws Exception {
106                 //access to persistence
107                 ServletContext context = getServlet().getServletContext();
108     HttpSession httpSession = request.getSession();
109                 ContentService contentService = 
110                         new ContentService((SessionFactory)context
111                                 .getAttribute(ServletConstants.SESSION_FACTORY));
112     ArticleTypeService articleTypeService = 
113       new ArticleTypeService((SessionFactory)context
114         .getAttribute(ServletConstants.SESSION_FACTORY));
115     LanguageService languageService = 
116       new LanguageService((SessionFactory)context
117         .getAttribute(ServletConstants.SESSION_FACTORY));
118                 
119     //retrieve the form
120                 DynaActionForm form = (DynaActionForm) actionForm;
121                 if(form == null){
122                         return actionMapping.findForward("failed");     
123                 }
124     
125                 //check if new
126                 Boolean isnew = (Boolean) form.get("new");
127                 
128                 //retrieve id
129                 IContent content = null;
130                 if(isnew.booleanValue()){
131                         content = new Content();
132       //set change and create date
133       content.setWebdbCreate(new Date());
134       content.setWebdbLastchange(new Date());
135                 } else {
136                         Integer id = new Integer(request.getParameter(ServletConstants.ID));
137                         content = (IContent) contentService.load(id);
138       //set last edit date
139       content.setWebdbLastchange(new Date());
140                 }
141                 
142                 if(content == null){
143                         return actionMapping.findForward("failed");     
144                 }
145
146                 String webDbCreate = (String) form.get("date");
147     if(webDbCreate == null || webDbCreate.length() == 0){
148       Date date = new Date();
149       SimpleDateFormat dateFormat = new SimpleDateFormat();
150       dateFormat.applyPattern("yyyyMMdd");
151       webDbCreate = dateFormat.format(date);
152     }
153     content.setDate(webDbCreate);
154     
155     //set articletype
156     Integer articleTypeId = (Integer) form.get("to_article_type");
157     content.setArticleType(
158       (ArticleType) articleTypeService.load(articleTypeId));
159     
160     //set language
161     Integer languageTypeId = (Integer) form.get("to_language");
162     content.setLanguage(
163       (Language) languageService.load(languageTypeId)); 
164     
165     //set form data
166     content.setPublisher(
167       (MirUser) httpSession.getAttribute(ServletConstants.USER));
168     content.setTitle((String) form.get("title"));
169     content.setSubtitle((String) form.get("subtitle"));
170                 content.setComment((String) form.get("comment"));
171                 content.setContentData((String) form.get("content_data"));
172                 content.setCreator((String) form.get("creator"));
173                 content.setCreatorAddress((String)form.get("creator_address"));
174                 content.setCreatorEmail((String)form.get("creator_email"));
175                 content.setCreatorPhone((String)form.get("creator_phone"));
176                 content.setCreatorMainUrl((String)form.get("creator_main_url"));
177                 content.setDescription((String)form.get("description"));
178                 content.setEdittitle((String)form.get("edittitle"));
179                 if(form.get("is_html") != null){
180                         content.setHtml(true);
181                 } else {
182                         content.setHtml(false);
183                 }
184                 if(form.get("is_published") != null){
185                         content.setPublished(true);
186                 } else {
187                         content.setPublished(false);
188                 }
189                                 
190 //              Integer parentId = (Integer)form.get("parentContent");
191 //              if(parentId != null && parentId.intValue() > -1){
192 //                      Topic parentTopic = (Topic) topicService.load(parentId);
193 //                      topic.setParentTopic(parentTopic);
194 //              } 
195     
196     //save the article
197                 if(isnew.booleanValue()){
198                         Integer id = contentService.save(content);
199                         request.setAttribute(ServletConstants.OFFSET, "0");
200                 } else {
201                         contentService.update(content);
202                 }
203                 
204                 //              show the view
205                 return actionMapping.findForward("success");
206         }
207                 
208         private ActionForward delete(ActionMapping actionMapping, ActionForm actionForm, 
209                 HttpServletRequest request, HttpServletResponse response)
210                 throws Exception {
211                 //access to persistence
212                 ServletContext context = getServlet().getServletContext();
213                 ContentService contentService = 
214                         new ContentService((SessionFactory)context
215                                 .getAttribute(ServletConstants.SESSION_FACTORY));
216                 
217                 //retrieve id
218                 Integer id = new Integer(request.getParameter(ServletConstants.ID));
219                 
220                 //confirm the request
221                 System.out.println(request.getAttribute(ServletConstants.DELETE));
222                 if(request.getAttribute(ServletConstants.DELETE) == null){
223                         return actionMapping.findForward("confirm");
224                 }
225                 //load object
226                 Content content = (Content) contentService.load(id);
227                 
228                 if(content == null){
229                         return actionMapping.findForward("failed");     
230                 }
231                                         
232                 contentService.delete(content);
233                 
234                 //              show the view
235                 return actionMapping.findForward("success");
236         }
237
238         private ActionForward list(ActionMapping actionMapping, ActionForm actionForm, 
239                 HttpServletRequest request, HttpServletResponse response)
240                 throws Exception {
241                 //retrieve parameters
242                 String offsetString = request.getParameter(ServletConstants.OFFSET);
243                 int offset = 0;
244                 if(offsetString != null && !offsetString.equals("")){
245                         offset = new Integer(offsetString).intValue();
246                 }
247     String articleTypeString = request.getParameter("articletype");
248     Integer articleTypeId = new Integer(-1);
249     if(articleTypeString != null && !articleTypeString.equals("")){
250       articleTypeId = new Integer(articleTypeString);
251     }   
252         
253                 //access to persistence
254                 ServletContext context = getServlet().getServletContext();
255                 ContentService contentService = 
256                         new ContentService((SessionFactory)context
257                                 .getAttribute(ServletConstants.SESSION_FACTORY));
258
259                 
260                 //retrieve entities
261                 Order order = Order.desc("id");
262     List contents;
263     if(articleTypeId.intValue() >= 0){
264       ArticleTypeService articleTypeService = 
265         new ArticleTypeService((SessionFactory)context
266           .getAttribute(ServletConstants.SESSION_FACTORY));
267       Expression expression = 
268         Expression.eq("articleType", 
269           articleTypeService.load(articleTypeId));
270       contents = contentService.list(offset, expression, order);
271     } else {
272       contents = contentService.list(offset, order);
273     }
274                  
275                 
276                 //configure the data to send to view
277                 int listSize = _configuration.getInt("ServletModule.Default.ListSize");
278                 Integer lastOffset;
279                 if(offset-listSize < 0){
280                         lastOffset = new Integer(0);
281                 } else {
282                         lastOffset = new Integer(offset-listSize);
283                 }
284                 request.setAttribute(ServletConstants.LAST_OFFSET, lastOffset);
285                 request.setAttribute(ServletConstants.NEXT_OFFSET, 
286                         new Integer(offset + listSize));
287                 request.setAttribute(ServletConstants.OFFSET, 
288                         new Integer(offset));
289                 request.setAttribute("articles", contents);
290                 
291                 //show the view
292                 return actionMapping.findForward("success");    
293         }
294         
295         private ActionForward edit(ActionMapping actionMapping, ActionForm actionForm, 
296                 HttpServletRequest request, HttpServletResponse response)
297                 throws Exception {
298                 //retrieve parameters
299                 Integer id = new Integer(request.getParameter(ServletConstants.ID));
300                 String offset = request.getParameter(ServletConstants.OFFSET);
301
302                 //access to persistence
303                 ServletContext context = getServlet().getServletContext();
304                 ContentService contentService = 
305                         new ContentService((SessionFactory)context
306                                 .getAttribute(ServletConstants.SESSION_FACTORY));
307                 
308                 //retrieve entities
309     IContent content = (IContent) contentService.load(id);
310                 
311                 //configure the data to send to view
312                 request.setAttribute(ServletConstants.OFFSET, offset);
313                 request.setAttribute(ServletConstants.NEW,"0");
314                 request.setAttribute("article", content);
315                                                 
316                 //show the view
317                 return actionMapping.findForward("success");    
318         }
319 }