1 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 ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.util.ArrayList ; 43 import java.util.Iterator ; 44 import java.util.LinkedList ; 45 import java.util.List ; 46 import java.util.Map ; 47 import java.util.SortedSet ; 48 import java.util.TreeSet ; 49 50 64 public final class ListDefinitionsAction extends BaseGridAction { 65 74 public ActionForward execute(ActionMapping mapping, ActionForm form, 75 HttpServletRequest request, HttpServletResponse response) throws Exception { 76 77 String inputId = request.getParameter("inputId"); 78 79 String 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 ) 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 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 Map definitionsMap = null; 105 try { 106 definitionsMap = ((HeritableI18nFactorySet) ((ComponentDefinitionsFactoryWrapper) TilesUtil.getDefinitionsFactory(request, request.getSession().getServletContext())).getInternalFactory()).getDefinitions(request, request.getSession().getServletContext()); 107 } catch ( Exception ex ) { 108 if ( log.isErrorEnabled() ) { 109 log.error("Cannot get definitions list", ex); 110 } 111 } 112 113 List definitions = new ArrayList (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 String 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 sortField = new SortField(sortFieldName); 130 definitionsGrid.addSortField(sortField); 131 } 132 sortField.reverseOrder(); 133 } 134 135 136 ArrayList filteredDefinitions = new ArrayList (); 137 138 for ( int i = 0; i < definitions.size(); i++ ) { 139 HeritableComponentDefinition definition = (HeritableComponentDefinition) definitions.get(i); 140 String definitionName = definition.getName(); 141 142 if ( definitionName.startsWith(currentFolder + ".") ) { 143 filteredDefinitions.add(definitionName); 144 } 145 146 } 147 148 saveGrid(definitionsGrid, request.getSession()); 149 150 String folderPatternString = ""; 154 if ( currentFolder.length() > 0 ) { 155 folderPatternString = "(" + RegExUtil.escapeMetasymbols(currentFolder) + ")"; 156 } 157 folderPatternString += "\\.([^\\.]+)(\\.[^\\.]+)+"; 158 159 PatternCompiler compiler = new Perl5Compiler(); 161 162 Pattern folderPattern = compiler.compile(folderPatternString); 164 165 PatternMatcher matcher = new Perl5Matcher(); 167 168 SortedSet folderSet = new TreeSet (); 170 171 definitions = new ArrayList (); 173 174 for ( Iterator i = filteredDefinitions.iterator(); i.hasNext(); ) { 175 String name = (String ) i.next(); 176 177 if ( matcher.matches(name, folderPattern) ) { 179 MatchResult result = matcher.getMatch(); 180 181 String 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 definitions.add(name.substring(currentFolder.length() + 1)); 190 } 191 } 192 193 Folder parentFolder = null; 195 if ( currentFolder.length() > 0 ) { 196 int k = currentFolder.lastIndexOf("."); 197 if ( k < 0 ) { 198 k = 0; 199 } 200 String parentFolderPath = currentFolder.substring(0, k); 201 parentFolder = new VirtualFolder("..", parentFolderPath, '.'); 202 } 203 204 LinkedList folders = new LinkedList (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 |