1 21 package com.jaspersoft.jasperserver.war.action; 22 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.springframework.validation.DataBinder; 29 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor; 30 import org.springframework.webflow.Event; 31 import org.springframework.webflow.RequestContext; 32 import org.springframework.webflow.ScopeType; 33 import org.springframework.webflow.action.FormAction; 34 35 import com.jaspersoft.jasperserver.api.JSException; 36 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource; 37 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder; 38 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource; 39 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup; 40 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 41 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection; 42 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapClientConnection; 43 import com.jaspersoft.jasperserver.api.metadata.olap.domain.XMLAConnection; 44 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria; 45 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor; 46 import com.jaspersoft.jasperserver.war.common.JasperServerConst; 47 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl; 48 import com.jaspersoft.jasperserver.war.dto.BaseDTO; 49 import com.jaspersoft.jasperserver.war.dto.FileResourceWrapper; 50 import com.jaspersoft.jasperserver.war.dto.OlapClientConnectionWrapper; 51 import com.jaspersoft.jasperserver.war.dto.OlapUnitWrapper; 52 import com.jaspersoft.jasperserver.war.validation.FileResourceValidator; 53 54 public class FileResourceAction extends FormAction { 55 56 protected final Log log = LogFactory.getLog(this.getClass()); 57 58 private static final String FORM_OBJECT_KEY = "fileResource"; 59 60 private static final String FILERES_URI_PARAM = "resource"; 61 62 private static final String PARENT_FOLDER_ATTR = "parentFolder"; 63 64 private static final String CONSTANTS_KEY = "constants"; 65 66 private RepositoryService repository; 67 68 public FileResourceAction() { 69 setFormObjectClass(FileResourceWrapper.class); 70 setFormObjectName(FORM_OBJECT_KEY); 71 setFormObjectScope(ScopeType.FLOW); 72 setValidator(new FileResourceValidator()); 73 } 74 75 public Event initAction(RequestContext context) throws Exception { 76 FileResourceWrapper wrapper = (FileResourceWrapper) getFormObject(context); 77 if (wrapper.isSubflowMode()) { 78 FilterCriteria criteria = FilterCriteria 79 .createFilter(FileResource.class); 80 if (wrapper.getFileResource().getFileType() != null 81 && wrapper.getFileResource().getFileType().trim().length() != 0) { 82 criteria.addFilterElement(FilterCriteria 83 .createPropertyEqualsFilter("fileType", wrapper 84 .getFileResource().getFileType())); 85 } 86 ResourceLookup[] lookups = repository.findResource(null, criteria); 87 List allResources = null; 88 if (lookups != null && lookups.length != 0) { 89 allResources = new ArrayList (); 90 log("Found lookups size=" + lookups.length); 91 for (int i = 0; i < lookups.length; i++) { 92 allResources.add(lookups[i].getURIString()); 93 } 94 } 95 wrapper.setAllResources(allResources); 96 } 97 99 if (wrapper.isNewMode()) { 100 String folderURI = wrapper.getFileResource().getParentFolder(); 101 if (folderURI == null) 102 { 103 folderURI = "/"; 104 } 105 FilterCriteria resourcesInFolder = FilterCriteria.createFilter(); 106 resourcesInFolder.addFilterElement(FilterCriteria 107 .createParentFolderFilter(folderURI)); 108 log("Searching for resources in the chosen folder:"+folderURI); 109 ResourceLookup[] existingResources = repository.findResource(null, 110 resourcesInFolder); 111 112 if (existingResources != null && existingResources.length != 0) { 113 log("res lookup size="+existingResources.length); 114 List allResources = new ArrayList (); 115 for (int i = 0; i < existingResources.length; i++) { 116 ResourceLookup rLookup = existingResources[i]; 117 allResources.add(rLookup.getName()); 118 log("adding resource: "+rLookup.getName()+ " to the list"); 119 } 120 wrapper.setExistingResources(allResources); 121 } 122 } 123 124 if (wrapper.isSubflowMode()) { 125 getAllFolders(wrapper); String folderURI = (String ) context.getFlowScope().get(PARENT_FOLDER_ATTR); 127 if (folderURI == null) 128 { 129 folderURI = "/"; 130 } 131 wrapper.getFileResource().setParentFolder( folderURI); 133 if (wrapper.getParentFlowObject() instanceof OlapClientConnectionWrapper) { 135 wrapper.getFileResource().setFileType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA); 136 } 137 } 138 context.getFlowScope().put(FORM_OBJECT_KEY, wrapper); 139 context.getFlowScope().put(CONSTANTS_KEY, new JasperServerConstImpl()); 140 return success(); 141 } 142 143 private void getAllFolders(FileResourceWrapper wrapper) 144 { 145 List allFolders = repository.getAllFolders(null); 146 wrapper.setAllFolders(new ArrayList ()); 147 for (int i = 0; i < allFolders.size(); i++) { 148 String folderUri = ((Folder) allFolders.get(i)).getURIString(); 149 wrapper.getAllFolders().add(folderUri); 150 } 151 } 152 153 public Event determineType(RequestContext context) throws Exception { 154 FileResourceWrapper wrapper = (FileResourceWrapper) getFormObject(context); 155 if (wrapper.getFileResource().getFileType() == null) { 156 String fileExtension = context.getExternalContext().getRequestMap() 157 .getString(JasperServerConst.UPLOADED_FILE_EXT); 158 if (fileExtension != null && fileExtension.trim().length() != 0) { 159 wrapper.getFileResource().setFileType( 160 wrapper.getTypeForExtention(fileExtension)); 161 } 162 } 163 if (wrapper.getSource() != null 164 && wrapper.getSource().equals( 165 JasperServerConst.FIELD_CHOICE_CONT_REPO)) { 166 String newUri = wrapper.getNewUri(); 168 if (newUri != null && newUri.trim().length() != 0) { 169 Resource resource = repository.getResource(null, newUri); 170 if (resource == null) 171 throw new JSException("Couldnt find the resource with URI " 172 + newUri); 173 if (FileResource.class.isAssignableFrom(resource.getClass())) { 174 FileResource fileR = (FileResource) resource; 175 wrapper.getFileResource().setFileType(fileR.getFileType()); 176 } 177 if (wrapper.getFileResource().getFileType().equals(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA)) { 179 ((FileResource) resource).setReferenceURI(wrapper.getFileResource().getReferenceURI()); 180 wrapper.setFileResource((FileResource) resource); 181 } 182 } 183 } 184 wrapper.setLocated(true); 185 return success(); 186 } 187 188 195 public Event saveResource(RequestContext context) throws Exception { 196 log("In saveresource"); 197 FileResourceWrapper wrapper = (FileResourceWrapper) getFormObject(context); 198 if (wrapper.getFileResource().getParentFolder() == null) 199 wrapper.getFileResource().setParentFolder("/"); 200 if (wrapper.isStandAloneMode()) { 201 repository.saveResource(null, wrapper.getFileResource()); 202 } 203 return success(); 204 } 205 206 public Object loadFormObject(RequestContext context) { 207 FileResourceWrapper formObject = null; 208 String resourceUri = context.getRequestParameters().get( 209 FILERES_URI_PARAM); 210 if (resourceUri != null && resourceUri.trim().length() != 0) { 211 Resource resource = (Resource) repository.getResource(null, 212 resourceUri); 213 if (resource == null) 214 throw new JSException("Could not find resource with URI " 215 + resourceUri); 216 log("Found resource with uri=" + resourceUri); 217 formObject = new FileResourceWrapper(); 218 formObject.setFileResource((FileResource) resource); 219 formObject.setMode(BaseDTO.MODE_STAND_ALONE_EDIT); 220 formObject.setLocated(true); 221 } 222 if (formObject == null) { 223 formObject = new FileResourceWrapper(); 224 formObject.setMode(BaseDTO.MODE_STAND_ALONE_NEW); 225 String parentFolder = (String ) context.getFlowScope().get( 227 PARENT_FOLDER_ATTR); 228 FileResource fileResource = (FileResource) repository.newResource( 229 null, FileResource.class); 230 fileResource.setParentFolder(parentFolder); 231 fileResource.setVersion(FileResource.VERSION_NEW); 232 formObject.setFileResource(fileResource); 233 } 234 return formObject; 235 } 236 237 242 public RepositoryService getRepository() { 243 return repository; 244 } 245 246 252 public void setRepository(RepositoryService repository) { 253 this.repository = repository; 254 } 255 256 260 protected void initBinder(RequestContext context, DataBinder binder) { 261 binder.registerCustomEditor(byte[].class, 262 new ByteArrayMultipartFileEditor()); 263 } 264 265 private void log(String text) { 266 log.debug(text); 267 } 268 269 public static String getFORM_OBJECT_KEY() { 270 return FORM_OBJECT_KEY; 271 } 272 273 protected void doBind(RequestContext context, DataBinder binder) 274 throws Exception { 275 super.doBind(context, binder); 276 FileResourceWrapper res = (FileResourceWrapper) binder.getTarget(); 277 res.afterBind(); 278 } 279 } 280 | Popular Tags |