KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Folder;
19 import com.blandware.atleap.common.util.FolderList;
20 import com.blandware.atleap.common.util.RegExUtil;
21 import com.blandware.atleap.webapp.action.core.grid.BaseGridAction;
22 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
23 import com.blandware.atleap.webapp.util.core.VirtualFolder;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import org.apache.commons.validator.GenericValidator;
26 import org.apache.oro.text.regex.MatchResult;
27 import org.apache.oro.text.regex.Pattern;
28 import org.apache.oro.text.regex.PatternCompiler;
29 import org.apache.oro.text.regex.PatternMatcher;
30 import org.apache.oro.text.regex.Perl5Compiler;
31 import org.apache.oro.text.regex.Perl5Matcher;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.config.ActionConfig;
36 import org.apache.struts.config.ModuleConfig;
37 import org.apache.struts.util.ModuleUtils;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.LinkedList JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.SortedSet JavaDoc;
46 import java.util.TreeSet JavaDoc;
47
48 /**
49  * <p>Forward to the page with list of actions
50  * </p>
51  * <p><a HREF="ListActionsAction.java.htm"><i>View Source</i></a></p>
52  * <p/>
53  *
54  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
55  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
56  * @version $Revision: 1.16 $ $Date: 2006/03/10 17:10:15 $
57  * @struts.action path="/core/actionPage/listActions"
58  * validate="false"
59  * roles="core-actionPage-listActions"
60  * @struts.action-forward name="listActions"
61  * path=".core.actionPage.listActions"
62  */

63 public final class ListActionsAction extends BaseGridAction {
64     /**
65      * @param mapping The ActionMapping used to select this instance
66      * @param form The optional ActionForm bean for this request (if any)
67      * @param request The HTTP request we are proceeding
68      * @param response The HTTP response we are creating
69      * @return an ActionForward instance describing where and how
70      * control should be forwarded, or null if response
71      * has already been completed
72      */

73     public ActionForward execute(ActionMapping mapping, ActionForm form,
74                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
75
76         String JavaDoc inputId = request.getParameter("inputId");
77
78         String JavaDoc currentFolder = "";
79
80         if ( request.getParameter("currentFolder") != null ) {
81             currentFolder = request.getParameter("currentFolder");
82         } else if ( request.getSession().getAttribute(WebappConstants.ACTIONS_LIST_CURRENT_PATH_KEY) != null ) {
83             currentFolder = (String JavaDoc) request.getSession().getAttribute(WebappConstants.ACTIONS_LIST_CURRENT_PATH_KEY);
84         }
85
86         // override value, if 'uri' parameter is specified
87
if ( !GenericValidator.isBlankOrNull(request.getParameter("uri")) && !"/".equals(request.getParameter("uri")) ) {
88             currentFolder = request.getParameter("uri");
89             int k = currentFolder.lastIndexOf("/");
90             if ( k != -1 ) {
91                 currentFolder = currentFolder.substring(0, k + 1);
92             }
93         }
94
95         request.getSession().setAttribute(WebappConstants.ACTIONS_LIST_CURRENT_PATH_KEY, currentFolder);
96
97         if ( currentFolder == null || currentFolder.equals("/") ) {
98             currentFolder = "";
99         }
100
101         // ensure, that current folder starts with slash and does not end with slash
102
if ( currentFolder.length() > 0 ) {
103             if ( !currentFolder.startsWith("/") ) {
104                 currentFolder = "/" + currentFolder;
105             }
106             if ( currentFolder.endsWith("/") ) {
107                 currentFolder = currentFolder.substring(0, currentFolder.length() - 1);
108             }
109         }
110
111         // Retrieve all action configs
112
ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, getServlet().getServletContext());
113         ActionConfig[] actionConfigs = moduleConfig.findActionConfigs();
114
115         Grid actionsGrid = getGridByName(WebappConstants.ACTIONS_GRID, request.getSession());
116
117         if ( actionsGrid == null ) {
118             actionsGrid = new Grid(WebappConstants.ACTIONS_GRID);
119         }
120
121         String JavaDoc prefix = moduleConfig.getPrefix();
122
123         // filter all actions
124
ArrayList JavaDoc filteredActions = new ArrayList JavaDoc();
125         for ( int i = 0; i < actionConfigs.length; i++ ) {
126             String JavaDoc uri = actionConfigs[i].getPath().substring(prefix.length());
127
128             if ( uri.startsWith(currentFolder + "/") ) {
129                 filteredActions.add(uri);
130             }
131
132         }
133
134         saveGrid(actionsGrid, request.getSession());
135
136         // we have list of actions which are all placed in current folder (see iteration above)
137
// we only need to divide acquired set to subfolders and pages
138
// we need regular expression in order to do it:
139
String JavaDoc folderPatternString = "";
140         if ( currentFolder.length() > 0 ) {
141             folderPatternString = "(" + RegExUtil.escapeMetasymbols(currentFolder) + ")";
142         }
143         folderPatternString += "\\/([^\\/]+)(\\/[^\\/]+)+";
144
145         // pattern compiler instance to use
146
PatternCompiler compiler = new Perl5Compiler();
147
148         // compiled patterns
149
Pattern folderPattern = compiler.compile(folderPatternString);
150
151         // pattern matcher instance to use
152
PatternMatcher matcher = new Perl5Matcher();
153
154         // list of folders
155
SortedSet JavaDoc folderSet = new TreeSet JavaDoc();
156
157         // list of pages
158
List actions = new ArrayList JavaDoc();
159
160         for ( Iterator JavaDoc i = filteredActions.iterator(); i.hasNext(); ) {
161             String JavaDoc uri = (String JavaDoc) i.next();
162
163             // check folder name
164
if ( matcher.matches(uri, folderPattern) ) {
165                 MatchResult result = matcher.getMatch();
166
167                 // according to pattern, if current folder is not an empty string, folder name is in second group
168
// otherwise it is in the first group;
169
String JavaDoc folderName = currentFolder.length() > 0 ? result.group(2) : result.group(1);
170                 Folder folder = new VirtualFolder(folderName, currentFolder, '/');
171                 folderSet.add(folder);
172
173             } else {
174                 // add action to list
175
actions.add(uri.substring(currentFolder.length() + 1));
176             }
177         }
178
179         // create parent folder if current folder is not the root folder
180
Folder parentFolder = null;
181         if ( currentFolder.length() > 0 ) {
182             int k = currentFolder.lastIndexOf("/");
183             if ( k < 0 ) {
184                 k = 0;
185             }
186             String JavaDoc parentFolderPath = currentFolder.substring(0, k);
187             parentFolder = new VirtualFolder("..", parentFolderPath, '/');
188         }
189
190         LinkedList JavaDoc folders = new LinkedList JavaDoc(folderSet);
191
192         if ( parentFolder != null ) {
193             folders.addFirst(parentFolder);
194         }
195
196
197         request.getSession().setAttribute(WebappConstants.ACTIONS_LIST_COLLECTION_KEY, actions);
198         request.setAttribute(WebappConstants.FOLDERS_COLLECTION_KEY, folders);
199         request.setAttribute(WebappConstants.CURRENT_PATH_KEY, new FolderList(VirtualFolder.splitPath(currentFolder)));
200
201
202         if ( inputId != null && inputId.length() != 0 ) {
203             request.getSession().setAttribute(WebappConstants.HTML_INPUT_TAG_ID_KEY, inputId);
204         }
205         return mapping.findForward("listActions");
206     }
207 }
Popular Tags