KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContentImage;
25 import com.blandware.atleap.service.core.ContentResourceManager;
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.action.ActionForm;
40 import org.apache.struts.action.ActionForward;
41 import org.apache.struts.action.ActionMapping;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.LinkedList JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.SortedSet JavaDoc;
52 import java.util.TreeSet JavaDoc;
53
54 /**
55  * <p>Gets collection of images and folders to display in browser
56  * </p>
57  * <p><a HREF="BrowseImagesAction.java.htm"><i>View Source</i></a></p>
58  * <p/>
59  *
60  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
61  * @version $Revision: 1.20 $ $Date: 2006/03/10 17:10:17 $
62  * @struts.action path="/core/browser/browseImages"
63  * validate="false"
64  * roles="core-browser-image"
65  * @struts.action-forward name="showBrowser"
66  * path=".core.browser.imageBrowser"
67  */

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

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

106         StringFilter uriFilter = new StringFilter("r.uri");
107         uriFilter.createFirstClause(FilterConditions.STRING_STARTS_WITH, Constants.RESOURCES_URI_PREFIX + currentFolder + "/");
108
109         Grid crGrid = getGridByName(WebappConstants.BROWSE_IMAGES_GRID, request.getSession());
110         if ( crGrid == null ) {
111             crGrid = new Grid(WebappConstants.BROWSE_IMAGES_GRID);
112         }
113
114         crGrid.addFilter(uriFilter);
115
116         saveGrid(crGrid, request.getSession());
117
118         QueryInfo queryInfo = new QueryInfo();
119         queryInfo.setWhereClause(crGrid.getWhereClause());
120
121         Map JavaDoc queryParameters = new HashMap JavaDoc();
122         queryParameters.put("contentImageType", Constants.RESOURCE_TYPE_IMAGE);
123         queryInfo.setQueryParameters(queryParameters);
124
125         ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
126         PartialCollection contentImages = contentResourceManager.listContentImages(queryInfo);
127
128         // we have list of action images which are all placed in current folder
129
// we only need to divide acquired set to subfolders and images
130
// we need regular expression in order to do it:
131
String JavaDoc folderPatternString = "(" + RegExUtil.escapeMetasymbols(Constants.RESOURCES_URI_PREFIX + currentFolder) + ")";
132         folderPatternString += "\\/([^\\/]+)(\\/[^\\/]+)+";
133
134         // pattern compiler instance to use
135
PatternCompiler compiler = new Perl5Compiler();
136
137         // compiled patterns
138
Pattern folderPattern = compiler.compile(folderPatternString);
139
140         // pattern matcher instance to use
141
PatternMatcher matcher = new Perl5Matcher();
142
143         // list of folders
144
SortedSet JavaDoc folderSet = new TreeSet JavaDoc();
145
146         // list of images
147
List images = new ArrayList JavaDoc();
148
149         for ( Iterator JavaDoc i = contentImages.iterator(); i.hasNext(); ) {
150             ContentImage contentImage = (ContentImage) i.next();
151             String JavaDoc uri = contentImage.getUri();
152
153             // check folder name
154
if ( matcher.matches(uri, folderPattern) ) {
155                 MatchResult result = matcher.getMatch();
156
157                 // according to pattern, folder name is in second group
158
Folder folder = new VirtualFolder(result.group(2), currentFolder, '/');
159                 folderSet.add(folder);
160
161             } else {
162                 // add image to list
163
ContentImage tmp = new ContentImage();
164                 WebappUtil.copyProperties(tmp, contentImage, request);
165                 int prefixLength = Constants.RESOURCES_URI_PREFIX.length();
166                 uri = uri.substring(prefixLength);
167                 tmp.setUri(uri.substring(currentFolder.length() + 1));
168                 images.add(tmp);
169             }
170         }
171
172         // create parent folder if current folder is not the root folder
173
Folder parentFolder = null;
174         if ( currentFolder.length() > 0 ) {
175             int k = currentFolder.lastIndexOf("/");
176             if ( k < 0 ) {
177                 k = 0;
178             }
179             String JavaDoc parentFolderPath = currentFolder.substring(0, k);
180             parentFolder = new VirtualFolder("..", parentFolderPath, '/');
181         }
182
183         LinkedList JavaDoc folders = new LinkedList JavaDoc(folderSet);
184         if ( parentFolder != null ) {
185             folders.addFirst(parentFolder);
186         }
187
188         request.setAttribute(WebappConstants.BROWSER_IMAGES_COLLECTION_KEY, images);
189         request.setAttribute(WebappConstants.FOLDERS_COLLECTION_KEY, folders);
190
191         request.setAttribute(WebappConstants.CURRENT_PATH_KEY, new FolderList(VirtualFolder.splitPath(currentFolder)));
192
193         String JavaDoc inputId = request.getParameter("inputId");
194         if ( inputId != null && inputId.length() != 0 ) {
195             request.getSession().setAttribute(WebappConstants.HTML_INPUT_TAG_ID_KEY, inputId);
196         }
197
198         return mapping.findForward("showBrowser");
199
200     }
201
202 }
Popular Tags