rest api for label and hibernate mapping

This commit is contained in:
Ezequiel Bergamaschi 2014-01-25 23:21:17 -03:00 committed by Ezequiel Bergamaschi
parent 504e3e18e6
commit 1b480b566a
13 changed files with 305 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package com.wisemapping.dao;
import com.wisemapping.model.Label;
public interface LabelManager {
void addLabel(Label label);
void saveLabel(Label label);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.wisemapping.model.Label" table="LABEL">
<id name="id">
<generator class="increment"/>
</id>
<property name="title"/>
<many-to-one name="parent" column="parent_label_id" not-null="false"/>
<many-to-one name="creator" column="creator_id" unique="true" not-null="false" lazy="proxy"/>
</class>
</hibernate-mapping>

View File

@ -33,6 +33,7 @@
<value>com/wisemapping/model/CollaborationProperties.hbm.xml</value> <value>com/wisemapping/model/CollaborationProperties.hbm.xml</value>
<value>com/wisemapping/model/AccessAuditory.hbm.xml</value> <value>com/wisemapping/model/AccessAuditory.hbm.xml</value>
<value>com/wisemapping/model/MindMapHistory.hbm.xml</value> <value>com/wisemapping/model/MindMapHistory.hbm.xml</value>
<value>com/wisemapping/model/Label.hbm.xml</value>
</list> </list>
</property> </property>
<property name="hibernateProperties"> <property name="hibernateProperties">

View File

@ -13,5 +13,9 @@
<bean id="mindmapManager" class="com.wisemapping.dao.MindmapManagerImpl"> <bean id="mindmapManager" class="com.wisemapping.dao.MindmapManagerImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/> <property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean> </bean>
<bean id="labelManager" class="com.wisemapping.dao.LabelManagerImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
</beans> </beans>
</beans> </beans>

View File

@ -44,6 +44,7 @@
<value>com.wisemapping.rest.model.RestCollaborationList</value> <value>com.wisemapping.rest.model.RestCollaborationList</value>
<value>com.wisemapping.rest.model.RestLogItem</value> <value>com.wisemapping.rest.model.RestLogItem</value>
<value>com.wisemapping.rest.model.RestLockInfo</value> <value>com.wisemapping.rest.model.RestLockInfo</value>
<value>com.wisemapping.rest.model.RestLabel</value>
</list> </list>
</property> </property>
</bean> </bean>

View File

@ -55,6 +55,23 @@
<property name="target" ref="mindMapServiceTarget"/> <property name="target" ref="mindMapServiceTarget"/>
</bean> </bean>
<bean id="labelServiceTarget" class="com.wisemapping.service.LabelServiceImpl">
<property name="labelManager" ref="labelManager"/>
</bean>
<bean id="labelService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target">
<ref local="labelServiceTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}"/> <property name="host" value="${mail.smtp.host}"/>
<property name="port" value="${mail.smtp.port}"/> <property name="port" value="${mail.smtp.port}"/>