Add error report for fields on new map.

Display error messages
This commit is contained in:
Paulo Gustavo Veiga
2012-04-07 12:45:35 -03:00
parent e4dc4a50aa
commit 3da0eec842
20 changed files with 311 additions and 186 deletions

View File

@@ -1,5 +1,22 @@
package com.wisemapping.rest;
/*
* 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.rest;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.model.User;

View File

@@ -1,7 +1,27 @@
/*
* 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.rest;
import com.wisemapping.rest.model.RestErrors;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -9,6 +29,9 @@ import org.springframework.web.bind.annotation.ResponseStatus;
public class BaseController {
@Autowired
private ResourceBundleMessageSource messageSource;
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
@@ -22,7 +45,14 @@ public class BaseController {
@ResponseBody
public String handleServerErrors(@NotNull Exception ex) {
ex.printStackTrace();
// LOGGER.error(ex.getMessage(), ex);
return ex.getMessage();
}
@ExceptionHandler(ValidationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestErrors handleValidationErrors(@NotNull ValidationException ex) {
return new RestErrors(ex.getErrors(),messageSource);
}
}

View File

@@ -10,10 +10,13 @@ import com.wisemapping.rest.model.RestMindmapInfo;
import com.wisemapping.rest.model.RestMindmapList;
import com.wisemapping.security.Utils;
import com.wisemapping.service.MindmapService;
import com.wisemapping.validator.MapInfoValidator;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@@ -154,22 +157,15 @@ public class MindmapController extends BaseController {
@ResponseStatus(value = HttpStatus.CREATED)
public void createMap(@RequestBody RestMindmap restMindmap, @NotNull HttpServletResponse response) throws IOException, WiseMappingException {
final String title = restMindmap.getTitle();
if (title == null || title.isEmpty()) {
throw new IllegalArgumentException("Map title can not be null");
}
final String description = restMindmap.getDescription();
if (description == null || description.isEmpty()) {
throw new IllegalArgumentException("Map details can not be null");
// Validate ...
final BindingResult result = new BeanPropertyBindingResult(restMindmap, "");
new MapInfoValidator(mindmapService).validate(restMindmap.getDelegated(), result);
if (result.hasErrors()) {
throw new ValidationException(result);
}
// Some basic validations ...
final User user = Utils.getUser();
final MindMap mindMap = mindmapService.getMindmapByTitle(title, user);
if (mindMap != null) {
throw new IllegalArgumentException("Map already exists with title '" + title + "'");
}
// If the user has not specified the xml content, add one ...
final MindMap delegated = restMindmap.getDelegated();
@@ -186,7 +182,6 @@ public class MindmapController extends BaseController {
// Return the new created map ...
response.setHeader("Location", "/service/maps/" + delegated.getId());
response.setHeader("ResourceId", Integer.toString(delegated.getId()));
}
@RequestMapping(method = RequestMethod.POST, value = "/maps/{id}", consumes = {"application/xml", "application/json"})
@@ -222,8 +217,8 @@ public class MindmapController extends BaseController {
// Return the new created map ...
response.setHeader("Location", "/service/maps/" + clonedMap.getId());
response.setHeader("ResourceId", Integer.toString(clonedMap.getId()));
}
}

View File

@@ -1,3 +1,21 @@
/*
* 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.rest;

View File

@@ -0,0 +1,37 @@
/*
* 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.rest;
import com.wisemapping.exceptions.WiseMappingException;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.Errors;
public class ValidationException extends WiseMappingException{
private Errors errors;
public ValidationException(@NotNull Errors errors) {
super("Validation Exceptions");
this.errors = errors;
}
public Errors getErrors() {
return errors;
}
}

View File

@@ -0,0 +1,70 @@
package com.wisemapping.rest.model;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.MessageSource;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.*;
@XmlRootElement(name = "errors")
@XmlAccessorType(XmlAccessType.PROPERTY)
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class RestErrors {
@JsonIgnore
private Errors errors;
@JsonIgnore
MessageSource messageSource;
public RestErrors() {
}
public RestErrors(@NotNull Errors errors, @NotNull MessageSource messageSource) {
this.errors = errors;
this.messageSource = messageSource;
}
public List<String> getGlobalErrors() {
final List<String> result = new ArrayList<String>();
final List<ObjectError> globalErrors = errors.getGlobalErrors();
for (ObjectError globalError : globalErrors) {
result.add(globalError.getObjectName());
}
return result;
}
public void setGlobalErrors(List<String> list) {
// Implemented only for XML serialization contract ...
}
public Map<String, String> getFieldErrors() {
final Map<String, String> result = new HashMap<String, String>();
final List<FieldError> fieldErrors = errors.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
result.put(fieldError.getField(), messageSource.getMessage(fieldError, Locale.ENGLISH));
}
return result;
}
public void setFieldErrors(Map<String, String> fieldErrors) {
// Implemented only for XML serialization contract ...
}
}

View File

@@ -1,5 +1,22 @@
package com.wisemapping.rest.view;
/*
* 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.rest.view;
import com.wisemapping.importer.ImportFormat;
import com.wisemapping.importer.Importer;

View File

@@ -1,5 +1,22 @@
package com.wisemapping.rest.view;
/*
* 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.rest.view;
import com.wisemapping.exporter.ExportFormat;
import com.wisemapping.exporter.ExportProperties;