KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContentPage;
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 a content 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/browseContentPages"
66  * validate="false"
67  * roles="core-browser-contentPage"
68  * @struts.action-forward name="showBrowser"
69  * path=".core.browser.cpBrowser"
70  */

71 public final class BrowseContentPagesAction 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.getSession().setAttribute(WebappConstants.BROWSER_LOCALE_SUFFIX_KEY, localeSuffix);
90         } else {
91             localeSuffix = (String JavaDoc) request.getSession().getAttribute(WebappConstants.BROWSER_LOCALE_SUFFIX_KEY);
92         }
93
94         String JavaDoc currentFolder = "";
95         if ( request.getParameter("currentFolder") != null ) {
96             currentFolder = request.getParameter("currentFolder");
97             request.getSession().setAttribute(WebappConstants.CONTENT_PAGES_CURRENT_PATH_KEY, currentFolder);
98         } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_PAGES_CURRENT_PATH_KEY) != null ) {
99             currentFolder = (String JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_PAGES_CURRENT_PATH_KEY);
100         }
101
102         if ( currentFolder == null || currentFolder.equals("/") ) {
103             currentFolder = "";
104         }
105
106         // ensure, that current folder starts with slash and does not end with slash
107
if ( currentFolder.length() > 0 ) {
108             if ( !currentFolder.startsWith("/") ) {
109                 currentFolder = "/" + currentFolder;
110             }
111             if ( currentFolder.endsWith("/") ) {
112                 currentFolder = currentFolder.substring(0, currentFolder.length() - 1);
113             }
114         }
115
116         // get all content pages, which exist in current folder
117

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