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

@@ -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;
}
}