KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > action > EditMondrianXmlaSourceAction


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.action;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.webflow.action.FormAction;
29 import org.springframework.webflow.RequestContext;
30 import org.springframework.webflow.ScopeType;
31 import org.springframework.webflow.Event;
32 import org.springframework.webflow.AttributeMap;
33 import org.springframework.validation.DataBinder;
34 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
35
36 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource;
37 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup;
38 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
39 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection;
40 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianXMLADefinition;
41 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapClientConnection;
42 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService;
43 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria;
44 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
45 import com.jaspersoft.jasperserver.war.validation.MondrianXmlaSourceValidator;
46 import com.jaspersoft.jasperserver.war.dto.BaseDTO;
47 import com.jaspersoft.jasperserver.war.dto.MondrianXmlaSourceWrapper;
48
49 /**
50  * The EditMondrianXmlaSourceAction class provides action methods for
51  * the mondrianXmlaSourceFlow web flow
52  *
53  * @author jshih
54  */

55 public class EditMondrianXmlaSourceAction extends FormAction {
56     public final Log log = LogFactory.getLog(this.getClass());
57     private static final String JavaDoc FORM_OBJECT_KEY = "mondrianXmlaSource";
58     private static final String JavaDoc PARENT_FOLDER_ATTR = "parentFolder";
59     private static final String JavaDoc CURRENT_MONDRIAN_XMLA_DEFINITION_ATTR = "currentMondrianXmlaDefinition";
60     private static final String JavaDoc IS_EDIT = "isEdit";//FIXME use wrapper to disable name in UI
61
private RepositoryService repository;
62     private OlapConnectionService connectionService;
63
64     /**
65      * initialize EditMondrianXmlaSourceAction.class object
66      */

67     public EditMondrianXmlaSourceAction() {
68         setFormObjectClass(MondrianXmlaSourceWrapper.class);
69         setFormObjectName(FORM_OBJECT_KEY);
70         setFormObjectScope(ScopeType.FLOW);
71         setValidator(new MondrianXmlaSourceValidator());
72     }
73     
74     /**
75      * loadFormObject initializes form object
76      *
77      * @param context
78      * @return wrapper
79      */

80     public Object JavaDoc loadFormObject(RequestContext context) {
81         MondrianXMLADefinition mondrianXmlaDefinition;
82         MondrianXmlaSourceWrapper wrapper;
83         ExecutionContextImpl executionContext = new ExecutionContextImpl();
84         if (context.getFlowScope().get(IS_EDIT) != null) {
85             String JavaDoc currentMondrianXmlaDefinition = (String JavaDoc) context
86                     .getFlowScope().get(CURRENT_MONDRIAN_XMLA_DEFINITION_ATTR);
87             mondrianXmlaDefinition = (MondrianXMLADefinition) repository
88                     .getResource(executionContext,
89                             currentMondrianXmlaDefinition);
90             wrapper = new MondrianXmlaSourceWrapper(mondrianXmlaDefinition);
91             wrapper.setMode(BaseDTO.MODE_STAND_ALONE_EDIT);
92         } else {
93             mondrianXmlaDefinition = (MondrianXMLADefinition) repository
94                     .newResource(executionContext, MondrianXMLADefinition.class);
95             String JavaDoc parentFolder = (String JavaDoc) context.getFlowScope().get(
96                     PARENT_FOLDER_ATTR);
97             if (parentFolder == null || parentFolder.trim().length() == 0)
98                 parentFolder = "/";
99             mondrianXmlaDefinition.setParentFolder(parentFolder);
100             wrapper = new MondrianXmlaSourceWrapper(mondrianXmlaDefinition);
101             wrapper.setMode(BaseDTO.MODE_STAND_ALONE_NEW);
102         }
103         getAllMondrianConnections(wrapper);
104         return wrapper;
105     }
106     
107     /**
108      * getAllMondrianConnections retrieves all Mondrian connections
109      *
110      * @param wrapper
111      */

112     private void getAllMondrianConnections(MondrianXmlaSourceWrapper wrapper) {
113         FilterCriteria filterCriteria = FilterCriteria
114                 .createFilter(MondrianConnection.class);
115         ResourceLookup[] resourceLookup = repository.findResource(null,
116                 filterCriteria);
117         List JavaDoc allMondrianConnections = null;
118         if (resourceLookup != null && resourceLookup.length != 0) {
119             log("Found Mondrian conneciton lookups size="
120                     + resourceLookup.length);
121             allMondrianConnections = new ArrayList JavaDoc(resourceLookup.length);
122             for (int i = 0; i < resourceLookup.length; i++) {
123                 Resource resource = (Resource) resourceLookup[i];
124                 Object JavaDoc resourceObj = repository.getResource(null, resource
125                         .getURIString());
126                 if (!allMondrianConnections
127                         .contains(((OlapClientConnection) resourceObj)
128                                 .getURIString())) {
129                     allMondrianConnections
130                             .add(((OlapClientConnection) resourceObj)
131                                     .getURIString());
132                 }
133             }
134             wrapper.setAllMondrianConnections(allMondrianConnections);
135         }
136     }
137     
138     /**
139      * saveMondrianXmlaSource saves mondrian xmla source definitions.
140      *
141      * @param context
142      * @return success() if valid, otherwise error()
143      * @throws Exception
144      */

145     public Event saveMondrianXmlaSource(RequestContext context)
146             throws Exception JavaDoc {
147         MondrianXmlaSourceWrapper wrapper = (MondrianXmlaSourceWrapper) getFormObject(context);
148         try {
149             if (wrapper.isStandAloneMode()) {
150                 MondrianConnection mondrianConnection = (MondrianConnection) repository
151                         .getResource(null, wrapper.getConnectionUri());
152                 wrapper.getMondrianXmlaDefinition().setMondrianConnection(
153                         mondrianConnection);
154                 wrapper.getMondrianXmlaDefinition()
155                         .setMondrianConnectionReference(
156                                 mondrianConnection.getURIString());
157                 repository.saveResource(null, wrapper
158                         .getMondrianXmlaDefinition());
159                 wrapper.setConnectionInvalid(false);
160             }
161         } catch (Exception JavaDoc e) {
162             wrapper.setConnectionInvalid(true);
163         }
164         return success();
165     }
166
167     /**
168      * getRepository returns repository service property
169      *
170      * @return repository
171      */

172     public RepositoryService getRepository() {
173         return repository;
174     }
175
176     /**
177      * setRepository sets repository service property
178      *
179      * @param repository
180      */

181     public void setRepository(RepositoryService repository) {
182         this.repository = repository;
183     }
184
185     /**
186      * getConnectionService returns connection service
187      *
188      * @return connectionService
189      */

190     public OlapConnectionService getConnectionService() {
191         return connectionService;
192     }
193
194     /**
195      * setConnectionService sets connection service property
196      *
197      * @param connectionService
198      */

199     public void setConnectionService(OlapConnectionService connectionService) {
200         this.connectionService = connectionService;
201     }
202     
203     /**
204      * initBinder initializes binder object
205      *
206      * @param context
207      * @param binder
208      */

209     public void initBinder(RequestContext context, DataBinder binder) {
210         binder.registerCustomEditor(byte[].class,
211                 new ByteArrayMultipartFileEditor());
212     }
213     
214     /**
215      * setupEditForm set the form object
216      *
217      * @param context
218      * @return
219      * @throws Exception
220      */

221     public Event setupEditForm(RequestContext context) throws Exception JavaDoc {
222         AttributeMap rs = context.getRequestScope();
223         rs.put(FORM_OBJECT_KEY, getFormObject(context));
224         return success();
225     }
226     
227     /**
228      * log logs debug message
229      *
230      * @param text
231      */

232     private void log(String JavaDoc text) {
233         log.debug(text);
234     }
235 }
236
237
Popular Tags