KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > layout > ListDefinitionsAction


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.layout;
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.struts.HeritableComponentDefinition;
23 import com.blandware.atleap.webapp.struts.HeritableI18nFactorySet;
24 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
25 import com.blandware.atleap.webapp.taglib.core.grid.util.SortField;
26 import com.blandware.atleap.webapp.util.core.VirtualFolder;
27 import com.blandware.atleap.webapp.util.core.WebappConstants;
28 import org.apache.oro.text.regex.MatchResult;
29 import org.apache.oro.text.regex.Pattern;
30 import org.apache.oro.text.regex.PatternCompiler;
31 import org.apache.oro.text.regex.PatternMatcher;
32 import org.apache.oro.text.regex.Perl5Compiler;
33 import org.apache.oro.text.regex.Perl5Matcher;
34 import org.apache.struts.action.ActionForm;
35 import org.apache.struts.action.ActionForward;
36 import org.apache.struts.action.ActionMapping;
37 import org.apache.struts.tiles.TilesUtil;
38 import org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper;
39
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.LinkedList JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.SortedSet JavaDoc;
48 import java.util.TreeSet JavaDoc;
49
50 /**
51  * <p>Forward to the page with list of tiles definitions
52  * </p>
53  * <p><a HREF="ListDefinitionsAction.java.htm"><i>View Source</i></a></p>
54  * <p/>
55  *
56  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
57  * @version $Revision: 1.14 $ $Date: 2006/03/10 17:10:25 $
58  * @struts.action path="/core/layout/listDefinitions"
59  * validate="false"
60  * roles="core-layout-listDefinitions"
61  * @struts.action-forward name="listDefinitions"
62  * path=".core.layout.listDefinitions"
63  */

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

74     public ActionForward execute(ActionMapping mapping, ActionForm form,
75                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
76
77         String JavaDoc inputId = request.getParameter("inputId");
78
79         String JavaDoc currentFolder = "";
80
81         if ( request.getParameter("currentFolder") != null ) {
82             currentFolder = request.getParameter("currentFolder");
83         } else if ( request.getSession().getAttribute(WebappConstants.DEFINITIONS_LIST_CURRENT_PATH_KEY) != null ) {
84             currentFolder = (String JavaDoc) request.getSession().getAttribute(WebappConstants.DEFINITIONS_LIST_CURRENT_PATH_KEY);
85         }
86
87         if ( currentFolder == null || currentFolder.equals(".") ) {
88             currentFolder = "";
89         }
90
91         request.getSession().setAttribute(WebappConstants.DEFINITIONS_LIST_CURRENT_PATH_KEY, currentFolder);
92
93         // ensure, that current folder starts with slash and does not end with slash
94
if ( currentFolder.length() > 0 ) {
95             if ( !currentFolder.startsWith(".") ) {
96                 currentFolder = "." + currentFolder;
97             }
98             if ( currentFolder.endsWith(".") ) {
99                 currentFolder = currentFolder.substring(0, currentFolder.length() - 1);
100             }
101         }
102
103         // Retrieve all definitions
104
Map JavaDoc definitionsMap = null;
105         try {
106             definitionsMap = ((HeritableI18nFactorySet) ((ComponentDefinitionsFactoryWrapper) TilesUtil.getDefinitionsFactory(request, request.getSession().getServletContext())).getInternalFactory()).getDefinitions(request, request.getSession().getServletContext());
107         } catch ( Exception JavaDoc ex ) {
108             if ( log.isErrorEnabled() ) {
109                 log.error("Cannot get definitions list", ex);
110             }
111         }
112
113         List definitions = new ArrayList JavaDoc(definitionsMap.values());
114
115         Grid definitionsGrid = getGridByName(WebappConstants.DEFINITIONS_GRID, request.getSession());
116
117         if ( definitionsGrid == null ) {
118             definitionsGrid = new Grid(WebappConstants.DEFINITIONS_GRID);
119         }
120
121         // catch parameters related to sorting
122
String JavaDoc sortFieldName = request.getParameter("sortField");
123         SortField sortField = new SortField("name");
124         if ( WebappConstants.DEFINITIONS_GRID.equals(request.getParameter("gridName")) && "name".equalsIgnoreCase(sortFieldName) ) {
125             sortField = definitionsGrid.getSortFieldByFieldName(sortFieldName);
126             if ( sortField == null ) {
127                 // add new sort field to grid with ascending order: when GridTag is evaluated,
128
// order will be changed to descending
129
sortField = new SortField(sortFieldName);
130                 definitionsGrid.addSortField(sortField);
131             }
132             sortField.reverseOrder();
133         }
134
135
136         ArrayList JavaDoc filteredDefinitions = new ArrayList JavaDoc();
137
138         for ( int i = 0; i < definitions.size(); i++ ) {
139             HeritableComponentDefinition definition = (HeritableComponentDefinition) definitions.get(i);
140             String JavaDoc definitionName = definition.getName();
141
142             if ( definitionName.startsWith(currentFolder + ".") ) {
143                 filteredDefinitions.add(definitionName);
144             }
145
146         }
147
148         saveGrid(definitionsGrid, request.getSession());
149
150         // we have list of definitions which are all placed in current folder (see iteration above)
151
// we only need to divide acquired set to subfolders and pages
152
// we need regular expression in order to do it:
153
String JavaDoc folderPatternString = "";
154         if ( currentFolder.length() > 0 ) {
155             folderPatternString = "(" + RegExUtil.escapeMetasymbols(currentFolder) + ")";
156         }
157         folderPatternString += "\\.([^\\.]+)(\\.[^\\.]+)+";
158
159         // pattern compiler instance to use
160
PatternCompiler compiler = new Perl5Compiler();
161
162         // compiled patterns
163
Pattern folderPattern = compiler.compile(folderPatternString);
164
165         // pattern matcher instance to use
166
PatternMatcher matcher = new Perl5Matcher();
167
168         // list of folders
169
SortedSet JavaDoc folderSet = new TreeSet JavaDoc();
170
171         // list of pages
172
definitions = new ArrayList JavaDoc();
173
174         for ( Iterator JavaDoc i = filteredDefinitions.iterator(); i.hasNext(); ) {
175             String JavaDoc name = (String JavaDoc) i.next();
176
177             // check folder name
178
if ( matcher.matches(name, folderPattern) ) {
179                 MatchResult result = matcher.getMatch();
180
181                 // according to pattern, if current folder is not an empty string, folder name is in second group
182
// otherwise it is in the first group;
183
String JavaDoc folderName = currentFolder.length() > 0 ? result.group(2) : result.group(1);
184                 Folder folder = new VirtualFolder(folderName, currentFolder, '.');
185                 folderSet.add(folder);
186
187             } else {
188                 // add definition to list
189
definitions.add(name.substring(currentFolder.length() + 1));
190             }
191         }
192
193         // create parent folder if current folder is not the root folder
194
Folder parentFolder = null;
195         if ( currentFolder.length() > 0 ) {
196             int k = currentFolder.lastIndexOf(".");
197             if ( k < 0 ) {
198                 k = 0;
199             }
200             String JavaDoc parentFolderPath = currentFolder.substring(0, k);
201             parentFolder = new VirtualFolder("..", parentFolderPath, '.');
202         }
203
204         LinkedList JavaDoc folders = new LinkedList JavaDoc(folderSet);
205         if ( parentFolder != null ) {
206             folders.addFirst(parentFolder);
207         }
208
209         request.setAttribute(WebappConstants.DEFINITIONS_LIST_COLLECTION_KEY, definitions);
210         request.setAttribute(WebappConstants.FOLDERS_COLLECTION_KEY, folders);
211         request.setAttribute(WebappConstants.CURRENT_PATH_KEY, new FolderList(VirtualFolder.splitPath(currentFolder, '.'), '.'));
212
213         if ( inputId != null && inputId.length() != 0 ) {
214             request.getSession().setAttribute(WebappConstants.HTML_INPUT_TAG_ID_KEY, inputId);
215         }
216         return mapping.findForward("listDefinitions");
217     }
218 }
Popular Tags