KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > browser > BrowseActionPagesAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.action.core.browser;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.Folder;
20 import com.blandware.atleap.common.util.FolderList;
21 import com.blandware.atleap.common.util.PartialCollection;
22 import com.blandware.atleap.common.util.QueryInfo;
23 import com.blandware.atleap.common.util.RegExUtil;
24 import com.blandware.atleap.model.core.ActionPage;
25 import com.blandware.atleap.service.core.PageManager;
26 import com.blandware.atleap.webapp.action.core.grid.BaseGridAction;
27 import com.blandware.atleap.webapp.taglib.core.grid.util.FilterConditions;
28 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
29 import com.blandware.atleap.webapp.taglib.core.grid.util.StringFilter;
30 import com.blandware.atleap.webapp.util.core.VirtualFolder;
31 import com.blandware.atleap.webapp.util.core.WebappConstants;
32 import com.blandware.atleap.webapp.util.core.WebappUtil;
33 import org.apache.commons.validator.GenericValidator;
34 import org.apache.oro.text.regex.MatchResult;
35 import org.apache.oro.text.regex.Pattern;
36 import org.apache.oro.text.regex.PatternCompiler;
37 import org.apache.oro.text.regex.PatternMatcher;
38 import org.apache.oro.text.regex.Perl5Compiler;
39 import org.apache.oro.text.regex.Perl5Matcher;
40 import org.apache.struts.Globals;
41 import org.apache.struts.action.ActionForm;
42 import org.apache.struts.action.ActionForward;
43 import org.apache.struts.action.ActionMapping;
44
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.LinkedList JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Locale JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.SortedSet JavaDoc;
55 import java.util.TreeSet JavaDoc;
56
57 /**
58  * <p>Returns browser page with allows to select an action page
59  * </p>
60  * <p><a HREF="CallCreateActionPageAction.java.htm"><i>View Source</i></a></p>
61  * <p/>
62  *
63  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
64  * @version $Revision: 1.25 $ $Date: 2006/03/10 17:10:16 $
65  * @struts.action path="/core/browser/browseActionPages"
66  * validate="false"
67  * roles="core-browser-actionPage"
68  * @struts.action-forward name="showBrowser"
69  * path=".core.browser.apBrowser"
70  */

71 public final class BrowseActionPagesAction extends BaseGridAction {
72
73     /**
74      * @param mapping The ActionMapping used to select this instance
75      * @param form The optional ActionForm bean for this request (if any)
76      * @param request The HTTP request we are proceeding
77      * @param response The HTTP response we are creating
78      * @return an ActionForward instance describing where and how
79      * control should be forwarded, or null if response
80      * has already been completed
81      */

82     public ActionForward execute(ActionMapping mapping, ActionForm form,
83                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
84
85         // check for locale suffix
86
String JavaDoc localeSuffix = null;
87         if ( !GenericValidator.isBlankOrNull(request.getParameter("editorAreaLanguage")) ) {
88             localeSuffix = request.getParameter("editorAreaLanguage");
89             request.setAttribute(WebappConstants.BROWSER_LOCALE_SUFFIX_KEY, localeSuffix);
90         } else {
91             localeSuffix = (String JavaDoc) request.getSession().getAttribute(WebappConstants.BROWSER_LOCALE_SUFFIX_KEY);
92         }
93
94
95         String JavaDoc currentFolder = "";
96         if ( request.getParameter("currentFolder") != null ) {
97             currentFolder = request.getParameter("currentFolder");
98             request.getSession().setAttribute(WebappConstants.ACTION_PAGES_CURRENT_PATH_KEY, currentFolder);
99         } else if ( request.getSession().getAttribute(WebappConstants.ACTION_PAGES_CURRENT_PATH_KEY) != null ) {
100             currentFolder = (String JavaDoc) request.getSession().getAttribute(WebappConstants.ACTION_PAGES_CURRENT_PATH_KEY);
101         }
102
103         if ( currentFolder == null || currentFolder.equals("/") ) {
104             currentFolder = "";
105         }
106
107         // ensure, that current folder starts with slash and does not end with slash
108
if ( currentFolder.length() > 0 ) {
109             if ( !currentFolder.startsWith("/") ) {
110                 currentFolder = "/" + currentFolder;
111             }
112             if ( currentFolder.endsWith("/") ) {
113                 currentFolder = currentFolder.substring(0, currentFolder.length() - 1);
114             }
115         }
116
117         // get all action pages, which exist in current folder
118

119         StringFilter uriFilter = new StringFilter("page.uri");
120         uriFilter.createFirstClause(FilterConditions.STRING_STARTS_WITH, currentFolder);
121
122         StringFilter linkableFilter = new StringFilter("page.active");
123         linkableFilter.createFirstClause(FilterConditions.STRING_EQUAL, "T");
124
125         Grid apGrid = getGridByName(WebappConstants.BROWSER_ACTION_PAGES_GRID, request.getSession());
126         if ( apGrid == null ) {
127             apGrid = new Grid(WebappConstants.BROWSER_ACTION_PAGES_GRID);
128         }
129
130         apGrid.addFilter(uriFilter);
131         apGrid.addFilter(linkableFilter);
132
133         saveGrid(apGrid, request.getSession());
134
135         QueryInfo queryInfo = new QueryInfo();
136         queryInfo.setWhereClause(apGrid.getWhereClause());
137
138         Map JavaDoc queryParameters = new HashMap JavaDoc();
139         Locale JavaDoc locale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
140         queryParameters.put("localeIdentifier", locale.getLanguage());
141         queryInfo.setQueryParameters(queryParameters);
142
143         PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
144         PartialCollection actionPages = pageManager.listActionPages(queryInfo);
145
146         // we have list of action pages which are all placed in current folder
147
// we only need to divide acquired set to subfolders and pages
148
// we need regular expression in order to do it:
149
String JavaDoc folderPatternString = "";
150         if ( currentFolder.length() > 0 ) {
151             folderPatternString = "(" + RegExUtil.escapeMetasymbols(currentFolder) + ")";
152         }
153         folderPatternString += "\\/([^\\/]+)(\\/[^\\/]+)+";
154
155         // pattern compiler instance to use
156
PatternCompiler compiler = new Perl5Compiler();
157
158         // compiled patterns
159
Pattern folderPattern = compiler.compile(folderPatternString);
160
161         // pattern matcher instance to use
162
PatternMatcher matcher = new Perl5Matcher();
163
164         // list of folders
165
SortedSet JavaDoc folderSet = new TreeSet JavaDoc();
166
167         // list of pages
168
List pages = new ArrayList JavaDoc();
169
170         for ( Iterator JavaDoc i = actionPages.iterator(); i.hasNext(); ) {
171             ActionPage actionPage = (ActionPage) i.next();
172             String JavaDoc uri = actionPage.getUri();
173
174             // check folder name
175
if ( matcher.matches(uri, folderPattern) ) {
176                 MatchResult result = matcher.getMatch();
177
178                 // according to pattern, if current folder is not an empty string, folder name is in second group
179
// otherwise it is in the first group;
180
String JavaDoc folderName = currentFolder.length() > 0 ? result.group(2) : result.group(1);
181                 Folder folder = new VirtualFolder(folderName, currentFolder, '/');
182                 folderSet.add(folder);
183
184             } else {
185                 // add page to list
186
ActionPage tmp = new ActionPage();
187                 WebappUtil.copyProperties(tmp, actionPage, request);
188                 String JavaDoc tmpUri = WebappUtil.getActionMappingURL(uri, null, request, WebappConstants.URL_TYPE_CONTEXT_RELATIVE, localeSuffix);
189                 if ( tmpUri.startsWith("/") ) {
190                     tmpUri = tmpUri.substring(1);
191                 }
192                 tmp.setUri(tmpUri);
193                 pages.add(tmp);
194             }
195         }
196
197         // create parent folder if current folder is not the root folder
198
Folder parentFolder = null;
199         if ( currentFolder.length() > 0 ) {
200             int k = currentFolder.lastIndexOf("/");
201             if ( k < 0 ) {
202                 k = 0;
203             }
204             String JavaDoc parentFolderPath = currentFolder.substring(0, k);
205             parentFolder = new VirtualFolder("..", parentFolderPath, '/');
206         }
207
208         LinkedList JavaDoc folders = new LinkedList JavaDoc(folderSet);
209
210         if ( parentFolder != null ) {
211             folders.addFirst(parentFolder);
212         }
213
214         String JavaDoc inputId = request.getParameter("inputId");
215         if ( inputId != null && inputId.length() != 0 ) {
216             request.getSession().setAttribute(WebappConstants.HTML_INPUT_TAG_ID_KEY, inputId);
217         }
218
219         request.setAttribute(WebappConstants.ACTION_PAGES_COLLECTION_KEY, pages);
220         request.setAttribute(WebappConstants.FOLDERS_COLLECTION_KEY, folders);
221
222         request.setAttribute(WebappConstants.CURRENT_PATH_KEY, new FolderList(VirtualFolder.splitPath(currentFolder)));
223
224
225         return mapping.findForward("showBrowser");
226     }
227 }
Popular Tags