1 16 package com.blandware.atleap.webapp.action.core.contentResource; 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.ContentResource; 25 import com.blandware.atleap.service.core.ContentResourceManager; 26 import com.blandware.atleap.service.core.LookupManager; 27 import com.blandware.atleap.webapp.action.core.grid.BaseGridAction; 28 import com.blandware.atleap.webapp.taglib.core.grid.util.FilterConditions; 29 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid; 30 import com.blandware.atleap.webapp.taglib.core.grid.util.StringFilter; 31 import com.blandware.atleap.webapp.util.core.*; 32 import org.apache.oro.text.regex.MatchResult; 33 import org.apache.oro.text.regex.Pattern; 34 import org.apache.oro.text.regex.PatternCompiler; 35 import org.apache.oro.text.regex.PatternMatcher; 36 import org.apache.oro.text.regex.Perl5Compiler; 37 import org.apache.oro.text.regex.Perl5Matcher; 38 import org.apache.struts.action.ActionForm; 39 import org.apache.struts.action.ActionForward; 40 import org.apache.struts.action.ActionMapping; 41 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpServletResponse ; 44 import java.util.ArrayList ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.LinkedList ; 48 import java.util.List ; 49 import java.util.Map ; 50 import java.util.SortedSet ; 51 import java.util.TreeSet ; 52 53 68 public final class ListContentResourcesAction extends BaseGridAction { 69 78 public ActionForward execute(ActionMapping mapping, ActionForm form, 79 HttpServletRequest request, HttpServletResponse response) throws Exception { 80 81 String currentFolder = ""; 82 if ( request.getParameter("currentFolder") != null ) { 83 currentFolder = request.getParameter("currentFolder"); 84 request.getSession().setAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY, currentFolder); 85 } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY) != null ) { 86 currentFolder = (String ) request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY); 87 } 88 89 if ( currentFolder == null || currentFolder.equals("/") ) { 90 currentFolder = ""; 91 } 92 93 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 105 StringFilter uriFilter = new StringFilter("r.uri"); 106 uriFilter.createFirstClause(FilterConditions.STRING_STARTS_WITH, Constants.RESOURCES_URI_PREFIX + currentFolder + "/"); 107 108 Grid crGrid = getGridByName(WebappConstants.CONTENT_RESOURCES_GRID, request.getSession()); 109 if ( crGrid == null ) { 110 crGrid = new Grid(WebappConstants.CONTENT_RESOURCES_GRID); 111 } 112 113 crGrid.addFilter(uriFilter); 114 115 saveGrid(crGrid, request.getSession()); 116 117 QueryInfo queryInfo = new QueryInfo(); 118 queryInfo.setWhereClause(crGrid.getWhereClause()); 119 120 ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN); 121 PartialCollection contentResources = contentResourceManager.listContentResources(queryInfo); 122 123 String folderPatternString = "(" + RegExUtil.escapeMetasymbols(Constants.RESOURCES_URI_PREFIX + currentFolder) + ")"; 127 folderPatternString += "\\/([^\\/]+)(\\/[^\\/]+)+"; 128 129 PatternCompiler compiler = new Perl5Compiler(); 131 132 Pattern folderPattern = compiler.compile(folderPatternString); 134 135 PatternMatcher matcher = new Perl5Matcher(); 137 138 SortedSet folderSet = new TreeSet (); 140 141 List resources = new ArrayList (); 143 144 for ( Iterator i = contentResources.iterator(); i.hasNext(); ) { 145 ContentResource contentResource = (ContentResource) i.next(); 146 String uri = contentResource.getUri(); 147 148 if ( matcher.matches(uri, folderPattern) ) { 150 MatchResult result = matcher.getMatch(); 151 152 Folder folder = new VirtualFolder(result.group(2), currentFolder, '/'); 154 folderSet.add(folder); 155 156 } else { 157 ContentResource tmp = new ContentResource(); 159 WebappUtil.copyProperties(tmp, contentResource, request); 160 int prefixLength = Constants.RESOURCES_URI_PREFIX.length(); 161 uri = uri.substring(prefixLength); 162 tmp.setUri(uri.substring(currentFolder.length() + 1)); 163 resources.add(tmp); 164 } 165 } 166 167 Folder parentFolder = null; 169 if ( currentFolder.length() > 0 ) { 170 int k = currentFolder.lastIndexOf("/"); 171 if ( k < 0 ) { 172 k = 0; 173 } 174 String parentFolderPath = currentFolder.substring(0, k); 175 parentFolder = new VirtualFolder("..", parentFolderPath, '/'); 176 } 177 178 LinkedList folders = new LinkedList (folderSet); 179 if ( parentFolder != null ) { 180 folders.addFirst(parentFolder); 181 } 182 183 request.setAttribute(WebappConstants.CONTENT_RESOURCES_COLLECTION_KEY, resources); 184 request.setAttribute(WebappConstants.FOLDERS_COLLECTION_KEY, folders); 185 request.setAttribute(WebappConstants.CURRENT_PATH_KEY, new FolderList(VirtualFolder.splitPath(currentFolder))); 186 187 LookupManager lookupManager = (LookupManager) getBean(Constants.LOOKUP_MANAGER_BEAN); 189 ResourceTypesManager typesManager = ResourceTypesManager.getInstance(servlet.getServletContext()); 190 191 List contentResourceTypes = lookupManager.listContentResourceTypes(); 192 Map localizedTypes = new HashMap (); 193 for ( Iterator i = contentResourceTypes.iterator(); i.hasNext(); ) { 194 String type = (String ) i.next(); 195 String localizedType = ApplicationResources.getInstance(servlet.getServletContext()).getMessage(request, typesManager.getResourceTypeNameKey(type)); 196 197 localizedTypes.put(type, localizedType); 198 } 199 200 request.setAttribute(WebappConstants.RESOURCE_TYPES_COLLECTION_KEY, localizedTypes); 201 202 saveToken(request); 203 return mapping.findForward("listContentResources"); 204 } 205 } | Popular Tags |