This is an automated email from the git hooks/post-receive script. unknown user pushed a commit to branch devel in repository Pollen. commit 9c9cf913a7a41448679d91fb405cf9a96bc99546 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sat May 24 14:51:13 2014 +0200 introduce json helper + use a PollenEntityRef --- .../persistence/PollenTopiaApplicationContext.java | 21 ++ .../pollen/persistence/entity/CommentTopiaDao.java | 21 ++ .../pollen/persistence/entity/VoteToChoices.java | 21 ++ .../pollen/persistence/entity/VoteTopiaDao.java | 21 ++ .../org/chorem/pollen/rest/api/JsonHelper.java | 254 +++++++++++++++++++++ .../org/chorem/pollen/rest/api/PollenRender.java | 50 +--- .../chorem/pollen/rest/api/PollenRestApiUtil.java | 145 ++---------- .../pollen/rest/api/converter/DateConverter.java | 21 ++ .../rest/api/converter/JsonArrayConverter.java | 43 +++- .../pollen/rest/api/converter/JsonConverter.java | 15 +- .../rest/api/injector/PollenBeanIdInjector.java | 21 ++ .../PollenRestApiRequestContextInjector.java | 21 ++ .../rest/api/injector/PollenServiceInjector.java | 21 ++ .../org/chorem/pollen/rest/api/v1/AuthApi.java | 7 +- .../org/chorem/pollen/rest/api/v1/ChoiceApi.java | 6 +- .../org/chorem/pollen/rest/api/v1/CommentApi.java | 6 +- .../chorem/pollen/rest/api/v1/FavoriteListApi.java | 10 +- .../org/chorem/pollen/rest/api/v1/PollApi.java | 12 +- .../chorem/pollen/rest/api/v1/PollenUserApi.java | 10 +- .../org/chorem/pollen/rest/api/v1/VoteApi.java | 6 +- .../chorem/pollen/rest/api/v1/VoterListApi.java | 6 +- .../pollen/rest/api/AbstractPollenRestApiTest.java | 14 ++ .../org/chorem/pollen/rest/api/AuthApiTest.java | 35 ++- .../org/chorem/pollen/rest/api/PollApiTest.java | 33 ++- .../chorem/pollen/rest/api/PollenUserApiTest.java | 19 +- .../rest/api/converter/JsonArrayConverterTest.java | 21 ++ .../chorem/pollen/services/bean/ChoiceBean.java | 21 ++ .../chorem/pollen/services/bean/CommentBean.java | 21 ++ .../chorem/pollen/services/bean/CreateBeanRef.java | 105 --------- .../pollen/services/bean/FavoriteListBean.java | 21 ++ .../services/bean/FavoriteListMemberBean.java | 21 ++ .../org/chorem/pollen/services/bean/PollBean.java | 21 ++ .../chorem/pollen/services/bean/PollenBean.java | 41 +++- .../pollen/services/bean/PollenEntityId.java | 21 ++ .../pollen/services/bean/PollenEntityRef.java | 129 +++++++++++ .../pollen/services/bean/PollenUserBean.java | 21 ++ .../org/chorem/pollen/services/bean/VoteBean.java | 21 ++ .../pollen/services/bean/VoteToChoiceBean.java | 21 ++ .../chorem/pollen/services/bean/VoterListBean.java | 21 ++ .../pollen/services/bean/VoterListMemberBean.java | 21 ++ .../chorem/pollen/services/bean/package-info.java | 4 +- .../pollen/services/service/ChoiceService.java | 6 +- .../pollen/services/service/CommentService.java | 6 +- .../services/service/FavoriteListService.java | 12 +- .../pollen/services/service/FixturesService.java | 6 +- .../pollen/services/service/PollService.java | 6 +- .../pollen/services/service/PollenUserService.java | 6 +- .../pollen/services/service/VoteService.java | 6 +- .../pollen/services/service/VoterListService.java | 6 +- .../services/service/security/SecurityService.java | 7 +- .../pollen/services/AbstractPollenServiceTest.java | 5 +- .../pollen/services/service/PollServiceTest.java | 69 +++--- .../services/service/PollenUserServiceTest.java | 10 +- .../services/service/VoteCountingServiceTest.java | 28 ++- .../pollen/services/service/VoteServiceTest.java | 21 ++ 55 files changed, 1134 insertions(+), 430 deletions(-) diff --git a/pollen-persistence/src/main/java/org/chorem/pollen/persistence/PollenTopiaApplicationContext.java b/pollen-persistence/src/main/java/org/chorem/pollen/persistence/PollenTopiaApplicationContext.java index f19a22f..1e7779b 100644 --- a/pollen-persistence/src/main/java/org/chorem/pollen/persistence/PollenTopiaApplicationContext.java +++ b/pollen-persistence/src/main/java/org/chorem/pollen/persistence/PollenTopiaApplicationContext.java @@ -1,5 +1,26 @@ package org.chorem.pollen.persistence; +/* + * #%L + * Pollen :: Persistence + * %% + * 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.nuiton.topia.persistence.TopiaIdFactory; import java.util.Map; diff --git a/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/CommentTopiaDao.java b/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/CommentTopiaDao.java index f4835cc..6ee2722 100644 --- a/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/CommentTopiaDao.java +++ b/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/CommentTopiaDao.java @@ -1,5 +1,26 @@ package org.chorem.pollen.persistence.entity; +/* + * #%L + * Pollen :: Persistence + * %% + * 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 java.util.List; public class CommentTopiaDao extends AbstractCommentTopiaDao<Comment> { diff --git a/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/VoteToChoices.java b/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/VoteToChoices.java index 43762ff..03dc60f 100644 --- a/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/VoteToChoices.java +++ b/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/VoteToChoices.java @@ -1,5 +1,26 @@ package org.chorem.pollen.persistence.entity; +/* + * #%L + * Pollen :: Persistence + * %% + * 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 java.io.Serializable; import java.util.Collections; import java.util.Comparator; diff --git a/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/VoteTopiaDao.java b/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/VoteTopiaDao.java index e6091e4..f2d675b 100644 --- a/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/VoteTopiaDao.java +++ b/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/VoteTopiaDao.java @@ -1,5 +1,26 @@ package org.chorem.pollen.persistence.entity; +/* + * #%L + * Pollen :: Persistence + * %% + * 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 java.util.List; public class VoteTopiaDao extends AbstractVoteTopiaDao<Vote> { diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/JsonHelper.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/JsonHelper.java new file mode 100644 index 0000000..863a2b1 --- /dev/null +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/JsonHelper.java @@ -0,0 +1,254 @@ +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.google.common.collect.Multimap; +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import org.chorem.pollen.services.bean.PollenEntityId; +import org.chorem.pollen.services.bean.PollenEntityRef; +import org.chorem.pollen.services.service.InvalidFormException; +import org.nuiton.topia.persistence.TopiaIdFactory; + +import java.lang.reflect.Type; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Created on 5/24/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class JsonHelper { + + private final Gson gson; + + public JsonHelper(final TopiaIdFactory idFactory, boolean prettyPrint) { + + GsonBuilder gsonBuilder = new GsonBuilder(); + + gsonBuilder.setExclusionStrategies(new ExclusionStrategy() { + @Override + public boolean shouldSkipField(FieldAttributes f) { + return false; + } + + @Override + public boolean shouldSkipClass(Class<?> clazz) { + return clazz == Class.class; + } + }); + + gsonBuilder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { + + @Override + public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { + + JsonElement result; + + if (src == null) { + + result = JsonNull.INSTANCE; + + } else { + + result = new JsonPrimitive(src.getTime()); + + } + + return result; + + } + + }); + + gsonBuilder.registerTypeAdapter(PollenEntityId.class, new JsonSerializer<PollenEntityId>() { + + @Override + public JsonElement serialize(PollenEntityId src, Type typeOfSrc, JsonSerializationContext context) { + + JsonElement result; + + if (src == null || src.getEntityId() == null) { + + result = JsonNull.INSTANCE; + + } else { + + src.encode(idFactory); + result = new JsonPrimitive(src.getReducedId()); + + } + + return result; + + } + + }); + + gsonBuilder.registerTypeAdapter(PollenEntityRef.class, new JsonSerializer<PollenEntityRef>() { + + @Override + public JsonElement serialize(PollenEntityRef src, Type typeOfSrc, JsonSerializationContext context) { + + JsonElement result; + + if (src == null || src.getEntityId() == null) { + + result = JsonNull.INSTANCE; + + } else { + + src.encode(idFactory); + + JsonObject object = new JsonObject(); + object.add("permission", new JsonPrimitive(src.getPermission())); + object.add("id", new JsonPrimitive(src.getReducedId())); + + result = object; + + } + + return result; + + } + + }); + + gsonBuilder.registerTypeAdapter(InvalidFormException.class, new JsonSerializer<InvalidFormException>() { + + @Override + public JsonElement serialize(InvalidFormException src, Type typeOfSrc, JsonSerializationContext context) { + + + Multimap<String, String> errors = src.getErrors(); + + Map<String, Object> map = new HashMap<>(); + for (String key : errors.keySet()) { + map.put(key, errors.get(key)); + } + + JsonElement result = context.serialize(map); + + return result; + + } + + }); + + gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { + + @Override + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsLong()); + } + + }); + + gsonBuilder.registerTypeAdapter(PollenEntityId.class, new JsonDeserializer<PollenEntityId>() { + + @Override + public PollenEntityId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + + PollenEntityId id = PollenEntityId.newId(typeOfT); + + String asString = json.getAsString(); + + if (asString != null) { + + id.setReducedId(asString); + id.decode(idFactory); + + } + + return id; + + } + + }); + + gsonBuilder.registerTypeAdapter(PollenEntityRef.class, new JsonDeserializer<PollenEntityRef>() { + + @Override + public PollenEntityRef deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + + String id = ((JsonObject) json).get("id").getAsString(); + String permission = ((JsonObject) json).get("permission").getAsString(); + + PollenEntityRef<?> ref = PollenEntityRef.newRef(typeOfT); + + ref.setReducedId(id); + ref.setPermission(permission); + ref.decode(idFactory); + + + return ref; + + } + + }); + + if (prettyPrint) { + + gsonBuilder.setPrettyPrinting(); + + } + + this.gson = gsonBuilder.create(); + + } + + public String toJson(Object model) { + + String json = gson.toJson(model); + return json; + + } + + public <O> O fromJson(String json, Class<O> type) { + + O o = gson.fromJson(json, type); + return o; + + } + + public <O> O fromJson(String json, Type type) { + + O o = gson.fromJson(json, type); + return o; + + } + +} diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java index 71a5922..979f22b 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java @@ -23,10 +23,6 @@ package org.chorem.pollen.rest.api; * #L% */ -import com.google.common.collect.Multimap; -import com.google.gson.Gson; -import org.chorem.pollen.services.bean.CreateBeanRef; -import org.chorem.pollen.services.bean.PollenEntityId; import org.chorem.pollen.services.service.InvalidFormException; import org.debux.webmotion.server.call.Call; import org.debux.webmotion.server.call.HttpContext; @@ -38,8 +34,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; -import java.util.HashMap; -import java.util.Map; /** * To render any entities. @@ -64,9 +58,6 @@ public class PollenRender<T> extends Render { PollenRestApiApplicationContext applicationContext = PollenRestApiApplicationContext.getApplicationContext(context.getServletContext()); - TopiaIdFactory topiaIdFactory = applicationContext.getTopiaApplicationContext().getTopiaIdFactory(); - - Object map; if (model instanceof InvalidFormException) { @@ -74,50 +65,15 @@ public class PollenRender<T> extends Render { } - map = toMap(topiaIdFactory, model); - - + TopiaIdFactory topiaIdFactory = applicationContext.getTopiaApplicationContext().getTopiaIdFactory(); boolean devMode = applicationContext.getApplicationConfig().isDevMode(); - - Gson gson = PollenRestApiUtil.newGson(topiaIdFactory, devMode); - String json = gson.toJson(map); + JsonHelper gson = new JsonHelper(topiaIdFactory, devMode); + String json = gson.toJson(model); PrintWriter out = context.getOut(); out.print(json); } - protected <M> Object toMap(TopiaIdFactory topiaIdFactory, M model) { - - if (model instanceof InvalidFormException) { - - InvalidFormException invalidFormException = (InvalidFormException) model; - - Multimap<String, String> errors = invalidFormException.getErrors(); - - Map<String, Object> result = new HashMap<>(); - for (String key : errors.keySet()) { - result.put(key, errors.get(key)); - } - - return result; - - } - - if (model instanceof CreateBeanRef) { - - // encode id - CreateBeanRef createBeanRef = (CreateBeanRef) model; - String encodeId = PollenEntityId.encode(topiaIdFactory, createBeanRef.getId()); - - CreateBeanRef result = CreateBeanRef.of(encodeId, createBeanRef.getPermission()); - return result; - - } - - return model; - - } - } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiUtil.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiUtil.java index affba69..f95df3a 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiUtil.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiUtil.java @@ -1,26 +1,31 @@ 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.google.common.collect.Lists; -import com.google.gson.ExclusionStrategy; -import com.google.gson.FieldAttributes; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonParseException; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; import org.apache.commons.lang3.StringUtils; -import org.chorem.pollen.services.bean.PollenEntityId; import org.debux.webmotion.server.call.HttpContext; -import org.nuiton.topia.persistence.TopiaIdFactory; import javax.servlet.http.HttpServletResponse; -import java.lang.reflect.Type; -import java.util.Date; import java.util.List; /** @@ -47,12 +52,6 @@ public class PollenRestApiUtil { return list; } - public static String entitiesToString(TopiaIdFactory topiaIdFactory, Object o) { - - return newGson(topiaIdFactory, true).toJson(o); - - } - public static void prepareResponse(HttpContext context) { HttpServletResponse response = context.getResponse(); @@ -70,106 +69,4 @@ public class PollenRestApiUtil { } } - public static Gson newGson(final TopiaIdFactory idFactory, boolean prettyPrint) { - - GsonBuilder gsonBuilder = new GsonBuilder(); - gsonBuilder.setExclusionStrategies(new ExclusionStrategy() { - @Override - public boolean shouldSkipField(FieldAttributes f) { - return false; - } - - @Override - public boolean shouldSkipClass(Class<?> clazz) { - return clazz == Class.class; - } - }); - - gsonBuilder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { - - @Override - public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { - - JsonElement result; - - if (src == null) { - - result = JsonNull.INSTANCE; - - } else { - - result = new JsonPrimitive(src.getTime()); - - } - - return result; - - } - - }); - - gsonBuilder.registerTypeAdapter(PollenEntityId.class, new JsonSerializer<PollenEntityId>() { - - @Override - public JsonElement serialize(PollenEntityId src, Type typeOfSrc, JsonSerializationContext context) { - - JsonElement result; - - if (src == null || src.getEntityId() == null) { - - result = JsonNull.INSTANCE; - - } else { - - src.encode(idFactory); - result = new JsonPrimitive(src.getReducedId()); - - } - - return result; - - } - - }); - - gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { - - @Override - public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - return new Date(json.getAsLong()); - } - - }); - gsonBuilder.registerTypeAdapter(PollenEntityId.class, new JsonDeserializer<PollenEntityId>() { - - @Override - public PollenEntityId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - - PollenEntityId id = PollenEntityId.newId(typeOfT); - - String asString = json.getAsString(); - - if (asString != null) { - - id.setReducedId(asString); - id.decode(idFactory); - - } - - return id; - - } - - }); - - if (prettyPrint) { - - gsonBuilder.setPrettyPrinting(); - - } - - Gson gson = gsonBuilder.create(); - return gson; - - } } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/DateConverter.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/DateConverter.java index 590bc91..f6ccfd2 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/DateConverter.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/DateConverter.java @@ -1,5 +1,26 @@ 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.apache.commons.beanutils.converters.AbstractConverter; import java.util.Date; diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonArrayConverter.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonArrayConverter.java index f7907b3..598a4ab 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonArrayConverter.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonArrayConverter.java @@ -1,12 +1,32 @@ package org.chorem.pollen.rest.api.converter; -import com.google.gson.Gson; +/* + * #%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.google.common.reflect.TypeParameter; +import com.google.common.reflect.TypeToken; import org.apache.commons.beanutils.converters.AbstractConverter; -import org.chorem.pollen.rest.api.PollenRestApiUtil; +import org.chorem.pollen.rest.api.JsonHelper; import org.nuiton.topia.persistence.TopiaIdFactory; -import java.lang.reflect.Array; - /** * Created on 5/16/14. * @@ -17,19 +37,18 @@ public class JsonArrayConverter<O> extends AbstractConverter { protected final Class<O[]> arrayType; - protected final Gson gson; + protected final JsonHelper jsonHelper; public static <O> JsonArrayConverter<O> newConverter(TopiaIdFactory topiaIdFactory, Class<O> objectType) { return new JsonArrayConverter<>(topiaIdFactory, objectType); } public JsonArrayConverter(TopiaIdFactory topiaIdFactory, Class<O> entityType) { - this.arrayType = getArrayClass(entityType); - this.gson = PollenRestApiUtil.newGson(topiaIdFactory, false); - } - <T> Class<T[]> getArrayClass(Class<T> clazz) { - return (Class<T[]>) Array.newInstance(clazz, 0).getClass(); + this.arrayType = (Class<O[]>) new TypeToken<O[]>() {} + .where(new TypeParameter<O>() {}, entityType) + .getType(); + this.jsonHelper = new JsonHelper(topiaIdFactory, false); } @Override @@ -47,8 +66,8 @@ public class JsonArrayConverter<O> extends AbstractConverter { } - O[] values = gson.fromJson(stringValue, this.arrayType); -// O[] values = mapper.readValue(stringValue, this.arrayType); + O[] values = jsonHelper.fromJson(stringValue, this.arrayType); + return (T) values; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java index 8d2fc9e..7560194 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java @@ -21,10 +21,9 @@ package org.chorem.pollen.rest.api.converter; * #L% */ -import com.google.gson.Gson; import org.apache.commons.beanutils.converters.AbstractConverter; import org.chorem.pollen.persistence.PollenEntityEnum; -import org.chorem.pollen.rest.api.PollenRestApiUtil; +import org.chorem.pollen.rest.api.JsonHelper; import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.TopiaIdFactory; @@ -40,13 +39,13 @@ public class JsonConverter<O> extends AbstractConverter { protected Class<O> implementationClass; - protected final Gson gson; + protected final JsonHelper jsonHelper; - public static <O> JsonConverter<O> newConverter(TopiaIdFactory topiaIdFactory , Class<O> objectType) { + public static <O> JsonConverter<O> newConverter(TopiaIdFactory topiaIdFactory, Class<O> objectType) { return newConverter(topiaIdFactory, objectType, objectType); } - public static <E extends TopiaEntity> JsonConverter<E> newEntityConverter(TopiaIdFactory topiaIdFactory , Class<E> objectType) { + public static <E extends TopiaEntity> JsonConverter<E> newEntityConverter(TopiaIdFactory topiaIdFactory, Class<E> objectType) { Class<E> implementationClass = PollenEntityEnum.getImplementationClass(objectType); return newConverter(topiaIdFactory, objectType, implementationClass); } @@ -58,14 +57,14 @@ public class JsonConverter<O> extends AbstractConverter { public JsonConverter(TopiaIdFactory topiaIdFactory, Class<O> objectType, Class<O> implementationClass) { this.objectType = objectType; - this.gson = PollenRestApiUtil.newGson(topiaIdFactory, false); + this.jsonHelper = new JsonHelper(topiaIdFactory, false); this.implementationClass = implementationClass; } @Override protected String convertToString(Object value) throws Throwable { - String result = gson.toJson(value); + String result = jsonHelper.toJson(value); return result; } @@ -85,7 +84,7 @@ public class JsonConverter<O> extends AbstractConverter { } - T result = (T) gson.fromJson(stringValue, implementationClass); + T result = (T) jsonHelper.fromJson(stringValue, implementationClass); return result; diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenBeanIdInjector.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenBeanIdInjector.java index 6ac37d3..5aa0d22 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenBeanIdInjector.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenBeanIdInjector.java @@ -1,5 +1,26 @@ package org.chorem.pollen.rest.api.injector; +/* + * #%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.services.bean.PollenEntityId; import org.debux.webmotion.server.call.Call; import org.debux.webmotion.server.handler.ExecutorParametersInjectorHandler; diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenRestApiRequestContextInjector.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenRestApiRequestContextInjector.java index 76fd593..30752a7 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenRestApiRequestContextInjector.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenRestApiRequestContextInjector.java @@ -1,5 +1,26 @@ package org.chorem.pollen.rest.api.injector; +/* + * #%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.rest.api.PollenRestApiRequestContext; import org.debux.webmotion.server.call.Call; import org.debux.webmotion.server.call.HttpContext; diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenServiceInjector.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenServiceInjector.java index f35b2a0..4177582 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenServiceInjector.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/injector/PollenServiceInjector.java @@ -1,5 +1,26 @@ package org.chorem.pollen.rest.api.injector; +/* + * #%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.rest.api.PollenRestApiRequestContext; import org.chorem.pollen.services.PollenService; import org.debux.webmotion.server.call.Call; diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/AuthApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/AuthApi.java index 8b8be04..717a580 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/AuthApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/AuthApi.java @@ -23,7 +23,8 @@ package org.chorem.pollen.rest.api.v1; * #L% */ -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.security.PollenAuthenticationException; import org.chorem.pollen.services.service.security.SecurityService; import org.debux.webmotion.server.WebMotionController; @@ -36,9 +37,9 @@ import org.debux.webmotion.server.WebMotionController; */ public class AuthApi extends WebMotionController { - public CreateBeanRef login(SecurityService securityService, String login, String password, Boolean rememberMe) throws PollenAuthenticationException { + public PollenEntityRef<PollenUser> login(SecurityService securityService, String login, String password, Boolean rememberMe) throws PollenAuthenticationException { - CreateBeanRef principalRef = securityService.login(login, password, rememberMe); + PollenEntityRef<PollenUser> principalRef = securityService.login(login, password, rememberMe); return principalRef; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java index d591990..96d8f2f 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java @@ -26,8 +26,8 @@ package org.chorem.pollen.rest.api.v1; import org.chorem.pollen.persistence.entity.Choice; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.services.bean.ChoiceBean; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollenEntityId; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.ChoiceService; import org.chorem.pollen.services.service.InvalidFormException; import org.debux.webmotion.server.WebMotionController; @@ -56,9 +56,9 @@ public class ChoiceApi extends WebMotionController { } - public CreateBeanRef addChoice(ChoiceService choiceService, PollenEntityId<Poll> pollId, ChoiceBean choice) throws InvalidFormException { + public PollenEntityRef<Choice> addChoice(ChoiceService choiceService, PollenEntityId<Poll> pollId, ChoiceBean choice) throws InvalidFormException { - CreateBeanRef principalRef = choiceService.addChoice(pollId.getEntityId(), choice); + PollenEntityRef<Choice> principalRef = choiceService.addChoice(pollId.getEntityId(), choice); return principalRef; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/CommentApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/CommentApi.java index 7e2283b..7113148 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/CommentApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/CommentApi.java @@ -26,8 +26,8 @@ package org.chorem.pollen.rest.api.v1; import org.chorem.pollen.persistence.entity.Comment; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.services.bean.CommentBean; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollenEntityId; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.CommentService; import org.chorem.pollen.services.service.InvalidFormException; import org.debux.webmotion.server.WebMotionController; @@ -57,9 +57,9 @@ public class CommentApi extends WebMotionController { } - public CreateBeanRef addComment(CommentService commentService, PollenEntityId<Poll> pollId, CommentBean comment) throws InvalidFormException { + public PollenEntityRef<Comment> addComment(CommentService commentService, PollenEntityId<Poll> pollId, CommentBean comment) throws InvalidFormException { - CreateBeanRef principalRef = commentService.addComment(pollId.getEntityId(), comment); + PollenEntityRef<Comment> principalRef = commentService.addComment(pollId.getEntityId(), comment); return principalRef; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/FavoriteListApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/FavoriteListApi.java index f4e53ad..17d0e0f 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/FavoriteListApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/FavoriteListApi.java @@ -25,10 +25,10 @@ package org.chorem.pollen.rest.api.v1; import org.chorem.pollen.persistence.entity.FavoriteList; import org.chorem.pollen.persistence.entity.FavoriteListMember; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.FavoriteListBean; import org.chorem.pollen.services.bean.FavoriteListMemberBean; import org.chorem.pollen.services.bean.PollenEntityId; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.FavoriteListImportException; import org.chorem.pollen.services.service.FavoriteListService; import org.chorem.pollen.services.service.InvalidFormException; @@ -60,9 +60,9 @@ public class FavoriteListApi extends WebMotionController { } - public CreateBeanRef createFavoriteList(FavoriteListService favoriteListService, FavoriteListBean favoriteList) throws InvalidFormException { + public PollenEntityRef<FavoriteList> createFavoriteList(FavoriteListService favoriteListService, FavoriteListBean favoriteList) throws InvalidFormException { - CreateBeanRef principalRef = favoriteListService.createFavoriteList(favoriteList); + PollenEntityRef<FavoriteList> principalRef = favoriteListService.createFavoriteList(favoriteList); return principalRef; } @@ -108,9 +108,9 @@ public class FavoriteListApi extends WebMotionController { } - public CreateBeanRef addMember(FavoriteListService favoriteListService, PollenEntityId<FavoriteList> favoriteListId, FavoriteListMemberBean member) throws InvalidFormException { + public PollenEntityRef<FavoriteListMember> addMember(FavoriteListService favoriteListService, PollenEntityId<FavoriteList> favoriteListId, FavoriteListMemberBean member) throws InvalidFormException { - CreateBeanRef principalRef = favoriteListService.addFavoriteListMember(favoriteListId.getEntityId(), member); + PollenEntityRef<FavoriteListMember> principalRef = favoriteListService.addFavoriteListMember(favoriteListId.getEntityId(), member); return principalRef; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollApi.java index a3a078b..2ca20ab 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollApi.java @@ -26,9 +26,9 @@ package org.chorem.pollen.rest.api.v1; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.rest.api.PollenRestApiUtil; import org.chorem.pollen.services.bean.ChoiceBean; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollBean; import org.chorem.pollen.services.bean.PollenEntityId; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.VoterListBean; import org.chorem.pollen.services.service.InvalidFormException; import org.chorem.pollen.services.service.PollService; @@ -88,16 +88,16 @@ public class PollApi extends WebMotionController { } - public CreateBeanRef createPoll(PollService pollService, - PollBean poll, - ChoiceBean[] choices, - VoterListBean[] voterLists) throws InvalidFormException { + public PollenEntityRef<Poll> createPoll(PollService pollService, + PollBean poll, + ChoiceBean[] choices, + VoterListBean[] voterLists) throws InvalidFormException { List<ChoiceBean> choiceList = PollenRestApiUtil.toList(choices); List<VoterListBean> voterListList = PollenRestApiUtil.toList(voterLists); - CreateBeanRef principalRef = pollService.createPoll(poll, choiceList, voterListList); + PollenEntityRef<Poll> principalRef = pollService.createPoll(poll, choiceList, voterListList); return principalRef; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenUserApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenUserApi.java index 325303b..4d42c3b 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenUserApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenUserApi.java @@ -24,8 +24,8 @@ package org.chorem.pollen.rest.api.v1; */ import org.chorem.pollen.persistence.entity.PollenUser; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollenEntityId; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.PollenUserBean; import org.chorem.pollen.services.service.InvalidFormException; import org.chorem.pollen.services.service.PollenUserService; @@ -57,11 +57,11 @@ public class PollenUserApi extends WebMotionController { } - public CreateBeanRef createUser(PollenUserService pollenUserService, - PollenUserBean user, - boolean generatePassword) throws InvalidFormException { + public PollenEntityRef<PollenUser> createUser(PollenUserService pollenUserService, + PollenUserBean user, + boolean generatePassword) throws InvalidFormException { - CreateBeanRef principalRef = pollenUserService.createUser(user, generatePassword); + PollenEntityRef<PollenUser> principalRef = pollenUserService.createUser(user, generatePassword); return principalRef; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteApi.java index c1a1b19..072e758 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteApi.java @@ -25,8 +25,8 @@ package org.chorem.pollen.rest.api.v1; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.Vote; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollenEntityId; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.VoteBean; import org.chorem.pollen.services.service.InvalidFormException; import org.chorem.pollen.services.service.VoteService; @@ -56,9 +56,9 @@ public class VoteApi extends WebMotionController { } - public CreateBeanRef addVote(VoteService voteService, PollenEntityId<Poll> pollId, VoteBean vote) throws InvalidFormException { + public PollenEntityRef<Vote> addVote(VoteService voteService, PollenEntityId<Poll> pollId, VoteBean vote) throws InvalidFormException { - CreateBeanRef principalRef = voteService.addVote(pollId.getEntityId(), vote); + PollenEntityRef<Vote> principalRef = voteService.addVote(pollId.getEntityId(), vote); return principalRef; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoterListApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoterListApi.java index 231d8d0..d6c35d0 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoterListApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoterListApi.java @@ -27,8 +27,8 @@ import org.chorem.pollen.persistence.entity.FavoriteList; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.VoterList; import org.chorem.pollen.persistence.entity.VoterListMember; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollenEntityId; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.VoterListBean; import org.chorem.pollen.services.bean.VoterListMemberBean; import org.chorem.pollen.services.service.InvalidFormException; @@ -67,9 +67,9 @@ public class VoterListApi extends WebMotionController { } - public CreateBeanRef createVoterList(VoterListService voterListService, PollenEntityId<Poll> pollId, VoterListBean voterList) throws InvalidFormException { + public PollenEntityRef<VoterList> createVoterList(VoterListService voterListService, PollenEntityId<Poll> pollId, VoterListBean voterList) throws InvalidFormException { - CreateBeanRef principalRef = voterListService.addVoterList(pollId.getEntityId(), voterList); + PollenEntityRef<VoterList> principalRef = voterListService.addVoterList(pollId.getEntityId(), voterList); return principalRef; } diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java index 939417f..523b001 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java @@ -60,11 +60,25 @@ public class AbstractPollenRestApiTest extends WebMotionTest { @Rule public final FakePollenApplicationContext application = new FakePollenApplicationContext("pollen-rest-api.properties"); + protected JsonHelper jsonHelper; + @Override protected int getPort() { return application.getPort(); } + protected JsonHelper getJsonHelper() { + + if (jsonHelper == null) { + + TopiaIdFactory topiaIdFactory = application.getTopiaApplicationContext().getTopiaIdFactory(); + + jsonHelper = new JsonHelper(topiaIdFactory, true); + } + + return jsonHelper; + } + protected void loadFixtures(String fixturesSetName) { PollenTopiaPersistenceContext persistenceContext = application.newPersistenceContext(); diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AuthApiTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AuthApiTest.java index 89ad8ff..308447b 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AuthApiTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AuthApiTest.java @@ -1,15 +1,36 @@ 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.google.common.reflect.TypeToken; import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Response; -import org.chorem.pollen.persistence.entity.Poll; -import org.chorem.pollen.rest.api.converter.JsonConverter; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.junit.Assert; import org.junit.Test; -import org.nuiton.topia.persistence.internal.DefaultTopiaIdFactory; import java.io.IOException; +import java.lang.reflect.Type; import java.net.URISyntaxException; @@ -47,9 +68,9 @@ public class AuthApiTest extends AbstractPollenRestApiTest { showTestResult(loginContent); Assert.assertNotNull(loginContent); - JsonConverter<CreateBeanRef> jsonConverter = JsonConverter.newConverter(new DefaultTopiaIdFactory(), CreateBeanRef.class); - CreateBeanRef createBeanRef = jsonConverter.convert(CreateBeanRef.class, loginContent); - Assert.assertNotNull(createBeanRef); + Type type = new TypeToken<PollenEntityRef<PollenUser>>() {}.getType(); + + PollenEntityRef createBeanRef = getJsonHelper().fromJson(loginContent, type); Request logout = createRequest("/v1/logout").Get(); logout.addHeader(PollenRestApiRequestFilter.REQUEST_HEADER_SESSION_TOKEN, createBeanRef.getPermission()); diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollApiTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollApiTest.java index 1aa53fd..7bd1100 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollApiTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollApiTest.java @@ -21,6 +21,7 @@ package org.chorem.pollen.rest.api; * #L% */ +import org.apache.http.client.HttpResponseException; import org.apache.http.client.fluent.Request; import org.chorem.pollen.persistence.entity.Choice; import org.chorem.pollen.persistence.entity.ChoiceImpl; @@ -35,7 +36,6 @@ import org.chorem.pollen.services.PollenFixtures; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.nuiton.topia.persistence.TopiaIdFactory; import java.io.IOException; import java.net.URISyntaxException; @@ -137,10 +137,35 @@ public class PollApiTest extends AbstractPollenRestApiTest { choice2.setName("choiceB"); choices.add(choice2); - TopiaIdFactory topiaIdFactory = application.getTopiaApplicationContext().getTopiaIdFactory(); + String choiceStr = getJsonHelper().toJson(choices); + String pollStr = getJsonHelper().toJson(poll); - String choiceStr = PollenRestApiUtil.entitiesToString(topiaIdFactory, choices); - String pollStr = PollenRestApiUtil.entitiesToString(topiaIdFactory, poll); + Request request = createRequest("/v1/polls") + .addParameter("poll", pollStr) + .addParameter("choices", choiceStr) + .Post(); + + String content = request.execute().returnContent().asString(); + showTestResult(content); + assertNotNull(content); + + } + + @Test(expected = HttpResponseException.class) + public void postBadPoll() throws Throwable { + + Poll poll = new PollImpl(); + poll.setPollType(PollType.FREE); + poll.setCommentVisibility(CommentVisibility.EVERYBODY); + poll.setResultVisibility(ResultVisibility.EVERYBODY); + poll.setVoteVisibility(VoteVisibility.EVERYBODY); + poll.setVoteCountingType(1); + poll.setTitle("title"); + + List<Choice> choices = new ArrayList<>(); + + String choiceStr = getJsonHelper().toJson(choices); + String pollStr = getJsonHelper().toJson(poll); Request request = createRequest("/v1/polls") .addParameter("poll", pollStr) diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollenUserApiTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollenUserApiTest.java index 1e7b9f1..18bf5bb 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollenUserApiTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollenUserApiTest.java @@ -23,16 +23,17 @@ package org.chorem.pollen.rest.api; * #L% */ +import com.google.common.reflect.TypeToken; import org.apache.http.client.fluent.Request; import org.chorem.pollen.persistence.entity.PollenUser; -import org.chorem.pollen.rest.api.converter.JsonConverter; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.services.bean.PollenEntityRef; +import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.nuiton.topia.persistence.internal.DefaultTopiaIdFactory; import java.io.IOException; +import java.lang.reflect.Type; import java.net.URISyntaxException; import static org.junit.Assert.assertTrue; @@ -59,9 +60,17 @@ public class PollenUserApiTest extends AbstractPollenRestApiTest { .Post(); String loginContent = loginRequest.execute().returnContent().asString(); - JsonConverter<CreateBeanRef> jsonConverter = JsonConverter.newConverter(new DefaultTopiaIdFactory(), CreateBeanRef.class); + showTestResult(loginContent); + + Type type = new TypeToken<PollenEntityRef<PollenUser>>() { + }.getType(); + + PollenEntityRef<PollenUser> createBeanRef = getJsonHelper().fromJson(loginContent, type); + Assert.assertNotNull(createBeanRef); + Assert.assertNotNull(createBeanRef.getEntityId()); + Assert.assertNotNull(createBeanRef.getReducedId()); + Assert.assertNotNull(createBeanRef.getPermission()); - CreateBeanRef createBeanRef = jsonConverter.convert(CreateBeanRef.class, loginContent); return createBeanRef.getPermission(); } diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonArrayConverterTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonArrayConverterTest.java index c216d62..78294be 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonArrayConverterTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/converter/JsonArrayConverterTest.java @@ -1,5 +1,26 @@ 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.ChoiceType; import org.chorem.pollen.services.bean.ChoiceBean; import org.junit.Assert; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/ChoiceBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/ChoiceBean.java index 746e055..c5ee22a 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/ChoiceBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/ChoiceBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.ChoiceImpl; import org.chorem.pollen.persistence.entity.ChoiceType; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/CommentBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/CommentBean.java index 513337e..262f3bf 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/CommentBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/CommentBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.Comment; import org.chorem.pollen.persistence.entity.CommentImpl; import org.chorem.pollen.persistence.entity.PollenPrincipalImpl; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/CreateBeanRef.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/CreateBeanRef.java deleted file mode 100644 index 5e5ff90..0000000 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/CreateBeanRef.java +++ /dev/null @@ -1,105 +0,0 @@ -package org.chorem.pollen.services.bean; - -import org.chorem.pollen.persistence.entity.Choice; -import org.chorem.pollen.persistence.entity.Comment; -import org.chorem.pollen.persistence.entity.Poll; -import org.chorem.pollen.persistence.entity.PollenToken; -import org.chorem.pollen.persistence.entity.SessionToken; -import org.chorem.pollen.persistence.entity.Vote; -import org.nuiton.topia.persistence.TopiaEntity; - -/** - * Created on 5/15/14. - * - * @author Tony Chemit <chemit@codelutin.com> - * @since 2.0 - */ -public class CreateBeanRef { - - protected String id; - - protected String permission; - - public static CreateBeanRef of(Poll entity) { - - CreateBeanRef ref = of(entity, entity.getCreator().getPermission()); - return ref; - - } - - public static CreateBeanRef of(Choice entity) { - - CreateBeanRef ref = of(entity, entity.getCreator().getPermission()); - return ref; - - } - - public static CreateBeanRef of(Comment entity) { - - CreateBeanRef ref = of(entity, entity.getAuthor().getPermission()); - return ref; - - } - - public static CreateBeanRef of(Vote entity) { - - CreateBeanRef ref = of(entity, entity.getVoter().getPermission()); - return ref; - - } - - public static CreateBeanRef of(SessionToken result) { - - CreateBeanRef ref = of(result.getPollenUser(), result.getPollenToken()); - return ref; - - } - - public static CreateBeanRef of(TopiaEntity result) { - - CreateBeanRef ref = new CreateBeanRef(result); - return ref; - - } - - public static CreateBeanRef of(TopiaEntity id, PollenToken permission) { - - CreateBeanRef ref = new CreateBeanRef(id.getTopiaId(), permission==null?null: permission.getToken()); - return ref; - - } - - public static CreateBeanRef of(String id, String permission) { - - CreateBeanRef ref = new CreateBeanRef(id, permission); - return ref; - - } - - protected CreateBeanRef(TopiaEntity id) { - this(id.getTopiaId(), null); - } - - protected CreateBeanRef(String id, String permission) { - - setId(id); - setPermission(permission); - - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getPermission() { - return permission; - } - - public void setPermission(String permission) { - this.permission = permission; - } -} diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/FavoriteListBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/FavoriteListBean.java index c6f961e..14ac766 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/FavoriteListBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/FavoriteListBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.FavoriteList; import org.chorem.pollen.persistence.entity.FavoriteListImpl; import org.chorem.pollen.persistence.entity.FavoriteListMember; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/FavoriteListMemberBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/FavoriteListMemberBean.java index 97423c2..b27e338 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/FavoriteListMemberBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/FavoriteListMemberBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.FavoriteListMember; import org.chorem.pollen.persistence.entity.FavoriteListMemberImpl; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollBean.java index 5013db7..0613f68 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.PollImpl; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenBean.java index 2883aa3..61789c7 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenBean.java @@ -1,5 +1,27 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.google.common.base.Preconditions; import org.nuiton.topia.persistence.TopiaEntity; /** @@ -16,29 +38,24 @@ public abstract class PollenBean<E extends TopiaEntity> { protected PollenBean(Class<E> entityType) { this.entityType = entityType; - } - - public PollenEntityId<E> getId() { - if (id == null) { - id = PollenEntityId.newId(entityType); - } - return id; + this.id = PollenEntityId.newId(entityType); } public void setId(PollenEntityId<E> id) { + Preconditions.checkNotNull("id can not be null", id); this.id = id; } public boolean isPersisted() { - return getId().isNotEmpty(); + return id.isNotEmpty(); } public String getEntityId() { - return getId().getEntityId(); + return id.getEntityId(); } public void setEntityId(String id) { - getId().setEntityId(id); + this.id.setEntityId(id); } public abstract void fromEntity(E entity); @@ -52,12 +69,12 @@ public abstract class PollenBean<E extends TopiaEntity> { PollenBean that = (PollenBean) o; - return getId().equals(that.getId()); + return id.equals(that.id); } @Override public int hashCode() { - return getId().hashCode(); + return id.hashCode(); } } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenEntityId.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenEntityId.java index 98147a3..47c7638 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenEntityId.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenEntityId.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import org.nuiton.topia.persistence.TopiaEntity; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenEntityRef.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenEntityRef.java new file mode 100644 index 0000000..f1e3c5c --- /dev/null +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenEntityRef.java @@ -0,0 +1,129 @@ +package org.chorem.pollen.services.bean; + +/* + * #%L + * Pollen :: Service + * %% + * 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.PollenEntityEnum; +import org.chorem.pollen.persistence.entity.Choice; +import org.chorem.pollen.persistence.entity.Comment; +import org.chorem.pollen.persistence.entity.Poll; +import org.chorem.pollen.persistence.entity.PollenToken; +import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.persistence.entity.SessionToken; +import org.chorem.pollen.persistence.entity.Vote; +import org.nuiton.topia.persistence.TopiaEntity; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +/** + * Created on 5/15/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class PollenEntityRef<E extends TopiaEntity> extends PollenEntityId<E> { + + protected String permission; + + public static PollenEntityRef<Poll> of(Poll entity) { + + PollenEntityRef<Poll> ref = of(entity, entity.getCreator().getPermission()); + return ref; + + } + + public static PollenEntityRef<Choice> of(Choice entity) { + + PollenEntityRef<Choice> ref = of(entity, entity.getCreator().getPermission()); + return ref; + + } + + public static PollenEntityRef<Comment> of(Comment entity) { + + PollenEntityRef<Comment> ref = of(entity, entity.getAuthor().getPermission()); + return ref; + + } + + public static PollenEntityRef<Vote> of(Vote entity) { + + PollenEntityRef<Vote> ref = of(entity, entity.getVoter().getPermission()); + return ref; + + } + + public static PollenEntityRef<PollenUser> of(SessionToken result) { + + PollenEntityRef<PollenUser> ref = of(result.getPollenUser(), result.getPollenToken()); + return ref; + + } + + public static <E extends TopiaEntity> PollenEntityRef<E> of(E result) { + + PollenEntityRef<E> ref = new PollenEntityRef<>(result, null); + return ref; + + } + + public static <E extends TopiaEntity> PollenEntityRef<E> of(E id, PollenToken permission) { + + PollenEntityRef<E> ref = new PollenEntityRef<>(id, permission == null ? null : permission.getToken()); + return ref; + + } + + public static <E extends TopiaEntity> PollenEntityRef<E> newRef(Type typeOfT) { + + Type[] actualTypeArguments = ((ParameterizedType) typeOfT).getActualTypeArguments(); + return newRef((Class<E>) actualTypeArguments[0]); + + } + + public static <E extends TopiaEntity> PollenEntityRef<E> newRef(Class<E> entityType) { + + PollenEntityRef<E> ref = new PollenEntityRef<>(entityType); + return ref; + + } + + protected PollenEntityRef(E entity, String permission) { + + this(PollenEntityEnum.getContractClass((Class) entity.getClass())); + setEntityId(entity.getTopiaId()); + setPermission(permission); + } + + protected PollenEntityRef(Class<E> entityType) { + + super(entityType); + } + + public String getPermission() { + return permission; + } + + public void setPermission(String permission) { + this.permission = permission; + } +} diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenUserBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenUserBean.java index 98cd27a..5fb38d1 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenUserBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/PollenUserBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.PollenUser; import org.chorem.pollen.persistence.entity.PollenUserImpl; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteBean.java index d1dad27..9f5cad2 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.PollenPrincipal; import org.chorem.pollen.persistence.entity.PollenPrincipalImpl; import org.chorem.pollen.persistence.entity.Vote; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteToChoiceBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteToChoiceBean.java index 2e29e35..dbbad5b 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteToChoiceBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteToChoiceBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.ChoiceImpl; import org.chorem.pollen.persistence.entity.VoteToChoice; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoterListBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoterListBean.java index 51ea9f7..f31ffe5 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoterListBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoterListBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.VoterList; import org.chorem.pollen.persistence.entity.VoterListImpl; import org.chorem.pollen.persistence.entity.VoterListMember; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoterListMemberBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoterListMemberBean.java index 12facbe..b778ce0 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoterListMemberBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoterListMemberBean.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.bean; +/* + * #%L + * Pollen :: Service + * %% + * 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.VoterListMember; import org.chorem.pollen.persistence.entity.VoterListMemberImpl; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/package-info.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/package-info.java index 2efadbe..53cfd83 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/package-info.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/package-info.java @@ -18,12 +18,12 @@ package org.chorem.pollen.services.bean; * 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% diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java index 07ea92a..11a7f02 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java @@ -31,7 +31,7 @@ import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.PollenPrincipal; import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.services.bean.ChoiceBean; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.security.PermissionVerb; import java.util.List; @@ -111,7 +111,7 @@ public class ChoiceService extends PollenServiceSupport { } - public CreateBeanRef addChoice(String pollId, ChoiceBean choice) throws InvalidFormException { + public PollenEntityRef<Choice> addChoice(String pollId, ChoiceBean choice) throws InvalidFormException { checkNotNull(pollId); checkNotNull(choice); @@ -132,7 +132,7 @@ public class ChoiceService extends PollenServiceSupport { getNotificationService().onChoiceAdded(poll, result); - CreateBeanRef createBeanRef = CreateBeanRef.of(result); + PollenEntityRef<Choice> createBeanRef = PollenEntityRef.of(result); return createBeanRef; } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/CommentService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/CommentService.java index 29cf6a9..8e20a02 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/CommentService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/CommentService.java @@ -27,7 +27,7 @@ import org.chorem.pollen.persistence.entity.Comment; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.PollenPrincipal; import org.chorem.pollen.services.bean.CommentBean; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.security.PermissionVerb; import java.util.List; @@ -77,7 +77,7 @@ public class CommentService extends PollenServiceSupport { } - public CreateBeanRef addComment(String pollId, CommentBean comment) throws InvalidFormException { + public PollenEntityRef<Comment> addComment(String pollId, CommentBean comment) throws InvalidFormException { checkNotNull(pollId); checkNotNull(comment); @@ -93,7 +93,7 @@ public class CommentService extends PollenServiceSupport { getNotificationService().onCommentAdded(poll, result); - CreateBeanRef createBeanRef = CreateBeanRef.of(result); + PollenEntityRef<Comment> createBeanRef = PollenEntityRef.of(result); return createBeanRef; } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java index de556ba..48c1641 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java @@ -33,9 +33,9 @@ import org.chorem.pollen.persistence.entity.FavoriteListMemberTopiaDao; import org.chorem.pollen.persistence.entity.FavoriteListTopiaDao; import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.services.PollenTechnicalException; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.FavoriteListBean; import org.chorem.pollen.services.bean.FavoriteListMemberBean; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.nuiton.util.StringUtil; import javax.naming.NamingEnumeration; @@ -89,7 +89,7 @@ public class FavoriteListService extends PollenServiceSupport { } - public CreateBeanRef createFavoriteList(FavoriteListBean favoriteList) throws InvalidFormException { + public PollenEntityRef<FavoriteList> createFavoriteList(FavoriteListBean favoriteList) throws InvalidFormException { checkIsConnected(); checkNotNull(favoriteList); @@ -107,7 +107,7 @@ public class FavoriteListService extends PollenServiceSupport { getNotificationService().onFavoriteListAdded(user, result); - CreateBeanRef createBeanRef = CreateBeanRef.of(result); + PollenEntityRef<FavoriteList> createBeanRef = PollenEntityRef.of(result); return createBeanRef; } @@ -182,8 +182,8 @@ public class FavoriteListService extends PollenServiceSupport { } - public CreateBeanRef addFavoriteListMember(String favoriteListId, - FavoriteListMemberBean member) throws InvalidFormException { + public PollenEntityRef<FavoriteListMember> addFavoriteListMember(String favoriteListId, + FavoriteListMemberBean member) throws InvalidFormException { checkIsConnected(); checkNotNull(favoriteListId); @@ -202,7 +202,7 @@ public class FavoriteListService extends PollenServiceSupport { FavoriteListMember result = saveFavoriteListMember(favoriteList, member); commit(); - CreateBeanRef createBeanRef = CreateBeanRef.of(result); + PollenEntityRef<FavoriteListMember> createBeanRef = PollenEntityRef.of(result); return createBeanRef; } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/FixturesService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/FixturesService.java index e20ba6e..87eab73 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/FixturesService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/FixturesService.java @@ -37,8 +37,8 @@ import org.chorem.pollen.persistence.entity.PollenUserTopiaDao; import org.chorem.pollen.services.PollenFixtures; import org.chorem.pollen.services.PollenTechnicalException; import org.chorem.pollen.services.bean.ChoiceBean; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollBean; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.security.SecurityService; import java.util.ArrayList; @@ -122,8 +122,8 @@ public class FixturesService extends PollenServiceSupport { try { PollBean pollBean = new PollBean(); pollBean.fromEntity(poll); - CreateBeanRef createdPoll = pollService.createPoll(pollBean, choicesBean, null); - poll.setTopiaId(createdPoll.getId()); + PollenEntityRef<Poll> createdPoll = pollService.createPoll(pollBean, choicesBean, null); + poll.setTopiaId(createdPoll.getEntityId()); PollenPrincipal principal = securityService.getPollenPrincipalByPermissionToken(createdPoll.getPermission()); poll.setCreator(principal); diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/PollService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/PollService.java index de3811c..ceed97b 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/PollService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/PollService.java @@ -30,8 +30,8 @@ import org.chorem.pollen.persistence.entity.PollenPrincipal; import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.persistence.entity.VoterList; import org.chorem.pollen.services.bean.ChoiceBean; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollBean; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.VoterListBean; import org.chorem.pollen.services.service.security.PermissionVerb; @@ -147,7 +147,7 @@ public class PollService extends PollenServiceSupport { } - public CreateBeanRef createPoll(PollBean poll, List<ChoiceBean> choices, List<VoterListBean> voterLists) throws InvalidFormException { + public PollenEntityRef<Poll> createPoll(PollBean poll, List<ChoiceBean> choices, List<VoterListBean> voterLists) throws InvalidFormException { checkNotNull(poll); checkIsNotPersisted(poll); @@ -162,7 +162,7 @@ public class PollService extends PollenServiceSupport { getNotificationService().onPollCreated(savedPoll); - CreateBeanRef createBeanRef = CreateBeanRef.of(savedPoll); + PollenEntityRef<Poll> createBeanRef = PollenEntityRef.of(savedPoll); return createBeanRef; diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenUserService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenUserService.java index 6a8083a..dce5eca 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenUserService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenUserService.java @@ -28,7 +28,7 @@ import org.chorem.pollen.persistence.entity.PollenToken; import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.persistence.entity.PollenUserTopiaDao; import org.chorem.pollen.services.PollenService; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.PollenUserBean; import org.chorem.pollen.services.service.security.PollenInvalidEmailActivationTokenException; import org.chorem.pollen.services.service.security.PollenInvalidPasswordException; @@ -71,7 +71,7 @@ public class PollenUserService extends PollenServiceSupport implements PollenSer return getPollenUserDao().findByTopiaId(userId); } - public CreateBeanRef createUser(PollenUserBean user, boolean generatePassword) throws InvalidFormException { + public PollenEntityRef<PollenUser> createUser(PollenUserBean user, boolean generatePassword) throws InvalidFormException { checkNotNull(user); checkIsNotPersisted(user); @@ -84,7 +84,7 @@ public class PollenUserService extends PollenServiceSupport implements PollenSer getNotificationService().onUserCreated(result, generatePassword); - CreateBeanRef createBeanRef = CreateBeanRef.of(result); + PollenEntityRef<PollenUser> createBeanRef = PollenEntityRef.of(result); return createBeanRef; } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteService.java index a608993..a753379 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteService.java @@ -31,7 +31,7 @@ import org.chorem.pollen.persistence.entity.Polls; import org.chorem.pollen.persistence.entity.Vote; import org.chorem.pollen.persistence.entity.VoteToChoice; import org.chorem.pollen.persistence.entity.VoteToChoiceTopiaDao; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.VoteBean; import org.chorem.pollen.services.bean.VoteToChoiceBean; import org.chorem.pollen.services.service.security.PermissionVerb; @@ -85,7 +85,7 @@ public class VoteService extends PollenServiceSupport { } - public CreateBeanRef addVote(String pollId, VoteBean vote) throws InvalidFormException { + public PollenEntityRef<Vote> addVote(String pollId, VoteBean vote) throws InvalidFormException { checkNotNull(pollId); checkNotNull(vote); @@ -102,7 +102,7 @@ public class VoteService extends PollenServiceSupport { getNotificationService().onVoteAdded(poll, result); - CreateBeanRef createBeanRef = CreateBeanRef.of(result); + PollenEntityRef<Vote> createBeanRef = PollenEntityRef.of(result); return createBeanRef; } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/VoterListService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/VoterListService.java index 398b642..fc91907 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/VoterListService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/VoterListService.java @@ -30,9 +30,9 @@ import org.chorem.pollen.persistence.entity.VoterList; import org.chorem.pollen.persistence.entity.VoterListMember; import org.chorem.pollen.persistence.entity.VoterListMemberTopiaDao; import org.chorem.pollen.persistence.entity.VoterListTopiaDao; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.FavoriteListBean; import org.chorem.pollen.services.bean.FavoriteListMemberBean; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.VoterListBean; import org.chorem.pollen.services.bean.VoterListMemberBean; @@ -105,7 +105,7 @@ public class VoterListService extends PollenServiceSupport { } - public CreateBeanRef addVoterList(String pollId, VoterListBean voterList) throws InvalidFormException { + public PollenEntityRef<VoterList> addVoterList(String pollId, VoterListBean voterList) throws InvalidFormException { checkNotNull(pollId); checkNotNull(voterList); @@ -124,7 +124,7 @@ public class VoterListService extends PollenServiceSupport { //TODO Notify - CreateBeanRef createBeanRef = CreateBeanRef.of(result); + PollenEntityRef<VoterList> createBeanRef = PollenEntityRef.of(result); return createBeanRef; } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/security/SecurityService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/security/SecurityService.java index 595b9cf..562690e 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/security/SecurityService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/security/SecurityService.java @@ -44,7 +44,7 @@ import org.chorem.pollen.persistence.entity.PollenTokenTopiaDao; import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.persistence.entity.SessionToken; import org.chorem.pollen.persistence.entity.Vote; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.PollenServiceSupport; import org.nuiton.topia.persistence.TopiaEntity; @@ -66,7 +66,7 @@ public class SecurityService extends PollenServiceSupport { /** Logger. */ private static final Log log = LogFactory.getLog(SecurityService.class); - public CreateBeanRef login(String login, String password, Boolean rememberMe) throws PollenAuthenticationException { + public PollenEntityRef<PollenUser> login(String login, String password, Boolean rememberMe) throws PollenAuthenticationException { Subject subject = getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(login, password); @@ -93,14 +93,13 @@ public class SecurityService extends PollenServiceSupport { Date endDate = calendar.getTime(); pollenToken.setEndDate(endDate); - // Create session Token SessionToken sessionToken = getSessionTokenDao().create( SessionToken.PROPERTY_POLLEN_TOKEN, pollenToken, SessionToken.PROPERTY_POLLEN_USER, user); commit(); - CreateBeanRef createBeanRef = CreateBeanRef.of(sessionToken); + PollenEntityRef<PollenUser> createBeanRef = PollenEntityRef.of(sessionToken); return createBeanRef; } diff --git a/pollen-services/src/test/java/org/chorem/pollen/services/AbstractPollenServiceTest.java b/pollen-services/src/test/java/org/chorem/pollen/services/AbstractPollenServiceTest.java index 11ff686..648d503 100644 --- a/pollen-services/src/test/java/org/chorem/pollen/services/AbstractPollenServiceTest.java +++ b/pollen-services/src/test/java/org/chorem/pollen/services/AbstractPollenServiceTest.java @@ -24,8 +24,9 @@ package org.chorem.pollen.services; */ import com.google.common.collect.Multimap; +import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.persistence.entity.SessionToken; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.service.InvalidFormException; import org.chorem.pollen.services.service.security.PollenAuthenticationException; import org.chorem.pollen.services.service.security.PollenInvalidSessionTokenException; @@ -90,7 +91,7 @@ public abstract class AbstractPollenServiceTest { SecurityService securityService = newService(SecurityService.class); - CreateBeanRef loginRef = securityService.login(login, password, null); + PollenEntityRef<PollenUser> loginRef = securityService.login(login, password, null); SessionToken sessionToken = securityService.getSessionTokenByToken(loginRef.getPermission()); serviceContext.getSecurityContext().setSessionToken(sessionToken); diff --git a/pollen-services/src/test/java/org/chorem/pollen/services/service/PollServiceTest.java b/pollen-services/src/test/java/org/chorem/pollen/services/service/PollServiceTest.java index 4fe1751..6679ee8 100644 --- a/pollen-services/src/test/java/org/chorem/pollen/services/service/PollServiceTest.java +++ b/pollen-services/src/test/java/org/chorem/pollen/services/service/PollServiceTest.java @@ -24,12 +24,13 @@ package org.chorem.pollen.services.service; */ import org.chorem.pollen.persistence.entity.ChoiceType; +import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.PollType; import org.chorem.pollen.persistence.entity.PollenPrincipal; import org.chorem.pollen.services.AbstractPollenServiceTest; import org.chorem.pollen.services.bean.ChoiceBean; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollBean; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.VoterListBean; import org.chorem.pollen.services.bean.VoterListMemberBean; import org.chorem.pollen.services.service.security.PollenInvalidPermissionException; @@ -98,15 +99,16 @@ public class PollServiceTest extends AbstractPollenServiceTest { choice2.setDescription("Choice B"); choices.add(choice2); - CreateBeanRef createdPoll = service.createPoll(poll, choices, null); + PollenEntityRef<Poll> createdPoll = service.createPoll(poll, choices, null); Assert.assertNotNull(createdPoll); - Assert.assertNotNull(createdPoll.getId()); + String createdPollId = createdPoll.getEntityId(); + Assert.assertNotNull(createdPollId); - PollBean reloadedPoll = service.getPoll(createdPoll.getId()); + PollBean reloadedPoll = service.getPoll(createdPollId); Assert.assertEquals(getServiceContext().getNow(), reloadedPoll.getBeginDate()); - List<ChoiceBean> createdChoices = choiceService.getChoices(createdPoll.getId()); + List<ChoiceBean> createdChoices = choiceService.getChoices(createdPollId); Assert.assertNotNull(createdChoices); Assert.assertEquals(2, createdChoices.size()); @@ -116,7 +118,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdChoice1); Assert.assertNotNull(createdChoice1.getEntityId()); - ChoiceBean reloadedChoice1 = choiceService.getChoice(createdPoll.getId(), createdChoice1.getEntityId()); + ChoiceBean reloadedChoice1 = choiceService.getChoice(createdPollId, createdChoice1.getEntityId()); Assert.assertEquals(createdChoice1, reloadedChoice1); Assert.assertEquals(choice1.getName(), createdChoice1.getName()); @@ -127,7 +129,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdChoice2); Assert.assertNotNull(createdChoice2.getEntityId()); - ChoiceBean reloadedChoice2 = choiceService.getChoice(createdPoll.getId(), createdChoice2.getEntityId()); + ChoiceBean reloadedChoice2 = choiceService.getChoice(createdPollId, createdChoice2.getEntityId()); Assert.assertEquals(createdChoice2, reloadedChoice2); Assert.assertEquals(choice2.getName(), createdChoice2.getName()); @@ -195,12 +197,13 @@ public class PollServiceTest extends AbstractPollenServiceTest { choice2.setName("B"); - CreateBeanRef createdPoll = service.createPoll(poll, choices, null); + PollenEntityRef<Poll> createdPoll = service.createPoll(poll, choices, null); Assert.assertNotNull(createdPoll); - Assert.assertNotNull(createdPoll.getId()); + String createdPollId = createdPoll.getEntityId(); + Assert.assertNotNull(createdPollId); - PollBean reloadedPoll = service.getPoll(createdPoll.getId()); - Assert.assertEquals(createdPoll.getId(), reloadedPoll.getEntityId()); + PollBean reloadedPoll = service.getPoll(createdPollId); + Assert.assertEquals(createdPollId, reloadedPoll.getEntityId()); Assert.assertEquals(getServiceContext().getNow(), reloadedPoll.getBeginDate()); @@ -212,7 +215,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { // Assert.assertNull(createdPoll.getVote()); // Assert.assertNull(createdPoll.getVoterList()); - List<ChoiceBean> createdChoices = choiceService.getChoices(createdPoll.getId()); + List<ChoiceBean> createdChoices = choiceService.getChoices(createdPollId); Assert.assertNotNull(createdChoices); Assert.assertEquals(2, createdChoices.size()); @@ -222,7 +225,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdChoice1); Assert.assertNotNull(createdChoice1.getEntityId()); - ChoiceBean reloadedChoice1 = choiceService.getChoice(createdPoll.getId(), createdChoice1.getEntityId()); + ChoiceBean reloadedChoice1 = choiceService.getChoice(createdPollId, createdChoice1.getEntityId()); Assert.assertEquals(createdChoice1, reloadedChoice1); Assert.assertEquals(choice1.getName(), createdChoice1.getName()); @@ -232,7 +235,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdChoice2); Assert.assertNotNull(createdChoice2.getEntityId()); - ChoiceBean reloadedChoice2 = choiceService.getChoice(createdPoll.getId(), createdChoice2.getEntityId()); + ChoiceBean reloadedChoice2 = choiceService.getChoice(createdPollId, createdChoice2.getEntityId()); Assert.assertEquals(createdChoice2, reloadedChoice2); Assert.assertEquals(choice2.getName(), createdChoice2.getName()); @@ -286,12 +289,13 @@ public class PollServiceTest extends AbstractPollenServiceTest { voterListMember2.setEmail("voter2@pollen.org"); voterListMember2.setWeight(0.7); - CreateBeanRef createdPoll = service.createPoll(poll, choices, voterLists); + PollenEntityRef<Poll> createdPoll = service.createPoll(poll, choices, voterLists); Assert.assertNotNull(createdPoll); - Assert.assertNotNull(createdPoll.getId()); + String createdPollId = createdPoll.getEntityId(); + Assert.assertNotNull(createdPollId); try { - service.getPoll(createdPoll.getId()); + service.getPoll(createdPollId); Assert.fail(); } catch (PollenInvalidPermissionException e) { Assert.assertTrue(true); @@ -302,7 +306,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { PollenPrincipal principal = securityService.getPollenPrincipalByPermissionToken(createdPoll.getPermission()); securityContext.setMainPrincipal(principal); - PollBean reloadedPoll = service.getPoll(createdPoll.getId()); + PollBean reloadedPoll = service.getPoll(createdPollId); // Assert.assertEquals(createdPoll, reloadedPoll); Assert.assertEquals(getServiceContext().getNow(), reloadedPoll.getBeginDate()); @@ -312,7 +316,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { // Assert.assertNull(createdPoll.getCreator().getName()); // Assert.assertNull(createdPoll.getCreator().getEmail()); - List<ChoiceBean> createdChoices = choiceService.getChoices(createdPoll.getId()); + List<ChoiceBean> createdChoices = choiceService.getChoices(createdPollId); Assert.assertNotNull(createdChoices); Assert.assertEquals(2, createdChoices.size()); @@ -321,7 +325,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdChoice1); Assert.assertNotNull(createdChoice1.getEntityId()); - ChoiceBean reloadedChoice1 = choiceService.getChoice(createdPoll.getId(), createdChoice1.getEntityId()); + ChoiceBean reloadedChoice1 = choiceService.getChoice(createdPollId, createdChoice1.getEntityId()); Assert.assertEquals(createdChoice1, reloadedChoice1); Assert.assertEquals(choice1.getName(), createdChoice1.getName()); @@ -332,14 +336,14 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdChoice2); Assert.assertNotNull(createdChoice2.getEntityId()); - ChoiceBean reloadedChoice2 = choiceService.getChoice(createdPoll.getId(), createdChoice2.getEntityId()); + ChoiceBean reloadedChoice2 = choiceService.getChoice(createdPollId, createdChoice2.getEntityId()); Assert.assertEquals(createdChoice2, reloadedChoice2); Assert.assertEquals(choice2.getName(), createdChoice2.getName()); Assert.assertEquals(choice2.getDescription(), createdChoice2.getDescription()); // Assert.assertEquals(createdPoll.getCreator(), createdChoice2.getCreator()); - List<VoterListBean> createdVoterLists = voterListService.getVoterLists(createdPoll.getId()); + List<VoterListBean> createdVoterLists = voterListService.getVoterLists(createdPollId); Assert.assertNotNull(createdVoterLists); Assert.assertEquals(1, createdVoterLists.size()); @@ -347,7 +351,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdVoterList); Assert.assertNotNull(createdVoterList.getEntityId()); - VoterListBean reloadedVoterList = voterListService.getVoterList(createdPoll.getId(), createdVoterList.getEntityId()); + VoterListBean reloadedVoterList = voterListService.getVoterList(createdPollId, createdVoterList.getEntityId()); Assert.assertEquals(createdVoterList, reloadedVoterList); Assert.assertEquals(voterList.getName(), createdVoterList.getName()); @@ -454,12 +458,13 @@ public class PollServiceTest extends AbstractPollenServiceTest { voterListMember2.setName("voter2"); voterListMember2.setEmail("voter2@pollen.org"); - CreateBeanRef createdPoll = service.createPoll(poll, choices, voterLists); + PollenEntityRef<Poll> createdPoll = service.createPoll(poll, choices, voterLists); Assert.assertNotNull(createdPoll); - Assert.assertNotNull(createdPoll.getId()); + String createdPollId = createdPoll.getEntityId(); + Assert.assertNotNull(createdPollId); try { - service.getPoll(createdPoll.getId()); + service.getPoll(createdPollId); Assert.fail(); } catch (PollenInvalidPermissionException e) { Assert.assertTrue(true); @@ -469,7 +474,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { FakePollenSecurityContext securityContext = (FakePollenSecurityContext) serviceContext.getSecurityContext(); securityContext.setMainPrincipal(principal); - PollBean reloadedPoll = service.getPoll(createdPoll.getId()); + PollBean reloadedPoll = service.getPoll(createdPollId); // Assert.assertEquals(createdPoll, reloadedPoll); Assert.assertEquals(getServiceContext().getNow(), reloadedPoll.getBeginDate()); @@ -481,7 +486,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { // Assert.assertNull(createdPoll.getComment()); // Assert.assertNull(createdPoll.getVote()); - List<ChoiceBean> createdChoices = choiceService.getChoices(createdPoll.getId()); + List<ChoiceBean> createdChoices = choiceService.getChoices(createdPollId); Assert.assertNotNull(createdChoices); Assert.assertEquals(2, createdChoices.size()); @@ -490,7 +495,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdChoice1); Assert.assertNotNull(createdChoice1.getEntityId()); - ChoiceBean reloadedChoice1 = choiceService.getChoice(createdPoll.getId(), createdChoice1.getEntityId()); + ChoiceBean reloadedChoice1 = choiceService.getChoice(createdPollId, createdChoice1.getEntityId()); Assert.assertEquals(createdChoice1, reloadedChoice1); Assert.assertEquals(choice1.getName(), createdChoice1.getName()); @@ -501,14 +506,14 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdChoice2); Assert.assertNotNull(createdChoice2.getEntityId()); - ChoiceBean reloadedChoice2 = choiceService.getChoice(createdPoll.getId(), createdChoice2.getEntityId()); + ChoiceBean reloadedChoice2 = choiceService.getChoice(createdPollId, createdChoice2.getEntityId()); Assert.assertEquals(createdChoice2, reloadedChoice2); Assert.assertEquals(choice2.getName(), createdChoice2.getName()); Assert.assertEquals(choice2.getDescription(), createdChoice2.getDescription()); // Assert.assertEquals(createdPoll.getCreator(), createdChoice2.getCreator()); - List<VoterListBean> createdVoterLists = voterListService.getVoterLists(createdPoll.getId()); + List<VoterListBean> createdVoterLists = voterListService.getVoterLists(createdPollId); Assert.assertNotNull(createdVoterLists); Assert.assertEquals(1, createdVoterLists.size()); @@ -516,7 +521,7 @@ public class PollServiceTest extends AbstractPollenServiceTest { Assert.assertNotNull(createdVoterList); Assert.assertNotNull(createdVoterList.getEntityId()); - VoterListBean reloadedVoterList = voterListService.getVoterList(createdPoll.getId(), createdVoterList.getEntityId()); + VoterListBean reloadedVoterList = voterListService.getVoterList(createdPollId, createdVoterList.getEntityId()); Assert.assertEquals(createdVoterList, reloadedVoterList); Assert.assertEquals(voterList.getName(), createdVoterList.getName()); diff --git a/pollen-services/src/test/java/org/chorem/pollen/services/service/PollenUserServiceTest.java b/pollen-services/src/test/java/org/chorem/pollen/services/service/PollenUserServiceTest.java index 461bfe2..43cb7e8 100644 --- a/pollen-services/src/test/java/org/chorem/pollen/services/service/PollenUserServiceTest.java +++ b/pollen-services/src/test/java/org/chorem/pollen/services/service/PollenUserServiceTest.java @@ -26,7 +26,7 @@ package org.chorem.pollen.services.service; import org.apache.commons.collections4.CollectionUtils; import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.services.AbstractPollenServiceTest; -import org.chorem.pollen.services.bean.CreateBeanRef; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.PollenUserBean; import org.chorem.pollen.services.service.security.PollenAuthenticationException; import org.chorem.pollen.services.service.security.PollenInvalidSessionTokenException; @@ -110,9 +110,9 @@ public class PollenUserServiceTest extends AbstractPollenServiceTest { newUser.setLogin("pollen"); newUser.setEmail("pollen@pollen.org"); - CreateBeanRef savedUser = service.createUser(newUser, true); + PollenEntityRef<PollenUser> savedUser = service.createUser(newUser, true); Assert.assertNotNull(savedUser); - Assert.assertNotNull(savedUser.getId()); + Assert.assertNotNull(savedUser.getEntityId()); PollenUserBean newUser2 = new PollenUserBean(); newUser2.setLogin("pollen"); @@ -137,9 +137,9 @@ public class PollenUserServiceTest extends AbstractPollenServiceTest { } newUser2.setEmail("pollen2@pollen.org"); - CreateBeanRef savedUser2 = service.createUser(newUser2, true); + PollenEntityRef<PollenUser> savedUser2 = service.createUser(newUser2, true); Assert.assertNotNull(savedUser2); - Assert.assertNotNull(savedUser2.getId()); + Assert.assertNotNull(savedUser2.getEntityId()); } @Test diff --git a/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteCountingServiceTest.java b/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteCountingServiceTest.java index 2a6e520..100a223 100644 --- a/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteCountingServiceTest.java +++ b/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteCountingServiceTest.java @@ -1,11 +1,33 @@ package org.chorem.pollen.services.service; +/* + * #%L + * Pollen :: Service + * %% + * 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.apache.commons.lang3.time.DateUtils; import org.chorem.pollen.persistence.entity.ChoiceType; +import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.services.AbstractPollenServiceTest; import org.chorem.pollen.services.bean.ChoiceBean; -import org.chorem.pollen.services.bean.CreateBeanRef; import org.chorem.pollen.services.bean.PollBean; +import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.VoteBean; import org.chorem.pollen.services.bean.VoteToChoiceBean; import org.chorem.pollen.votecounting.model.ChoiceScore; @@ -72,9 +94,9 @@ public class VoteCountingServiceTest extends AbstractPollenServiceTest { choice2.setDescription("Choice B"); choices.add(choice2); - CreateBeanRef createdPollRef = pollService.createPoll(poll, choices, null); + PollenEntityRef<Poll> createdPollRef = pollService.createPoll(poll, choices, null); - String pollId = createdPollRef.getId(); + String pollId = createdPollRef.getEntityId(); List<ChoiceBean> createdChoices = choiceService.getChoices(pollId); ChoiceBean createdChoiceBean1 = createdChoices.get(0); diff --git a/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteServiceTest.java b/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteServiceTest.java index 78e69bc..a5ca140 100644 --- a/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteServiceTest.java +++ b/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteServiceTest.java @@ -1,5 +1,26 @@ package org.chorem.pollen.services.service; +/* + * #%L + * Pollen :: Service + * %% + * 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.services.AbstractPollenServiceTest; import org.chorem.pollen.services.service.security.SecurityService; import org.junit.Test; -- To stop receiving notification emails like this one, please contact Chorem.org SCM administrator <admin+scm@chorem.org>.