KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentPage > ListContentPagesAction


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.contentPage;
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.model.core.Layout;
26 import com.blandware.atleap.service.core.LayoutManager;
27 import com.blandware.atleap.service.core.PageManager;
28 import com.blandware.atleap.webapp.action.core.grid.BaseGridAction;
29 import com.blandware.atleap.webapp.taglib.core.grid.util.FilterConditions;
30 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
31 import com.blandware.atleap.webapp.taglib.core.grid.util.StringFilter;
32 import com.blandware.atleap.webapp.util.core.VirtualFolder;
33 import com.blandware.atleap.webapp.util.core.WebappConstants;
34 import com.blandware.atleap.webapp.util.core.WebappUtil;
35 import org.apache.oro.text.regex.MatchResult;
36 import org.apache.oro.text.regex.Pattern;
37 import org.apache.oro.text.regex.PatternCompiler;
38 import org.apache.oro.text.regex.PatternMatcher;
39 import org.apache.oro.text.regex.Perl5Compiler;
40 import org.apache.oro.text.regex.Perl5Matcher;
41 import org.apache.struts.Globals;
42 import org.apache.struts.action.ActionForm;
43 import org.apache.struts.action.ActionForward;
44 import org.apache.struts.action.ActionMapping;
45
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47 import javax.servlet.http.HttpServletResponse JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.LinkedList JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Locale JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.SortedSet JavaDoc;
56 import java.util.TreeSet JavaDoc;
57
58 /**
59  * <p>Forward to the page with list of content pages
60  * </p>
61  * <p><a HREF="ListContentPagesAction.java.htm"><i>View Source</i></a></p>
62  * <p/>
63  *
64  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
65  * @version $Revision: 1.27 $ $Date: 2006/03/26 11:47:35 $
66  * @struts.action path="/core/contentPage/list"
67  * name="contentPageForm"
68  * validate="false"
69  * roles="core-contentPage-list"
70  * @struts.action-forward name="listContentPages"
71  * path=".core.contentPage.list"
72  */

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

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

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