KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > contenttool > actions > UpdateContentVersionAttributeAction


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.applications.contenttool.actions;
25
26 import java.io.StringReader JavaDoc;
27
28 import org.apache.log4j.Logger;
29 import org.apache.xerces.parsers.DOMParser;
30 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController;
31 import org.infoglue.cms.entities.content.ContentVersionVO;
32 import org.infoglue.cms.util.ConstraintExceptionBuffer;
33 import org.infoglue.cms.util.XMLHelper;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38
39 /**
40   * This is the action-class for UpdateContentVersionVersion
41   *
42   * @author Mattias Bogeblad
43   */

44
45 public class UpdateContentVersionAttributeAction extends ViewContentVersionAction
46 {
47     private final static Logger logger = Logger.getLogger(UpdateContentVersionAttributeAction.class.getName());
48
49     private static final long serialVersionUID = 1L;
50     
51     private ContentVersionVO contentVersionVO;
52     private Integer JavaDoc contentId;
53     private Integer JavaDoc languageId;
54     private Integer JavaDoc contentVersionId;
55     private String JavaDoc attributeName;
56
57     private ConstraintExceptionBuffer ceb;
58         
59     public UpdateContentVersionAttributeAction()
60     {
61         this(new ContentVersionVO());
62     }
63     
64     public UpdateContentVersionAttributeAction(ContentVersionVO contentVersionVO)
65     {
66         this.contentVersionVO = contentVersionVO;
67         this.ceb = new ConstraintExceptionBuffer();
68     }
69     
70     public String JavaDoc doExecute() throws Exception JavaDoc
71     {
72         super.initialize(this.contentVersionId, this.contentId, this.languageId);
73
74         this.contentVersionVO = this.getContentVersionVO();
75
76         String JavaDoc attributeValue = getRequest().getParameter(this.attributeName);
77         if(attributeValue != null)
78         {
79             setAttributeValue(this.contentVersionVO, this.attributeName, attributeValue);
80             ceb.throwIfNotEmpty();
81             
82             this.contentVersionVO.setVersionModifier(this.getInfoGluePrincipal().getName());
83             ContentVersionController.getContentVersionController().update(this.contentId, this.languageId, this.contentVersionVO);
84         }
85         
86         return "success";
87     }
88
89
90     /**
91      * This method sets a value to the xml that is the contentVersions Value.
92      */

93      
94     private void setAttributeValue(ContentVersionVO contentVersionVO, String JavaDoc attributeName, String JavaDoc attributeValue)
95     {
96         String JavaDoc value = "";
97         if(this.contentVersionVO != null)
98         {
99             try
100             {
101                 logger.info("VersionValue:" + this.contentVersionVO.getVersionValue());
102                 InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(this.contentVersionVO.getVersionValue()));
103                 
104                 DOMParser parser = new DOMParser();
105                 parser.parse(inputSource);
106                 Document JavaDoc document = parser.getDocument();
107                 
108                 NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
109                 Node JavaDoc n = nl.item(0);
110                 
111                 nl = n.getChildNodes();
112                 for(int i=0; i<nl.getLength(); i++)
113                 {
114                     n = nl.item(i);
115                     if(n.getNodeName().equalsIgnoreCase(attributeName))
116                     {
117                         logger.info("Setting attributeValue: " + attributeValue);
118                         Node JavaDoc valueNode = n.getFirstChild();
119                         n.getFirstChild().setNodeValue(attributeValue);
120                         break;
121                     }
122                 }
123                 contentVersionVO.setVersionValue(XMLHelper.serializeDom(document, new StringBuffer JavaDoc()).toString());
124             }
125             catch(Exception JavaDoc e)
126             {
127                 e.printStackTrace();
128             }
129         }
130     }
131     
132
133     public void setContentVersionId(Integer JavaDoc contentVersionId)
134     {
135         this.contentVersionVO.setContentVersionId(contentVersionId);
136     }
137
138     public java.lang.Integer JavaDoc getContentVersionId()
139     {
140         return this.contentVersionVO.getContentVersionId();
141     }
142
143     public void setStateId(Integer JavaDoc stateId)
144     {
145         this.contentVersionVO.setStateId(stateId);
146     }
147
148     public java.lang.Integer JavaDoc getStateId()
149     {
150         return this.contentVersionVO.getStateId();
151     }
152
153     public void setContentId(Integer JavaDoc contentId)
154     {
155         this.contentId = contentId;
156     }
157
158     public java.lang.Integer JavaDoc getContentId()
159     {
160         return this.contentId;
161     }
162
163     public void setLanguageId(Integer JavaDoc languageId)
164     {
165         this.languageId = languageId;
166     }
167
168     public java.lang.Integer JavaDoc getLanguageId()
169     {
170         return this.languageId;
171     }
172         
173     public java.lang.String JavaDoc getVersionValue()
174     {
175         return this.contentVersionVO.getVersionValue();
176     }
177         
178     public void setVersionValue(java.lang.String JavaDoc versionValue)
179     {
180         this.contentVersionVO.setVersionValue(versionValue);
181     }
182     
183     public String JavaDoc getAttributeName()
184     {
185         return attributeName;
186     }
187
188     public void setAttributeName(String JavaDoc attributeName)
189     {
190         this.attributeName = attributeName;
191     }
192
193 }
194
Popular Tags