Fix import.

This commit is contained in:
Paulo Gustavo Veiga
2012-04-15 08:00:51 -03:00
parent b245b97db3
commit 873e635b9a
14 changed files with 109 additions and 761 deletions

View File

@@ -46,7 +46,7 @@ public class ImportController
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws ServletException, WiseMappingException, ImporterException {
final ImportMapBean bean = (ImportMapBean) command;
User user = Utils.getUser();
final UserService userService = this.getUserService();
@@ -55,16 +55,14 @@ public class ImportController
mindMap.setOwner(user);
final MindmapService mindmapService = this.getMindmapService();
mindmapService.addMindmap(mindMap,user);
mindmapService.addMindmap(mindMap, user);
final StringBuilder redirectionTo = new StringBuilder("redirect:editor.htm?mapId=");
redirectionTo.append(mindMap.getId());
redirectionTo.append("&action=open");
final StringBuilder redirectionTo = new StringBuilder("redirect:" + mindMap.getId() + "/edit.htm");
return new ModelAndView(redirectionTo.toString());
}
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
throws ServletException {
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
throws ServletException {
// to actually be able to convert Multipart instance to a String
// we have to register a custom editor
binder.registerCustomEditor(String.class, new StringMultipartFileEditor());

View File

@@ -1,58 +0,0 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.controller;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.service.MindmapService;
import com.wisemapping.view.MindMapInfoBean;
import com.wisemapping.model.MindMap;
import com.wisemapping.model.User;
import com.wisemapping.security.Utils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RenameMindmapController
extends BaseSimpleFormController {
//~ Methods ..............................................................................................
protected Object formBackingObject(HttpServletRequest httpServletRequest) throws Exception {
final MindMap mindMap = getMindmapFromRequest(httpServletRequest);
User user = Utils.getUser();
if (!mindMap.getOwner().equals(user)) {
throw new IllegalStateException("No enough right to execute this operation");
}
return new MindMapInfoBean(mindMap);
}
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws ServletException, WiseMappingException {
final MindMapInfoBean bean = (MindMapInfoBean) command;
final MindmapService mindmapService = this.getMindmapService();
mindmapService.updateMindmap(bean.getMindMap(), false);
return new ModelAndView(getSuccessView());
}
}

View File

@@ -1,98 +0,0 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.controller;
import com.wisemapping.model.MindMap;
import com.wisemapping.model.MindMapCriteria;
import com.wisemapping.security.Utils;
import com.wisemapping.view.MindMapBean;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SearchController extends BaseMultiActionController {
private static final int MAX_RESULT = 20;
public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException noSuchRequestHandlingMethodException, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
return searchPage(httpServletRequest, httpServletResponse);
}
public ModelAndView searchPage(HttpServletRequest request, HttpServletResponse response) {
logger.info("Search");
return new ModelAndView("search");
}
public ModelAndView showAll(HttpServletRequest request, HttpServletResponse response)
{
final Map<String, Object> viewAttrMap = getRequestAttributes(request);
viewAttrMap.put("emptyCriteria", false);
com.wisemapping.model.User user = Utils.getUser();
viewAttrMap.put("user",user);
final List<MindMap> searchResult = getMindmapService().getPublicMaps(MAX_RESULT);
final List<MindMapBean> result = new ArrayList<MindMapBean>();
for (MindMap mindMap : searchResult) {
result.add(new MindMapBean(mindMap));
}
viewAttrMap.put("wisemapsList", result);
return new ModelAndView("searchResult", viewAttrMap);
}
public ModelAndView search(HttpServletRequest request, HttpServletResponse response) {
logger.info("Search Result");
final Map<String, Object> viewAttrMap = getRequestAttributes(request);
final MindMapCriteria criteria = getMindMapCriteriaFromRequest(request);
viewAttrMap.put("emptyCriteria", criteria.isEmpty());
com.wisemapping.model.User user = Utils.getUser();
viewAttrMap.put("user",user);
if (!criteria.isEmpty()) {
final List<MindMap> searchResult = getMindmapService().search(criteria);
final List<MindMapBean> result = new ArrayList<MindMapBean>();
for (MindMap mindMap : searchResult) {
result.add(new MindMapBean(mindMap));
}
viewAttrMap.put("wisemapsList", result);
}
return new ModelAndView("searchResult", viewAttrMap);
}
private Map<String, Object> getRequestAttributes(HttpServletRequest request) {
final Map<String, Object> viewAttrMap = new HashMap<String, Object>();
viewAttrMap.put("titleOrTags", request.getParameter("titleOrTags"));
final String name = request.getParameter("name");
viewAttrMap.put("name", name);
viewAttrMap.put("description", request.getParameter("description"));
viewAttrMap.put("tags", request.getParameter("tags"));
viewAttrMap.put("advanceSearch", request.getParameter("advanceSearch"));
return viewAttrMap;
}
}

View File

@@ -25,6 +25,7 @@ import com.wisemapping.importer.ImporterException;
import com.wisemapping.importer.ImporterFactory;
import com.wisemapping.model.MindMap;
import com.wisemapping.view.ImportMapBean;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
@@ -32,26 +33,25 @@ import java.io.ByteArrayInputStream;
public class ImportMapValidator extends MapInfoValidator {
public boolean supports(final Class clazz) {
public boolean supports(final Class clazz) {
return clazz.equals(ImportMapBean.class);
}
public void validate(Object obj, Errors errors) {
ImportMapBean bean = (ImportMapBean) obj;
super.validate(obj,errors);
public void validate(Object obj, @NotNull Errors errors) {
final ImportMapBean bean = (ImportMapBean) obj;
this.validateMapInfo(errors, bean.getTitle(), bean.getDescription());
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "mapFile", Messages.FIELD_REQUIRED);
try {
final Importer importer = ImporterFactory.getInstance().getImporter(ImportFormat.FREEMIND);
final ByteArrayInputStream stream = new ByteArrayInputStream(bean.getMapFile().getBytes());
final MindMap map = importer.importMap(bean.getTitle(),bean.getDescription(),stream);
final MindMap map = importer.importMap(bean.getTitle(), bean.getDescription(), stream);
bean.setImportedMap(map);
} catch (ImporterException e) {
Object[] errorArgs = new Object[]{e.getMessage()};
errors.rejectValue("mapFile", Messages.IMPORT_MAP_ERROR,errorArgs,"FreeMind could not be imported.");
Object[] errorArgs = new Object[]{e.getMessage()};
errors.rejectValue("mapFile", Messages.IMPORT_MAP_ERROR, errorArgs, "FreeMind could not be imported.");
}
}
}

View File

@@ -48,38 +48,45 @@ public class MapInfoValidator implements Validator {
public void validate(Object obj, @NotNull Errors errors) {
final MindMap map = (MindMap) obj;
if (map == null) {
errors.rejectValue("map", "error.not-specified", null, "Value required.");
} else {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", Messages.FIELD_REQUIRED);
final String title = map.getTitle();
final String desc = map.getDescription();
if (title != null && title.length() > 0) {
if (title.length() > Constants.MAX_MAP_NAME_LENGTH) {
errors.rejectValue("title", "field.max.length",
new Object[]{Constants.MAX_MAP_NAME_LENGTH},
"The title must have less than " + Constants.MAX_MAP_NAME_LENGTH + " characters.");
} else {
// Map already exists ?
final MindmapService service = this.getMindmapService();
final User user = com.wisemapping.security.Utils.getUser();
final MindMap mindMap = service.getMindmapByTitle(title, user);
if (mindMap != null) {
errors.rejectValue("title", Messages.MAP_TITLE_ALREADY_EXISTS);
}
}
}
ValidatorUtils.rejectIfExceeded(errors,
"description",
"The description must have less than " + Constants.MAX_MAP_DESCRIPTION_LENGTH + " characters.",
desc,
Constants.MAX_MAP_DESCRIPTION_LENGTH);
validateMapInfo(errors, title, desc);
}
}
protected void validateMapInfo(Errors errors, String title, String desc) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", Messages.FIELD_REQUIRED);
if (title != null && title.length() > 0) {
if (title.length() > Constants.MAX_MAP_NAME_LENGTH) {
errors.rejectValue("title", "field.max.length",
new Object[]{Constants.MAX_MAP_NAME_LENGTH},
"The title must have less than " + Constants.MAX_MAP_NAME_LENGTH + " characters.");
} else {
// Map already exists ?
final MindmapService service = this.getMindmapService();
final User user = com.wisemapping.security.Utils.getUser();
final MindMap mindMap = service.getMindmapByTitle(title, user);
if (mindMap != null) {
errors.rejectValue("title", Messages.MAP_TITLE_ALREADY_EXISTS);
}
}
}
ValidatorUtils.rejectIfExceeded(errors,
"description",
"The description must have less than " + Constants.MAX_MAP_DESCRIPTION_LENGTH + " characters.",
desc,
Constants.MAX_MAP_DESCRIPTION_LENGTH);
}
public MindmapService getMindmapService() {
return mindmapService;
}

View File

@@ -1,82 +0,0 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.validator;
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import com.wisemapping.service.MindmapService;
import com.wisemapping.view.MindMapInfoBean;
import com.wisemapping.controller.Messages;
import com.wisemapping.model.Constants;
import com.wisemapping.model.User;
import com.wisemapping.model.MindMap;
public class RenameMapValidator
implements Validator {
private MindmapService mindmapService;
public boolean supports(final Class clazz) {
return clazz.equals(MindMapInfoBean.class);
}
public void validate(Object obj, Errors errors) {
final MindMapInfoBean map = (MindMapInfoBean) obj;
if (map == null) {
errors.rejectValue("map", "error.not-specified", null, "Value required.");
} else {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", Messages.FIELD_REQUIRED);
final String title = map.getTitle();
final String desc = map.getDescription();
if (title != null && title.length() > 0) {
if (title.length() > Constants.MAX_MAP_NAME_LENGTH) {
errors.rejectValue("title", "field.max.length",
new Object[]{Constants.MAX_MAP_NAME_LENGTH},
"The title must have less than " + Constants.MAX_MAP_NAME_LENGTH + " characters.");
} else {
// Map alredy exists ?
final MindmapService service = this.getMindmapService();
final User user = com.wisemapping.security.Utils.getUser();
final MindMap mindMap = service.getMindmapByTitle(title, user);
if (mindMap != null && map.getMindMap().getId() != mindMap.getId() ) {
errors.rejectValue("title", Messages.MAP_TITLE_ALREADY_EXISTS);
}
}
}
ValidatorUtils.rejectIfExceeded(errors,
"description",
"The description must have less than "+Constants.MAX_MAP_DESCRIPTION_LENGTH + " characters.",
desc,
Constants.MAX_MAP_DESCRIPTION_LENGTH);
}
}
public MindmapService getMindmapService() {
return mindmapService;
}
public void setMindmapService(MindmapService mindmapService) {
this.mindmapService = mindmapService;
}
}