KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > actionPage > ListActionPagesAction


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.actionPage;
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.oro.text.regex.MatchResult;
34 import org.apache.oro.text.regex.Pattern;
35 import org.apache.oro.text.regex.PatternCompiler;
36 import org.apache.oro.text.regex.PatternMatcher;
37 import org.apache.oro.text.regex.Perl5Compiler;
38 import org.apache.oro.text.regex.Perl5Matcher;
39 import org.apache.struts.Globals;
40 import org.apache.struts.action.ActionForm;
41 import org.apache.struts.action.ActionForward;
42 import org.apache.struts.action.ActionMapping;
43
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.LinkedList JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Locale JavaDoc;
52 import java.util.Map JavaDoc;
53 import java.util.SortedSet JavaDoc;
54 import java.util.TreeSet JavaDoc;
55
56 /**
57  * <p>Forward to the page with list of action pages
58  * </p>
59  * <p><a HREF="ListActionPagesAction.java.htm"><i>View Source</i></a></p>
60  * <p/>
61  *
62  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
63  * @version $Revision: 1.26 $ $Date: 2006/03/26 11:47:32 $
64  * @struts.action path="/core/actionPage/list"
65  * name="actionPageForm"
66  * validate="false"
67  * roles="core-actionPage-list"
68  * @struts.action-forward name="listActionPages"
69  * path=".core.actionPage.list"
70  */

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

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

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