diff --git a/wise-webapp/src/main/java/com/wisemapping/dao/LabelManager.java b/wise-webapp/src/main/java/com/wisemapping/dao/LabelManager.java new file mode 100644 index 00000000..b6941d7b --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/dao/LabelManager.java @@ -0,0 +1,10 @@ +package com.wisemapping.dao; + +import com.wisemapping.model.Label; + +public interface LabelManager { + + void addLabel(Label label); + + void saveLabel(Label label); +} diff --git a/wise-webapp/src/main/java/com/wisemapping/dao/LabelManagerImpl.java b/wise-webapp/src/main/java/com/wisemapping/dao/LabelManagerImpl.java new file mode 100644 index 00000000..657d9423 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/dao/LabelManagerImpl.java @@ -0,0 +1,19 @@ +package com.wisemapping.dao; + +import com.wisemapping.model.Label; +import org.jetbrains.annotations.NotNull; +import org.springframework.orm.hibernate3.support.HibernateDaoSupport; + +public class LabelManagerImpl extends HibernateDaoSupport + implements LabelManager { + + @Override + public void addLabel(Label label) { + saveLabel(label); + } + + @Override + public void saveLabel(@NotNull Label label) { + getSession().save(label); + } +} diff --git a/wise-webapp/src/main/java/com/wisemapping/model/Label.java b/wise-webapp/src/main/java/com/wisemapping/model/Label.java new file mode 100644 index 00000000..ccc04f90 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/model/Label.java @@ -0,0 +1,49 @@ +package com.wisemapping.model; + + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class Label { + + //~ Instance fields ...................................................................................... + private int id; + @NotNull private String title; + @NotNull private User creator; + @Nullable private Label parent; + + public void setParent(@Nullable Label parent) { + this.parent = parent; + } + + @Nullable + public Label getParent() { + return parent; + } + + public void setCreator(@NotNull User creator) { + this.creator = creator; + } + + @NotNull + public User getCreator() { + return creator; + } + + @NotNull + public String getTitle() { + return title; + } + + public void setTitle(@NotNull String title) { + this.title = title; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/LabelController.java b/wise-webapp/src/main/java/com/wisemapping/rest/LabelController.java new file mode 100644 index 00000000..046ae8f6 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/rest/LabelController.java @@ -0,0 +1,60 @@ +package com.wisemapping.rest; + +import com.wisemapping.exceptions.WiseMappingException; +import com.wisemapping.model.Label; +import com.wisemapping.model.User; +import com.wisemapping.rest.model.RestLabel; +import com.wisemapping.security.Utils; +import com.wisemapping.service.LabelService; +import com.wisemapping.validator.LabelValidator; +import org.jetbrains.annotations.NotNull; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +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.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; + +import javax.servlet.http.HttpServletResponse; + +@Controller +public class LabelController extends BaseController { + + @Qualifier("labelService") + @Autowired + private LabelService labelService; + + + @RequestMapping(method = RequestMethod.POST, value = "/labels", consumes = {"application/json"}) + @ResponseStatus(value = HttpStatus.CREATED) + public void createLabel(@RequestBody RestLabel restLabel, @NotNull HttpServletResponse response, @RequestParam(required = false) String title) throws WiseMappingException { + // Overwrite title if it was specified by parameter. + if (title != null && !title.isEmpty()) { + restLabel.setTitle(title); + } + + final Label label = restLabel.getDelegated(); + + // Validate ... + final BindingResult result = new BeanPropertyBindingResult(restLabel, ""); + new LabelValidator().validate(label, result); + if (result.hasErrors()) { + + throw new ValidationException(result); + } + + // Add new label ... + final User user = Utils.getUser(); + assert user != null; + labelService.addLabel(label, user); + + // Return the new created map ... + response.setHeader("ResourceId", Integer.toString(label.getId())); + } + +} diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/model/RestLabel.java b/wise-webapp/src/main/java/com/wisemapping/rest/model/RestLabel.java new file mode 100644 index 00000000..57760fab --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/rest/model/RestLabel.java @@ -0,0 +1,59 @@ +package com.wisemapping.rest.model; + +import com.wisemapping.model.Label; +import org.codehaus.jackson.annotate.JsonAutoDetect; +import org.codehaus.jackson.annotate.JsonIgnore; +import org.jetbrains.annotations.NotNull; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; +import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE; +import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY; + +@XmlRootElement(name = "label") +@XmlAccessorType(XmlAccessType.PROPERTY) +@JsonAutoDetect( + fieldVisibility = NONE, + setterVisibility = PUBLIC_ONLY, + isGetterVisibility = NONE, + getterVisibility = PUBLIC_ONLY +) +public class RestLabel { + + @JsonIgnore + private Label label; + + public RestLabel() { + this(new Label()); + } + + + public RestLabel(@NotNull final Label label) { + this.label = label; + } + + public String getTitle() { + return this.label.getTitle(); + } + + public int getId() { + return label.getId(); + } + + public String getCreator() { + return this.label.getCreator().getEmail(); + } + + public void setId(int id) { + label.setId(id); + } + + public void setTitle(String title) { + label.setTitle(title); + } + + @JsonIgnore + public Label getDelegated() { + return label; + } +} diff --git a/wise-webapp/src/main/java/com/wisemapping/service/LabelService.java b/wise-webapp/src/main/java/com/wisemapping/service/LabelService.java new file mode 100644 index 00000000..0a85d757 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/service/LabelService.java @@ -0,0 +1,12 @@ +package com.wisemapping.service; + +import com.wisemapping.exceptions.WiseMappingException; +import com.wisemapping.model.Label; +import com.wisemapping.model.User; +import org.jetbrains.annotations.NotNull; + +public interface LabelService { + + void addLabel(@NotNull final Label label, @NotNull final User user) throws WiseMappingException; + +} diff --git a/wise-webapp/src/main/java/com/wisemapping/service/LabelServiceImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/LabelServiceImpl.java new file mode 100644 index 00000000..4e4437da --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/service/LabelServiceImpl.java @@ -0,0 +1,24 @@ +package com.wisemapping.service; + +import com.wisemapping.dao.LabelManager; +import com.wisemapping.exceptions.WiseMappingException; +import com.wisemapping.model.Label; +import com.wisemapping.model.User; +import org.jetbrains.annotations.NotNull; + +public class LabelServiceImpl implements LabelService { + + private LabelManager labelManager; + + public void setLabelManager(LabelManager labelManager) { + this.labelManager = labelManager; + } + + @Override + public void addLabel(@NotNull final Label label, @NotNull final User user) throws WiseMappingException { + + label.setCreator(user); + labelManager.addLabel(label); + } + +} diff --git a/wise-webapp/src/main/java/com/wisemapping/validator/LabelValidator.java b/wise-webapp/src/main/java/com/wisemapping/validator/LabelValidator.java new file mode 100644 index 00000000..ed920fcb --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/validator/LabelValidator.java @@ -0,0 +1,32 @@ +package com.wisemapping.validator; + +import com.wisemapping.model.Label; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.springframework.validation.Errors; +import org.springframework.validation.ValidationUtils; +import org.springframework.validation.Validator; + +public class LabelValidator implements Validator { + @Override + public boolean supports(Class clazz) { + return clazz.equals(Label.class); + } + + @Override + public void validate(@Nullable final Object target, @NotNull final Errors errors) { + final Label label = (Label) target; + if (label == null) { + errors.rejectValue("map", "error.not-specified", null, "Value required."); + } else { + validateLabel(label, errors); + + } + } + + private void validateLabel(@NotNull final Label label, @NotNull final Errors errors) { + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", Messages.FIELD_REQUIRED); + String title = label.getTitle(); + //todo hacer otras validaciones como si supera el maximo o el label existe + } +} diff --git a/wise-webapp/src/main/resources/com/wisemapping/model/Label.hbm.xml b/wise-webapp/src/main/resources/com/wisemapping/model/Label.hbm.xml new file mode 100644 index 00000000..71ff0bc9 --- /dev/null +++ b/wise-webapp/src/main/resources/com/wisemapping/model/Label.hbm.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-dao.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-dao.xml index 0ba373f6..4f613d66 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-dao.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-dao.xml @@ -33,6 +33,7 @@ com/wisemapping/model/CollaborationProperties.hbm.xml com/wisemapping/model/AccessAuditory.hbm.xml com/wisemapping/model/MindMapHistory.hbm.xml + com/wisemapping/model/Label.hbm.xml diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-model.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-model.xml index 97e97080..cedd25dd 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-model.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-model.xml @@ -13,5 +13,9 @@ + + + + \ No newline at end of file diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml index 53384828..3271da01 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml @@ -44,6 +44,7 @@ com.wisemapping.rest.model.RestCollaborationList com.wisemapping.rest.model.RestLogItem com.wisemapping.rest.model.RestLockInfo + com.wisemapping.rest.model.RestLabel diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-service.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-service.xml index 8ebbd33f..923939a0 100755 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-service.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-service.xml @@ -55,6 +55,23 @@ + + + + + + + + + + + + + PROPAGATION_REQUIRED + + + +