Author: tchemit Date: 2014-05-15 12:32:05 +0200 (Thu, 15 May 2014) New Revision: 3944 Url: http://forge.chorem.org/projects/pollen/repository/revisions/3944 Log: introduce converter package + a JsonConverter to replace the EntityConverter + add some bean for vote Added: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/TopiaEntityArrayConverter.java trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/TopiaEntityConverter.java trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteBean.java trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteToChoiceBean.java trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/ trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonConverterTest.java trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/TopiaEntityArrayConverterTest.java trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/TopiaEntityConverterTest.java Removed: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverter.java trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityConverter.java trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverterTest.java trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityConverterTest.java Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiApplicationListener.java trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteApi.java trunk/pollen-rest-api/src/main/resources/mapping trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiApplicationListener.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiApplicationListener.java 2014-05-15 10:23:42 UTC (rev 3943) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiApplicationListener.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -24,6 +24,10 @@ */ import org.chorem.pollen.persistence.PollenEntityEnum; +import org.chorem.pollen.rest.api.converter.JsonConverter; +import org.chorem.pollen.rest.api.converter.TopiaEntityArrayConverter; +import org.chorem.pollen.rest.api.v1.VoteBean; +import org.chorem.pollen.rest.api.v1.VoteToChoiceBean; import org.chorem.pollen.services.PollenService; import org.debux.webmotion.server.WebMotionServerListener; import org.debux.webmotion.server.call.Call; @@ -50,7 +54,7 @@ for (Class<? extends TopiaEntity> entityClass : PollenEntityEnum.getContractClasses()) { - TopiaEntityConverter<? extends TopiaEntity> converter = TopiaEntityConverter.newconverter(entityClass); + JsonConverter converter = JsonConverter.newEntityconverter(entityClass); serverContext.addConverter(converter, entityClass); TopiaEntityArrayConverter<? extends TopiaEntity> arrayConverter = TopiaEntityArrayConverter.newconverter(entityClass); @@ -58,6 +62,9 @@ } + serverContext.addConverter(JsonConverter.newConverter(VoteBean.class), VoteBean.class); + serverContext.addConverter(JsonConverter.newConverter(VoteToChoiceBean.class), VoteToChoiceBean.class); + // --- init injectors --- // serverContext.addInjector(new ExecutorParametersInjectorHandler.Injector() { Deleted: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverter.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverter.java 2014-05-15 10:23:42 UTC (rev 3943) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverter.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -1,88 +0,0 @@ -package org.chorem.pollen.rest.api; - -/* - * #%L - * Pollen :: Rest Api - * %% - * Copyright (C) 2009 - 2014 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.beanutils.converters.AbstractConverter; -import org.chorem.pollen.services.PollenTechnicalException; -import org.nuiton.topia.persistence.TopiaEntity; - -import java.io.IOException; -import java.lang.reflect.Array; - -/** - * Created on 5/7/14. - * - * @author Tony Chemit <chemit@codelutin.com> - * @since 2.0 - */ -public class TopiaEntityArrayConverter<E extends TopiaEntity> extends AbstractConverter { - - protected final Class<E[]> type; - - protected final ObjectMapper mapper; - - - public static <E extends TopiaEntity> TopiaEntityArrayConverter<E> newconverter(Class<E> entityType) { - return new TopiaEntityArrayConverter<>(entityType); - } - - public TopiaEntityArrayConverter(Class<E> entityType) { - this.type = getArrayClass(entityType); - this.mapper = Jsons.newEntityMapper(); - } - - <T> Class<T[]> getArrayClass(Class<T> clazz) { - return (Class<T[]>) Array.newInstance(clazz, 0).getClass(); - } - - @Override - protected <T> T convertToType(Class<T> type, Object value) throws Throwable { - - String stringValue; - - if (value instanceof String) { - - stringValue = (String) value; - - } else { - - stringValue = ((String[]) value)[0]; - - } - - try { - - E[] values = mapper.readValue(stringValue, this.type); - return (T) values; - - } catch (IOException e) { - throw new PollenTechnicalException("Could not convert " + stringValue + " to " + type.getName(), e); - } - - } - - @Override - protected Class<?> getDefaultType() { - return type; - } -} Deleted: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityConverter.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityConverter.java 2014-05-15 10:23:42 UTC (rev 3943) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityConverter.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -1,119 +0,0 @@ -package org.chorem.pollen.rest.api; - -/* - * #%L - * Pollen :: Rest Api - * %% - * Copyright (C) 2009 - 2014 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; -import org.apache.commons.beanutils.converters.AbstractConverter; -import org.chorem.pollen.persistence.PollenEntityEnum; -import org.chorem.pollen.services.PollenTechnicalException; -import org.nuiton.topia.persistence.TopiaEntity; - -import java.io.IOException; - -/** - * Created on 5/6/14. - * - * @author Tony Chemit <chemit@codelutin.com> - * @since 2.0 - */ -public class TopiaEntityConverter<E extends TopiaEntity> extends AbstractConverter { - - protected Class<E> entityType; - - protected Class<E> implementationClass; - - protected ObjectMapper mapper; - - public static <E extends TopiaEntity> TopiaEntityConverter<E> newconverter(Class<E> entityType) { - return new TopiaEntityConverter<>(entityType); - } - - public TopiaEntityConverter(Class<E> entityType) { - - this.entityType = entityType; - - this.implementationClass = PollenEntityEnum.getImplementationClass(entityType); - - SimpleModule module = new SimpleModule(); - - for (Class<? extends TopiaEntity> entityClass : PollenEntityEnum.getContractClasses()) { - - Class concreteClass = PollenEntityEnum.getImplementationClass(entityClass); - module.addAbstractTypeMapping(entityClass, concreteClass); - - } - - mapper = Jsons.newEntityMapper(); - - } - - public ObjectMapper getMapper() { - - return mapper; - - } - - @Override - protected String convertToString(Object value) throws Throwable { - - String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value); - return result; - - } - - @Override - protected <T> T convertToType(Class<T> type, Object value) throws Throwable { - - String stringValue; - - if (value instanceof String) { - - stringValue = (String) value; - - } else { - - stringValue = ((String[]) value)[0]; - - } - - try { - - T result = (T) mapper.readValue(stringValue, implementationClass); - - return result; - - } catch (IOException e) { - throw new PollenTechnicalException("Could not convert " + stringValue + " to " + type.getName(), e); - } - - } - - @Override - protected Class<E> getDefaultType() { - - return entityType; - - } - - -} Copied: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java (from rev 3938, trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityConverter.java) =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java (rev 0) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -0,0 +1,135 @@ +package org.chorem.pollen.rest.api.converter; + +/* + * #%L + * Pollen :: Rest Api + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import org.apache.commons.beanutils.converters.AbstractConverter; +import org.chorem.pollen.persistence.PollenEntityEnum; +import org.chorem.pollen.rest.api.Jsons; +import org.chorem.pollen.services.PollenTechnicalException; +import org.nuiton.topia.persistence.TopiaEntity; + +import java.io.IOException; + +/** + * Created on 5/6/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class JsonConverter<O> extends AbstractConverter { + + protected Class<O> entityType; + + protected Class<O> implementationClass; + + protected ObjectMapper mapper; + + public static <O> JsonConverter<O> newConverter(Class<O> entityType) { + return newConverter(entityType, entityType); + } + + public static <E extends TopiaEntity> JsonConverter<E> newEntityconverter(Class<E> entityType) { + Class<E> implementationClass = PollenEntityEnum.getImplementationClass(entityType); + return newConverter(entityType, implementationClass); + } + + public static <O> JsonConverter<O> newConverter(Class<O> entityType, Class<O> implementationClass) { + return new JsonConverter<>(entityType, implementationClass); + } + + public JsonConverter(Class<O> entityType, Class<O> implementationClass) { + + this.entityType = entityType; + + this.implementationClass = implementationClass; + + SimpleModule module = new SimpleModule(); + + if (entityType != implementationClass) { + + module.addAbstractTypeMapping(entityType, implementationClass); + + } + + for (Class<? extends TopiaEntity> entityClass : PollenEntityEnum.getContractClasses()) { + + Class concreteClass = PollenEntityEnum.getImplementationClass(entityClass); + module.addAbstractTypeMapping(entityClass, concreteClass); + + } + + mapper = Jsons.newEntityMapper(); + + } + + public ObjectMapper getMapper() { + + return mapper; + + } + + @Override + protected String convertToString(Object value) throws Throwable { + + String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value); + return result; + + } + + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + + String stringValue; + + if (value instanceof String) { + + stringValue = (String) value; + + } else { + + stringValue = ((String[]) value)[0]; + + } + + try { + + T result = (T) mapper.readValue(stringValue, implementationClass); + + return result; + + } catch (IOException e) { + throw new PollenTechnicalException("Could not convert " + stringValue + " to " + type.getName(), e); + } + + } + + @Override + protected Class<O> getDefaultType() { + + return entityType; + + } + + +} Copied: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/TopiaEntityArrayConverter.java (from rev 3938, trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverter.java) =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/TopiaEntityArrayConverter.java (rev 0) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/TopiaEntityArrayConverter.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -0,0 +1,88 @@ +package org.chorem.pollen.rest.api.converter; + +/* + * #%L + * Pollen :: Rest Api + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.beanutils.converters.AbstractConverter; +import org.chorem.pollen.rest.api.Jsons; +import org.chorem.pollen.services.PollenTechnicalException; +import org.nuiton.topia.persistence.TopiaEntity; + +import java.io.IOException; +import java.lang.reflect.Array; + +/** + * Created on 5/7/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class TopiaEntityArrayConverter<E extends TopiaEntity> extends AbstractConverter { + + protected final Class<E[]> type; + + protected final ObjectMapper mapper; + + public static <E extends TopiaEntity> TopiaEntityArrayConverter<E> newconverter(Class<E> entityType) { + return new TopiaEntityArrayConverter<>(entityType); + } + + public TopiaEntityArrayConverter(Class<E> entityType) { + this.type = getArrayClass(entityType); + this.mapper = Jsons.newEntityMapper(); + } + + <T> Class<T[]> getArrayClass(Class<T> clazz) { + return (Class<T[]>) Array.newInstance(clazz, 0).getClass(); + } + + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + + String stringValue; + + if (value instanceof String) { + + stringValue = (String) value; + + } else { + + stringValue = ((String[]) value)[0]; + + } + + try { + + E[] values = mapper.readValue(stringValue, this.type); + return (T) values; + + } catch (IOException e) { + throw new PollenTechnicalException("Could not convert " + stringValue + " to " + type.getName(), e); + } + + } + + @Override + public Class<?> getDefaultType() { + return type; + } +} Copied: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/TopiaEntityConverter.java (from rev 3938, trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/TopiaEntityConverter.java) =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/TopiaEntityConverter.java (rev 0) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/TopiaEntityConverter.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -0,0 +1,120 @@ +package org.chorem.pollen.rest.api.converter; + +/* + * #%L + * Pollen :: Rest Api + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import org.apache.commons.beanutils.converters.AbstractConverter; +import org.chorem.pollen.persistence.PollenEntityEnum; +import org.chorem.pollen.rest.api.Jsons; +import org.chorem.pollen.services.PollenTechnicalException; +import org.nuiton.topia.persistence.TopiaEntity; + +import java.io.IOException; + +/** + * Created on 5/6/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class TopiaEntityConverter<E extends TopiaEntity> extends AbstractConverter { + + protected Class<E> entityType; + + protected Class<E> implementationClass; + + protected ObjectMapper mapper; + + public static <E extends TopiaEntity> TopiaEntityConverter<E> newconverter(Class<E> entityType) { + return new TopiaEntityConverter<>(entityType); + } + + public TopiaEntityConverter(Class<E> entityType) { + + this.entityType = entityType; + + this.implementationClass = PollenEntityEnum.getImplementationClass(entityType); + + SimpleModule module = new SimpleModule(); + + for (Class<? extends TopiaEntity> entityClass : PollenEntityEnum.getContractClasses()) { + + Class concreteClass = PollenEntityEnum.getImplementationClass(entityClass); + module.addAbstractTypeMapping(entityClass, concreteClass); + + } + + mapper = Jsons.newEntityMapper(); + + } + + public ObjectMapper getMapper() { + + return mapper; + + } + + @Override + protected String convertToString(Object value) throws Throwable { + + String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value); + return result; + + } + + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + + String stringValue; + + if (value instanceof String) { + + stringValue = (String) value; + + } else { + + stringValue = ((String[]) value)[0]; + + } + + try { + + T result = (T) mapper.readValue(stringValue, implementationClass); + + return result; + + } catch (IOException e) { + throw new PollenTechnicalException("Could not convert " + stringValue + " to " + type.getName(), e); + } + + } + + @Override + protected Class<E> getDefaultType() { + + return entityType; + + } + + +} Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteApi.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteApi.java 2014-05-15 10:23:42 UTC (rev 3943) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteApi.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -29,6 +29,7 @@ import org.chorem.pollen.services.service.VoteService; import org.debux.webmotion.server.WebMotionController; +import java.util.ArrayList; import java.util.List; /** @@ -39,33 +40,47 @@ */ public class VoteApi extends WebMotionController { - public List<Vote> getVotes(VoteService voteService, String pollId) { + public List<VoteBean> getVotes(VoteService voteService, String pollId) { List<Vote> votes = voteService.getVotes(pollId); - return votes; + List<VoteBean> votesBean = new ArrayList<>(); + for (Vote vote : votes) { + VoteBean voteBean = VoteBean.fromEntity(vote); + votesBean.add(voteBean); + + } + + return votesBean; + } - public Vote getVote(VoteService voteService, String pollId, String voteId) { + public VoteBean getVote(VoteService voteService, String pollId, String voteId) { Vote vote = voteService.getVote(pollId, voteId); - return vote; + VoteBean voteBean = VoteBean.fromEntity(vote); + return voteBean; } - public PollenPrincipalRef<Vote> addVote(VoteService voteService, String pollId, Vote vote) throws InvalidFormException { + public PollenPrincipalRef<Vote> addVote(VoteService voteService, String pollId, VoteBean vote) throws InvalidFormException { - Vote createdVote = voteService.addVote(pollId, vote); + Vote entity = vote.toEntity(); + + Vote createdVote = voteService.addVote(pollId, entity); PollenPrincipalRef<Vote> principalRef = PollenPrincipalRef.newRef(createdVote); return principalRef; } - public Vote editVote(VoteService voteService, String pollId, Vote vote) throws InvalidFormException { + public VoteBean editVote(VoteService voteService, String pollId, VoteBean vote) throws InvalidFormException { - Vote editedVote = voteService.editVote(pollId, vote); - return editedVote; + Vote entity = vote.toEntity(); + Vote editedVote = voteService.editVote(pollId, entity); + VoteBean editedVoteBean = VoteBean.fromEntity(editedVote); + return editedVoteBean; + } public void deleteVote(VoteService voteService, String pollId, String voteId) { Added: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteBean.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteBean.java (rev 0) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteBean.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -0,0 +1,80 @@ +package org.chorem.pollen.rest.api.v1; + +import org.chorem.pollen.persistence.entity.PollenPrincipal; +import org.chorem.pollen.persistence.entity.Vote; +import org.chorem.pollen.persistence.entity.VoteImpl; +import org.chorem.pollen.persistence.entity.VoteToChoice; + +import java.util.HashSet; +import java.util.Set; + +/** + * Created on 5/15/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class VoteBean { + + protected PollenPrincipal voter; + + protected Boolean anonymous; + + protected Set<VoteToChoiceBean> choice = new HashSet<>(); + + public PollenPrincipal getVoter() { + return voter; + } + + public void setVoter(PollenPrincipal voter) { + this.voter = voter; + } + + public Boolean getAnonymous() { + return anonymous; + } + + public void setAnonymous(Boolean anonymous) { + this.anonymous = anonymous; + } + + public Set<VoteToChoiceBean> getChoice() { + return choice; + } + + public void addChoice(VoteToChoiceBean choice) { + this.choice.add(choice); + } + + public static VoteBean fromEntity(Vote vote) { + + VoteBean voteBean = new VoteBean(); + voteBean.setAnonymous(vote.isAnonymous()); + + for (VoteToChoice voteToChoice : vote.getVoteToChoice()) { + + VoteToChoiceBean choiceBean = new VoteToChoiceBean(); + choiceBean.setChoiceId(voteToChoice.getChoice().getTopiaId()); + choiceBean.setVoteValue(voteToChoice.getVoteValue()); + voteBean.addChoice(choiceBean); + + } + + return voteBean; + + } + + public Vote toEntity() { + + Vote vote = new VoteImpl(); + vote.setAnonymous(getAnonymous() != null && getAnonymous()); + for (VoteToChoiceBean choiceBean : choice) { + + VoteToChoice voteToChoice = choiceBean.toEntity(); + vote.addVoteToChoice(voteToChoice); + + } + return vote; + + } +} Property changes on: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteBean.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteToChoiceBean.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteToChoiceBean.java (rev 0) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteToChoiceBean.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -0,0 +1,45 @@ +package org.chorem.pollen.rest.api.v1; + +import org.chorem.pollen.persistence.entity.ChoiceImpl; +import org.chorem.pollen.persistence.entity.VoteToChoice; +import org.chorem.pollen.persistence.entity.VoteToChoiceImpl; + +/** + * Created on 5/15/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class VoteToChoiceBean { + + protected String choiceId; + + protected Double voteValue; + + public String getChoiceId() { + return choiceId; + } + + public void setChoiceId(String choiceId) { + this.choiceId = choiceId; + } + + public Double getVoteValue() { + return voteValue; + } + + public void setVoteValue(Double voteValue) { + this.voteValue = voteValue; + } + + public VoteToChoice toEntity() { + + VoteToChoice voteToChoice = new VoteToChoiceImpl(); + voteToChoice.setVoteValue(getVoteValue()); + voteToChoice.setChoice(new ChoiceImpl()); + voteToChoice.getChoice().setTopiaId(getChoiceId()); + + return voteToChoice; + + } +} Property changes on: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteToChoiceBean.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/pollen-rest-api/src/main/resources/mapping =================================================================== --- trunk/pollen-rest-api/src/main/resources/mapping 2014-05-15 10:23:42 UTC (rev 3943) +++ trunk/pollen-rest-api/src/main/resources/mapping 2014-05-15 10:32:05 UTC (rev 3944) @@ -119,6 +119,8 @@ # VoteApi GET /v1/polls/{pollId}/votes VoteApi.getVotes +GET /v1/polls/{pollId}/votes/add VoteApi.addVote +GET /v1/polls/{pollId}/votes/{voteId}/edit VoteApi.editVote POST /v1/polls/{pollId}/votes VoteApi.addVote GET /v1/polls/{pollId}/votes/{voteId} VoteApi.getVote PUT /v1/polls/{pollId}/votes/{voteId} VoteApi.editVote Deleted: trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverterTest.java =================================================================== --- trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverterTest.java 2014-05-15 10:23:42 UTC (rev 3943) +++ trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverterTest.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -1,54 +0,0 @@ -package org.chorem.pollen.rest.api; - -/* - * #%L - * Pollen :: Rest Api - * %% - * Copyright (C) 2009 - 2014 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -import org.chorem.pollen.persistence.entity.Choice; -import org.chorem.pollen.persistence.entity.ChoiceType; -import org.junit.Assert; -import org.junit.Test; - -public class TopiaEntityArrayConverterTest { - - @Test - public void convertToType() throws Exception { - - String choicesStr = "[ {name: \"choice1\", choiceType: \"TEXT\", choiceOrder: 1}, " + - "{name: \"choice2\", choiceType: \"IMAGE\", choiceOrder: 2} ]"; - - TopiaEntityArrayConverter converter = TopiaEntityArrayConverter.newconverter(Choice.class); - - Choice[] choices = converter.convert(Choice[].class, choicesStr); - Assert.assertNotNull(choices); - Assert.assertEquals(2, choices.length); - - Assert.assertNotNull(choices[0]); - Assert.assertEquals("choice1", choices[0].getName()); - Assert.assertEquals(ChoiceType.TEXT, choices[0].getChoiceType()); - Assert.assertEquals(1, choices[0].getChoiceOrder()); - - Assert.assertNotNull(choices[1]); - Assert.assertEquals("choice2", choices[1].getName()); - Assert.assertEquals(ChoiceType.IMAGE, choices[1].getChoiceType()); - Assert.assertEquals(2, choices[1].getChoiceOrder()); - - } -} \ No newline at end of file Deleted: trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityConverterTest.java =================================================================== --- trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityConverterTest.java 2014-05-15 10:23:42 UTC (rev 3943) +++ trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityConverterTest.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -1,61 +0,0 @@ -package org.chorem.pollen.rest.api; - -/* - * #%L - * Pollen :: Rest Api - * %% - * Copyright (C) 2009 - 2014 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -import org.chorem.pollen.persistence.entity.CommentVisibility; -import org.chorem.pollen.persistence.entity.Poll; -import org.chorem.pollen.persistence.entity.PollType; -import org.chorem.pollen.persistence.entity.VoteVisibility; -import org.junit.Assert; -import org.junit.Test; - -public class TopiaEntityConverterTest { - - @Test - public void convert() { - - String pollStr = "{topiaCreateDate: 1399033089600, title: \"pollTitle\", " + - "choice: [ {name: \"choice1\", choiceType: \"TEXT\"}, " + - "{name: \"choice2\", choiceType: \"IMAGE\"} ], voteVisibility: \"ANONYMOUS\", " + - "commentVisibility:\"NOBODY\", pollType: \"FREE\" }"; - - TopiaEntityConverter<Poll> converter = new TopiaEntityConverter<>(Poll.class); - - Poll poll = converter.convert(Poll.class, pollStr); - - Assert.assertNotNull(poll); - Assert.assertEquals("pollTitle", poll.getTitle()); - Assert.assertEquals(VoteVisibility.ANONYMOUS, poll.getVoteVisibility()); - Assert.assertEquals(CommentVisibility.NOBODY, poll.getCommentVisibility()); - Assert.assertEquals(PollType.FREE, poll.getPollType()); -// Assert.assertEquals(2, poll.sizeChoice()); -// Assert.assertEquals("choice1", poll.getChoice(0).getName()); -// Assert.assertEquals(ChoiceType.TEXT, poll.getChoice(0).getChoiceType()); -// Assert.assertEquals("choice2", poll.getChoice(1).getName()); -// Assert.assertEquals(ChoiceType.IMAGE, poll.getChoice(1).getChoiceType()); - - String convert = converter.convert(String.class, poll); - Assert.assertNotNull(convert); - - } - -} \ No newline at end of file Added: trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonConverterTest.java =================================================================== --- trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonConverterTest.java (rev 0) +++ trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonConverterTest.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -0,0 +1,90 @@ +package org.chorem.pollen.rest.api.converter; + +/* + * #%L + * Pollen :: Rest Api + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import org.chorem.pollen.persistence.entity.CommentVisibility; +import org.chorem.pollen.persistence.entity.Poll; +import org.chorem.pollen.persistence.entity.PollType; +import org.chorem.pollen.persistence.entity.VoteVisibility; +import org.chorem.pollen.rest.api.v1.VoteBean; +import org.chorem.pollen.rest.api.v1.VoteToChoiceBean; +import org.junit.Assert; +import org.junit.Test; + +public class JsonConverterTest { + + @Test + public void convertBean() { + + String pollStr = "{voter:{name:\"toto\"},anonymous:true,choice:[{voteValue:1.5,choiceId:\"AA\"}]}"; + + JsonConverter<VoteBean> converter = JsonConverter.newConverter(VoteBean.class); + + VoteBean voteBean = converter.convert(VoteBean.class, pollStr); + + Assert.assertNotNull(voteBean); + Assert.assertEquals(true, voteBean.getAnonymous()); + + Assert.assertNotNull(voteBean.getVoter()); + Assert.assertEquals("toto", voteBean.getVoter().getName()); + + Assert.assertNotNull(voteBean.getChoice()); + Assert.assertEquals(1, voteBean.getChoice().size()); + VoteToChoiceBean voteToChoiceBean = voteBean.getChoice().iterator().next(); + Assert.assertNotNull(voteToChoiceBean); + Assert.assertEquals(1.5, voteToChoiceBean.getVoteValue(), 0); + Assert.assertEquals("AA", voteToChoiceBean.getChoiceId()); + + String convert = converter.convert(String.class, voteBean); + Assert.assertNotNull(convert); + + } + + @Test + public void convertEntity() { + + String pollStr = "{topiaCreateDate: 1399033089600, title: \"pollTitle\", " + + "choice: [ {name: \"choice1\", choiceType: \"TEXT\"}, " + + "{name: \"choice2\", choiceType: \"IMAGE\"} ], voteVisibility: \"ANONYMOUS\", " + + "commentVisibility:\"NOBODY\", pollType: \"FREE\" }"; + + TopiaEntityConverter<Poll> converter = new TopiaEntityConverter<>(Poll.class); + + Poll poll = converter.convert(Poll.class, pollStr); + + Assert.assertNotNull(poll); + Assert.assertEquals("pollTitle", poll.getTitle()); + Assert.assertEquals(VoteVisibility.ANONYMOUS, poll.getVoteVisibility()); + Assert.assertEquals(CommentVisibility.NOBODY, poll.getCommentVisibility()); + Assert.assertEquals(PollType.FREE, poll.getPollType()); +// Assert.assertEquals(2, poll.sizeChoice()); +// Assert.assertEquals("choice1", poll.getChoice(0).getName()); +// Assert.assertEquals(ChoiceType.TEXT, poll.getChoice(0).getChoiceType()); +// Assert.assertEquals("choice2", poll.getChoice(1).getName()); +// Assert.assertEquals(ChoiceType.IMAGE, poll.getChoice(1).getChoiceType()); + + String convert = converter.convert(String.class, poll); + Assert.assertNotNull(convert); + + } + +} \ No newline at end of file Property changes on: trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonConverterTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Copied: trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/TopiaEntityArrayConverterTest.java (from rev 3938, trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityArrayConverterTest.java) =================================================================== --- trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/TopiaEntityArrayConverterTest.java (rev 0) +++ trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/TopiaEntityArrayConverterTest.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -0,0 +1,54 @@ +package org.chorem.pollen.rest.api.converter; + +/* + * #%L + * Pollen :: Rest Api + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import org.chorem.pollen.persistence.entity.Choice; +import org.chorem.pollen.persistence.entity.ChoiceType; +import org.junit.Assert; +import org.junit.Test; + +public class TopiaEntityArrayConverterTest { + + @Test + public void convertToType() throws Exception { + + String choicesStr = "[ {name: \"choice1\", choiceType: \"TEXT\", choiceOrder: 1}, " + + "{name: \"choice2\", choiceType: \"IMAGE\", choiceOrder: 2} ]"; + + TopiaEntityArrayConverter converter = TopiaEntityArrayConverter.newconverter(Choice.class); + + Choice[] choices = converter.convert(Choice[].class, choicesStr); + Assert.assertNotNull(choices); + Assert.assertEquals(2, choices.length); + + Assert.assertNotNull(choices[0]); + Assert.assertEquals("choice1", choices[0].getName()); + Assert.assertEquals(ChoiceType.TEXT, choices[0].getChoiceType()); + Assert.assertEquals(1, choices[0].getChoiceOrder()); + + Assert.assertNotNull(choices[1]); + Assert.assertEquals("choice2", choices[1].getName()); + Assert.assertEquals(ChoiceType.IMAGE, choices[1].getChoiceType()); + Assert.assertEquals(2, choices[1].getChoiceOrder()); + + } +} \ No newline at end of file Copied: trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/TopiaEntityConverterTest.java (from rev 3938, trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/TopiaEntityConverterTest.java) =================================================================== --- trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/TopiaEntityConverterTest.java (rev 0) +++ trunk/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/TopiaEntityConverterTest.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -0,0 +1,61 @@ +package org.chorem.pollen.rest.api.converter; + +/* + * #%L + * Pollen :: Rest Api + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import org.chorem.pollen.persistence.entity.CommentVisibility; +import org.chorem.pollen.persistence.entity.Poll; +import org.chorem.pollen.persistence.entity.PollType; +import org.chorem.pollen.persistence.entity.VoteVisibility; +import org.junit.Assert; +import org.junit.Test; + +public class TopiaEntityConverterTest { + + @Test + public void convert() { + + String pollStr = "{topiaCreateDate: 1399033089600, title: \"pollTitle\", " + + "choice: [ {name: \"choice1\", choiceType: \"TEXT\"}, " + + "{name: \"choice2\", choiceType: \"IMAGE\"} ], voteVisibility: \"ANONYMOUS\", " + + "commentVisibility:\"NOBODY\", pollType: \"FREE\" }"; + + TopiaEntityConverter<Poll> converter = new TopiaEntityConverter<>(Poll.class); + + Poll poll = converter.convert(Poll.class, pollStr); + + Assert.assertNotNull(poll); + Assert.assertEquals("pollTitle", poll.getTitle()); + Assert.assertEquals(VoteVisibility.ANONYMOUS, poll.getVoteVisibility()); + Assert.assertEquals(CommentVisibility.NOBODY, poll.getCommentVisibility()); + Assert.assertEquals(PollType.FREE, poll.getPollType()); +// Assert.assertEquals(2, poll.sizeChoice()); +// Assert.assertEquals("choice1", poll.getChoice(0).getName()); +// Assert.assertEquals(ChoiceType.TEXT, poll.getChoice(0).getChoiceType()); +// Assert.assertEquals("choice2", poll.getChoice(1).getName()); +// Assert.assertEquals(ChoiceType.IMAGE, poll.getChoice(1).getChoiceType()); + + String convert = converter.convert(String.class, poll); + Assert.assertNotNull(convert); + + } + +} \ No newline at end of file Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java 2014-05-15 10:23:42 UTC (rev 3943) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java 2014-05-15 10:32:05 UTC (rev 3944) @@ -147,6 +147,7 @@ Poll poll = getPollService().getPoll0(pollId); Choice choice = getChoice(poll, choiceId); + //TODO Check if this is not the last choice, if so can not delete getChoiceDao().delete(choice); commit();