Author: tchemit Date: 2013-06-11 11:05:14 +0200 (Tue, 11 Jun 2013) New Revision: 3820 Url: http://chorem.org/projects/pollen/repository/revisions/3820 Log: continue... Added: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/AbstractJpaPollenEntity.java branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/JpaPollenEntity.java branches/pollen-2.0/pollen-rest-api/src/jetty/ branches/pollen-2.0/pollen-rest-api/src/jetty/jetty-context.xml branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenRender.java branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/ branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/ branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/FakePollenServiceContext.java branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/UserServiceTest.java branches/pollen-2.0/pollen-rest-api/src/test/resources/pollen.properties Removed: branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/service/ Modified: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/BookmarkableEntity.java branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/PollenEntities.java branches/pollen-2.0/pollen-persistence/src/main/xmi/pollen.properties branches/pollen-2.0/pollen-persistence/src/main/xmi/pollen.zargo branches/pollen-2.0/pollen-rest-api/pom.xml branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenJpaTransactionFilter.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceListener.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/CommentService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/FavoriteListService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/UserService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoterListService.java branches/pollen-2.0/pollen-rest-api/src/main/resources/mapping branches/pollen-2.0/pollen-rest-api/src/main/webapp/WEB-INF/web.xml branches/pollen-2.0/pollen-rest-api/src/test/resources/log4j.properties branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/DefaultPollenServiceContext.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollenServiceContext.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/UserService.java branches/pollen-2.0/pollen-ui-js/src/main/webapp/css/bootstrap.min.css branches/pollen-2.0/pollen-ui-js/src/main/webapp/css/style.css branches/pollen-2.0/pollen-ui-js/src/main/webapp/img/glyphicons-halflings-white.png branches/pollen-2.0/pollen-ui-js/src/main/webapp/img/glyphicons-halflings.png branches/pollen-2.0/pollen-ui-js/src/main/webapp/index.html branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/controls/menu.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/controls/poll_form.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/controls/poll_summary.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/controls/vote.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/bootstrap.min.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.fixture.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.jquery.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.jquery.min.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.object.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.observe.attributes.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.observe.delegate.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/models/comments.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/models/polls.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/models/votes.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/route.js branches/pollen-2.0/pollen-ui-js/src/main/webapp/views/menu.ejs branches/pollen-2.0/pollen-ui-js/src/main/webapp/views/poll_form.ejs branches/pollen-2.0/pollen-ui-js/src/main/webapp/views/summary.ejs branches/pollen-2.0/pollen-ui-js/src/main/webapp/views/vote.ejs branches/pollen-2.0/pom.xml Added: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/AbstractJpaPollenEntity.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/AbstractJpaPollenEntity.java (rev 0) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/AbstractJpaPollenEntity.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -0,0 +1,44 @@ +package org.chorem.pollen.persistence.entity; + +/* + * #%L + * Pollen :: Persistence + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 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.jpa.api.AbstractJpaEntity; + +import java.io.Serializable; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public abstract class AbstractJpaPollenEntity extends AbstractJpaEntity implements JpaPollenEntity { + + private static final long serialVersionUID = 1L; + + @Override + public String getSimpleId() { + return PollenEntities.getSimpleId(this); + } +} Property changes on: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/AbstractJpaPollenEntity.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/BookmarkableEntity.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/BookmarkableEntity.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/BookmarkableEntity.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -35,7 +35,7 @@ String getSimpleId(); - PollenUser getPollenUser(); +// PollenUser getPollenUser(); - void setPollenUser(PollenUser pollenUser); +// void setPollenUser(PollenUser pollenUser); } Added: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/JpaPollenEntity.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/JpaPollenEntity.java (rev 0) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/JpaPollenEntity.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -0,0 +1,38 @@ +package org.chorem.pollen.persistence.entity; + +/* + * #%L + * Pollen :: Persistence + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 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.jpa.api.JpaEntity; + +import java.io.Serializable; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public interface JpaPollenEntity extends JpaEntity, Serializable { + String getSimpleId(); +} Property changes on: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/JpaPollenEntity.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/PollenEntities.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/PollenEntities.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/entity/PollenEntities.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -34,9 +34,15 @@ */ public class PollenEntities { + public static boolean isSimpleId(String id) { + return !id.contains("_"); + } + public static String getSimpleId(JpaEntity e) { - String id = e.getId(); - String result = StringUtils.substringAfter(id, "_").replaceAll("-", ""); + String result = e.getId(); + if (result != null) { + result = StringUtils.substringAfter(result, "_").replaceAll("-", ""); + } return result; } Modified: branches/pollen-2.0/pollen-persistence/src/main/xmi/pollen.properties =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/xmi/pollen.properties 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-persistence/src/main/xmi/pollen.properties 2013-06-11 09:05:14 UTC (rev 3820) @@ -22,7 +22,7 @@ ### model.tagValue.idFactory=true model.tagvalue.version=2.0 -model.tagvalue.constantPrefix=PROPERTY_ +model.tagvalue.entitySuperClass=org.chorem.pollen.persistence.entity.AbstractJpaPollenEntity org.chorem.pollen.persistence.entity.Poll.attribute.choice.stereotype=ordered org.chorem.pollen.persistence.entity.Poll.attribute.voterList.stereotype=ordered Modified: branches/pollen-2.0/pollen-persistence/src/main/xmi/pollen.zargo =================================================================== (Binary files differ) Modified: branches/pollen-2.0/pollen-rest-api/pom.xml =================================================================== --- branches/pollen-2.0/pollen-rest-api/pom.xml 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/pom.xml 2013-06-11 09:05:14 UTC (rev 3820) @@ -1,5 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> @@ -10,6 +12,7 @@ <groupId>org.chorem.pollen</groupId> <artifactId>pollen-rest-api</artifactId> + <packaging>war</packaging> <name>Pollen :: Rest Api</name> <description>Pollen Rest Api</description> @@ -59,10 +62,15 @@ </dependency> <dependency> + <groupId>javax</groupId> + <artifactId>javaee-api</artifactId> + </dependency> + + <!--dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <scope>provided</scope> - </dependency> + </dependency--> <dependency> <groupId>commons-logging</groupId> @@ -121,7 +129,7 @@ <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> - <scope>test</scope> + <scope>runtime</scope> </dependency> <dependency> @@ -138,4 +146,80 @@ </dependencies> + <properties> + <defaultWebContextPath>pollen</defaultWebContextPath> + <defaultLogDir>${basedir}/target</defaultLogDir> + <defaultDbDir>${basedir}/target</defaultDbDir> + </properties> + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.mortbay.jetty</groupId> + <artifactId>jetty-maven-plugin</artifactId> + <configuration> + <stopKey>B</stopKey> + <stopPort>1270</stopPort> + <contextXml>${basedir}/src/jetty/jetty-context.xml</contextXml> + <webAppConfig> + <contextPath>/${defaultWebContextPath}</contextPath> + </webAppConfig> + <systemProperties> + <systemProperty> + <name>testDirectory</name> + <value>${defaultLogDir}</value> + </systemProperty> + <systemProperty> + <name>testDirectory</name> + <value>${defaultDbDir}</value> + </systemProperty> + </systemProperties> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.tomcat.maven</groupId> + <artifactId>tomcat7-maven-plugin</artifactId> + <configuration> + <path>/${defaultWebContextPath}</path> + <systemProperties> + <testDirectory>${defaultDbDir}</testDirectory> + <pollen.log.dir>${defaultLogDir}</pollen.log.dir> + </systemProperties> + <uriEncoding>UTF-8</uriEncoding> + </configuration> + </plugin> + </plugins> + </pluginManagement> + </build> + + <profiles> + <profile> + <id>test</id> + + <build> + <resources> + <resource> + <directory>src/test/resources</directory> + <includes> + <include>**/*</include> + </includes> + <filtering>true</filtering> + </resource> + </resources> + </build> + <properties> + + </properties> + <dependencies> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-jcl</artifactId> + <scope>runtime</scope> + </dependency> + </dependencies> + </profile> + + </profiles> + </project> Added: branches/pollen-2.0/pollen-rest-api/src/jetty/jetty-context.xml =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/jetty/jetty-context.xml (rev 0) +++ branches/pollen-2.0/pollen-rest-api/src/jetty/jetty-context.xml 2013-06-11 09:05:14 UTC (rev 3820) @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + #%L + Pollen :: UI (struts2) + $Id$ + $HeadURL$ + %% + Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + %% + 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% + --> + +<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> + +<Configure class="org.eclipse.jetty.webapp.WebAppContext"> + <Call name="setAttribute"> + <Arg>org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern</Arg> + <Arg>.*/.*jsp-api-[^/]\.jar$|./.*jsp-[^/]\.jar$|./.*taglibs[^/]*\.jar$</Arg> + </Call> +</Configure> \ No newline at end of file Property changes on: branches/pollen-2.0/pollen-rest-api/src/jetty/jetty-context.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenJpaTransactionFilter.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenJpaTransactionFilter.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenJpaTransactionFilter.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -50,10 +50,20 @@ log.info("Init JPA Filter"); } - applicationContext = PollenServices.getApplicationContext( - filterConfig.getServletContext()); +// applicationContext = PollenServices.getApplicationContext( +// filterConfig.getServletContext()); +// +// Preconditions.checkNotNull(applicationContext, "Could not find application context at ServletContext#pollen_PollenApplicationContext"); + } - Preconditions.checkNotNull(applicationContext, "Could not find application context at ServletContext#pollen_PollenApplicationContext"); + protected PollenApplicationContext getApplicationContext(ServletRequest request) { + if (applicationContext == null) { + applicationContext = PollenServices.getApplicationContext( + request.getServletContext()); + + Preconditions.checkNotNull(applicationContext, "Could not find application context at ServletContext#pollen_PollenApplicationContext"); + } + return applicationContext; } @Override @@ -63,7 +73,7 @@ try { - entityManager = applicationContext.newEntityManager(); + entityManager = getApplicationContext(request).newEntityManager(); } catch (PersistenceException e) { Added: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenRender.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenRender.java (rev 0) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenRender.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -0,0 +1,243 @@ +package org.chorem.pollen.rest; + +/* + * #%L + * Pollen :: Rest Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 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.common.collect.Maps; +import com.google.gson.Gson; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.persistence.entity.JpaPollenEntity; +import org.debux.webmotion.server.call.Call; +import org.debux.webmotion.server.call.HttpContext; +import org.debux.webmotion.server.mapping.Mapping; +import org.debux.webmotion.server.render.Render; +import org.nuiton.jpa.api.JpaEntity; +import org.nuiton.util.ObjectUtil; +import org.nuiton.util.beans.Binder; +import org.nuiton.util.beans.BinderFactory; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * To render + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class PollenRender<T> extends Render { + + + /** + * To specify entity collections to include in binded entities. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.METHOD) + public @interface ExposeCollection { + + public String[] values() default {}; + } + + protected T model; + + public PollenRender(T model) { + this.model = model; + } + + @Override + public void create(Mapping mapping, Call call) throws IOException, ServletException { + + HttpContext context = call.getContext(); + HttpServletResponse response = context.getResponse(); + response.setContentType("application/json"); + + String[] includeCollection = null; + ExposeCollection annotation = call.getCurrent().getMethod().getAnnotation(ExposeCollection.class); + if (annotation != null) { + includeCollection = annotation.values(); + } + Object map = toMap(model, includeCollection); + + Gson gson = new Gson(); + String json = gson.toJson(map); + PrintWriter out = context.getOut(); + out.print(json); + } + + protected <M> Object toMap(M model, String... includeCollection) { + if (model instanceof Iterable<?>) { + + // collection of objects + Iterable<?> objects = (Iterable<?>) model; + List<Object> result = Lists.newArrayList(); + for (Object object : objects) { + Object objectMap = toMap(object, includeCollection); + result.add(objectMap); + } + return result; + } + + // single object + + if (model instanceof JpaPollenEntity) { + + // entity need to transform it + + JpaPollenEntity jpaEntity = (JpaPollenEntity) model; + + PollenEntityBinder<JpaPollenEntity> binder = BinderFactory.newBinder(jpaEntity.getClass(), jpaEntity.getClass(), null, PollenEntityBinder.class); + Map<String, Object> map = binder.obtainProperties(jpaEntity); + Map<String, Object> result = Maps.newTreeMap(); + + for (Map.Entry<String, Object> entry : map.entrySet()) { + String propertyName = entry.getKey(); + Object propertyValue = entry.getValue(); + if (propertyValue instanceof JpaPollenEntity) { + result.put(propertyName, toMap(propertyValue)); + continue; + } + + result.put(propertyName, propertyValue); + } + + // replace id to simpleId + result.put(JpaEntity.PROPERTY_ID, jpaEntity.getSimpleId()); + + // treat collections + + if (includeCollection != null) { + for (String propertyName : includeCollection) { + Iterable<?> collectionValue = binder.getCollectionValue(jpaEntity, propertyName); + result.put(propertyName, toMap(collectionValue)); + + } + } + return result; + + } + + return model; + + + } + + protected static class PollenEntityBinder<E extends JpaPollenEntity> extends Binder<E, E> { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(PollenEntityBinder.class); + + private static final long serialVersionUID = 1L; + + protected List<String> simpleProperties; + + public PollenEntityBinder() { + + } + + @Override + protected void setModel(BinderModel<E, E> model) { + super.setModel(model); + this.simpleProperties = Lists.newArrayList(); + for (String property : model.getSourceDescriptors()) { + if (model.getCollectionType(property)==null) { + simpleProperties.add(property); + } + } + } + + /** + * Obtain from the given object all properties registered in the binder + * model. + * + * @param source the bean to read + * @return the map of properties obtained indexed by their property name, + * or an empty map is the given {@code from} is {@code null}. + * @since 2.3 + */ + public Map<String, Object> obtainProperties(E source) { + if (source == null) { + // special limit case + return Collections.emptyMap(); + } + + Map<String, Object> result = Maps.newTreeMap(); + for (String sourceProperty : simpleProperties) { + + + try { + Object read; + Method readMethod = model.getSourceReadMethod(sourceProperty); + read = readMethod.invoke(source); + if (log.isDebugEnabled()) { + log.debug("property " + sourceProperty + ", type : " + + readMethod.getReturnType() + ", value = " + read); + } + if (readMethod.getReturnType().isPrimitive() && + ObjectUtil.getNullValue( + readMethod.getReturnType()).equals(read)) { + // for primitive type case, force nullity + read = null; + } + + if (read != null) { + result.put(sourceProperty, read); + } + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + return result; + } + + public Iterable<?> getCollectionValue(E source, String sourceProperty) { + try { + Method readMethod = model.getSourceReadMethod(sourceProperty); + Object result = readMethod.invoke(source); + return (Iterable<?>) result; + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + } +} Property changes on: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenRender.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceListener.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceListener.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceListener.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -23,18 +23,28 @@ * #L% */ +import com.google.common.collect.Maps; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.persistence.entity.Comment; +import org.chorem.pollen.persistence.entity.Poll; +import org.chorem.pollen.persistence.entity.PollenEntities; +import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.UserEmailAlreadyUsedException; +import org.chorem.pollen.service.exception.UserLoginAlreadyUsedException; import org.debux.webmotion.server.WebMotionServerListener; import org.debux.webmotion.server.call.Call; import org.debux.webmotion.server.call.HttpContext; import org.debux.webmotion.server.call.ServerContext; import org.debux.webmotion.server.handler.ExecutorParametersInjectorHandler; import org.debux.webmotion.server.mapping.Mapping; +import org.nuiton.jpa.api.JpaEntity; +import javax.persistence.EntityManager; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Type; +import java.util.Map; /** * TODO @@ -52,6 +62,7 @@ public void onStart(Mapping mapping, ServerContext serverContext) { serverContext.addInjector(new PollenServiceContextInjector()); + serverContext.addInjector(new PollenIdInjector()); // Create application context PollenApplicationContext applicationContext = @@ -60,6 +71,19 @@ // push it in context PollenServices.setApplicationContext( serverContext.getServletContext(), applicationContext); + + EntityManager entityManager = applicationContext.newEntityManager(); + + try { + PollenServiceContext serviceContext = applicationContext.newServiceContext(entityManager); + serviceContext.getUserService().createDefaultUsers(); + } catch (UserEmailAlreadyUsedException e) { + //Can't happen + } catch (UserLoginAlreadyUsedException e) { + //Can't happen + } finally { + entityManager.close(); + } } @Override @@ -78,7 +102,7 @@ protected static class PollenServiceContextInjector implements ExecutorParametersInjectorHandler.Injector { @Override - public Object getValue(Mapping mapping, Call call, Class<?> type, Type generic) { + public Object getValue(Mapping mapping, Call call, String name, Class<?> type, Type generic) { if (PollenServiceContext.class.isAssignableFrom(type)) { HttpContext httpContext = call.getContext(); HttpServletRequest request = httpContext.getRequest(); @@ -87,4 +111,45 @@ return null; } } + + protected static class PollenIdInjector implements ExecutorParametersInjectorHandler.Injector { + + final Map<String, Class<? extends JpaEntity>> paramMapping; + + public PollenIdInjector() { + paramMapping = Maps.newTreeMap(); + paramMapping.put("userId", PollenUser.class); + paramMapping.put("pollId", Poll.class); + paramMapping.put("commentId", Comment.class); + } + + @Override + public Object getValue(Mapping mapping, + Call call, + String name, + Class<?> type, + Type generic) { + + if (String.class.isAssignableFrom(type) && paramMapping.containsKey(name)) { + + Class<? extends JpaEntity> entityType = paramMapping.get(name); + + Call.ParameterTree parameterTree = call.getParameterTree(); + Map<String, Call.ParameterTree> tree = parameterTree.getTree(); + + Call.ParameterTree paramValue = tree.get(name); + if (paramValue != null) { + String id = String.valueOf(((String[]) paramValue.getValue())[0]); + if (PollenEntities.isSimpleId(id)) { + + // get the full id + String fullId = PollenEntities.getEntityId( + entityType, id); + return fullId; + } + } + } + return null; + } + } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/CommentService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/CommentService.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/CommentService.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -38,9 +38,9 @@ */ public class CommentService extends WebMotionController { - public Comment[] getComments(PollenServiceContext context, String pollId) throws EntityNotFoundException { + public List<Comment> getComments(PollenServiceContext context, String pollId) throws EntityNotFoundException { List<Comment> comments = context.getCommentService().getComments(pollId); - return comments.toArray(new Comment[comments.size()]); + return comments; } public Comment getComment(PollenServiceContext context, String commentId) throws EntityNotFoundException { Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/FavoriteListService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/FavoriteListService.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/FavoriteListService.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -39,9 +39,9 @@ */ public class FavoriteListService extends WebMotionController { - public FavoriteList[] getFavoriteLists(PollenServiceContext context, String userId) throws EntityNotFoundException { + public List<FavoriteList> getFavoriteLists(PollenServiceContext context, String userId) throws EntityNotFoundException { List<FavoriteList> favoriteLists = context.getFavoriteListService().getFavoriteLists(userId); - return favoriteLists.toArray(new FavoriteList[favoriteLists.size()]); + return favoriteLists; } public FavoriteList getFavoriteList(PollenServiceContext context, String favoriteListId) throws EntityNotFoundException { Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollService.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollService.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -29,6 +29,7 @@ import org.chorem.pollen.service.exception.EntityNotFoundException; import org.debux.webmotion.server.WebMotionController; +import javax.servlet.annotation.WebFilter; import java.io.File; import java.util.List; import java.util.Set; @@ -41,19 +42,24 @@ */ public class PollService extends WebMotionController { - public Poll[] getCreatedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { + public Set<Poll> getPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { + Set<Poll> polls = context.getPollService().getPolls(userId); + return polls; + } + + public Set<Poll> getCreatedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { Set<Poll> polls = context.getPollService().getCreatedPolls(userId); - return polls.toArray(new Poll[polls.size()]); + return polls; } - public Poll[] getInvitedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { + public Set<Poll> getInvitedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { Set<Poll> polls = context.getPollService().getInvitedPolls(userId); - return polls.toArray(new Poll[polls.size()]); + return polls; } - public Poll[] getParticipatedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { + public Set<Poll> getParticipatedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { Set<Poll> polls = context.getPollService().getParticipatedPolls(userId); - return polls.toArray(new Poll[polls.size()]); + return polls; } public Poll createPoll(PollenServiceContext context, String userId, Poll poll) throws EntityNotFoundException { Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/UserService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/UserService.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/UserService.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -42,9 +42,9 @@ */ public class UserService extends WebMotionController { - public PollenUser[] getUsers(PollenServiceContext context) { + public List<PollenUser> getUsers(PollenServiceContext context) { List<PollenUser> users = context.getUserService().getUsers(); - return users.toArray(new PollenUser[users.size()]); + return users; } public PollenUser getUser(PollenServiceContext context, Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteService.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteService.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -38,9 +38,9 @@ */ public class VoteService extends WebMotionController { - public Vote[] getVotes(PollenServiceContext context, String pollId) throws EntityNotFoundException { + public List<Vote> getVotes(PollenServiceContext context, String pollId) throws EntityNotFoundException { List<Vote> votes = context.getVoteService().getVotes(pollId); - return votes.toArray(new Vote[votes.size()]); + return votes; } public Vote getVote(PollenServiceContext context, String voteId) throws EntityNotFoundException { Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoterListService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoterListService.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoterListService.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -44,9 +44,9 @@ return context.getVoterListService().importFavoriteList(pollId, favoriteListId); } - public VoterList[] getVoterLists(PollenServiceContext context, String pollId) throws EntityNotFoundException { + public List<VoterList> getVoterLists(PollenServiceContext context, String pollId) throws EntityNotFoundException { List<VoterList> voterLists = context.getVoterListService().getVoterLists(pollId); - return voterLists.toArray(new VoterList[voterLists.size()]); + return voterLists; } public VoterList getVoterList(PollenServiceContext context, String voterListId) throws EntityNotFoundException { Modified: branches/pollen-2.0/pollen-rest-api/src/main/resources/mapping =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/resources/mapping 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/resources/mapping 2013-06-11 09:05:14 UTC (rev 3820) @@ -1,101 +1,104 @@ [config] package.filters=org.chorem.pollen.rest package.actions=org.chorem.pollen.rest.api +package.errors=org.chorem.pollen.rest.api server.listener.class=org.chorem.pollen.rest.PollenServiceListener +default.render=org.chorem.pollen.rest.PollenRender [filters] * /* PollenServiceContextFilter.inject -* /* PollenIdOrTokenInjector.inject -* /* PollenSecurityFilter.inject (prend le param auth pour retrouver le PollenUser) +#* /* PollenIdOrTokenInjector.inject +#* /* PollenSecurityFilter.inject (prend le param auth pour retrouver le PollenUser) #- une fois un object lié à PollenUser, c'est immuable. [errors] -org.chorem.pollen.service.exception.EntityNotFoundException action:ErrorAction.on402 -org.chorem.pollen.service.exception.UserEmailAlreadyUsedException action:ErrorAction.on500 -org.chorem.pollen.service.exception.UserLoginAlreadyUsedException action:ErrorAction.on500 -org.chorem.pollen.service.exception.UserInvalidPasswordException action:ErrorAction.on500 -org.chorem.pollen.service.exception.UserInvalidEmailActiviationTokenException action:ErrorAction.on500 +org.chorem.pollen.service.exception.EntityNotFoundException ErrorAction.on404 +org.chorem.pollen.service.exception.UserEmailAlreadyUsedException ErrorAction.on500 +org.chorem.pollen.service.exception.UserLoginAlreadyUsedException ErrorAction.on500 +org.chorem.pollen.service.exception.UserInvalidPasswordException ErrorAction.on500 +org.chorem.pollen.service.exception.UserInvalidEmailActiviationTokenException ErrorAction.on500 [actions] # AuthService -PUT /login action:AuthService.login -GET /lostpassword/{token} action:AuthService.lostPassword -GET /logout action:AuthService.logout +PUT /login AuthService.login +GET /lostpassword/{token} AuthService.lostPassword +GET /logout AuthService.logout # CommentService -GET /polls/{pollId}/comments action:CommentService.getComments -POST /polls/{pollId}/comments action:CommentService.addComment -GET /comments/{commentId} action:CommentService.getComment -PUT /comments action:CommentService.editComment -DELETE /polls/{pollId}/comments/{commentId} action:CommentService.deleteComment +GET /polls/{pollId}/comments CommentService.getComments +POST /polls/{pollId}/comments CommentService.addComment +GET /comments/{commentId} CommentService.getComment +PUT /comments CommentService.editComment +DELETE /polls/{pollId}/comments/{commentId} CommentService.deleteComment # FavoriteListService -GET /favoriteLists action:FavoriteList.getFavoriteLists -GET /favoriteLists/{flId} action:FavoriteList.getFavoriteList -POST /favoriteLists action:FavoriteList.createFavoriteList -PUT /favoriteLists action:FavoriteList.editFavoriteList -DELETE /favoriteLists/{flId} action:FavoriteList.deleteFavoriteList -GET /favoriteLists/{flId}/members action:FavoriteList.getMembers -GET /favoriteListMembers/{mId} action:FavoriteList.getMember -POST /favoriteLists/{flId}/members action:FavoriteList.addMember -PUT /favoriteListMembers action:FavoriteList.editMember -DELETE /favoriteLists/{flId}/members/{mId} action:FavoriteList.removeMember +GET /favoriteLists FavoriteListService.getFavoriteLists +GET /favoriteLists/{flId} FavoriteListService.getFavoriteList +POST /favoriteLists FavoriteListService.createFavoriteList +PUT /favoriteLists FavoriteListService.editFavoriteList +DELETE /favoriteLists/{flId} FavoriteListService.deleteFavoriteList +GET /favoriteLists/{flId}/members FavoriteListService.getMembers +GET /favoriteListMembers/{mId} FavoriteListService.getMember +POST /favoriteLists/{flId}/members FavoriteListService.addMember +PUT /favoriteListMembers FavoriteListService.editMember +DELETE /favoriteLists/{flId}/members/{mId} FavoriteListService.removeMember # PollService -GET /polls action:PollService.getPolls -GET /polls/created action:PollService.getCreatedPolls -GET /polls/invited action:PollService.getInvitedPolls -GET /polls/participated action:PollService.getParticipatedPolls -PUT /polls action:PollService.createPoll -PUT /polls action:PollService.editPoll -DELETE /polls/{pollOrTokenId} action:PollService.deletePoll -POST /polls/{pollId} action:PollService.clonePoll -GET /polls/{pollId}/export action:PollService.exportPoll -PUT /polls/{pollId}/close action:PollService.closePoll +GET /polls PollService.getPolls +GET /polls/created PollService.getCreatedPolls +GET /polls/invited PollService.getInvitedPolls +GET /polls/participated PollService.getParticipatedPolls +POST /polls PollService.createPoll +PUT /polls PollService.editPoll +GET /polls/{pollId} PollService.getPoll +DELETE /polls/{pollId} PollService.deletePoll +POST /polls/{pollId} PollService.clonePoll +GET /polls/{pollId}/export PollService.exportPoll +PUT /polls/{pollId}/close PollService.closePoll -GET /polls/{pollId}/choices action:PollService.getChoices -POST /polls/{pollId}/choices action:PollService.addChoice -GET /choices/{choiceId} action:PollService.getChoice -PUT /choices action:PollService.editChoice -DELETE /polls/{pollId}/choices/{choiceId} action:PollService.deleteChoice +GET /polls/{pollId}/choices PollService.getChoices +POST /polls/{pollId}/choices PollService.addChoice +GET /choices/{choiceId} PollService.getChoice +PUT /choices PollService.editChoice +DELETE /polls/{pollId}/choices/{choiceId} PollService.deleteChoice # UserService -GET /users action:UserService.getUsers -GET /users/{userId} action:UserService.getUser -POST /users action:UserService.createUser -PUT /users action:UserService.editUser -GET /validateemail/{token} action:UserService.validateUserEmail +GET /users UserService.getUsers +GET /users/{userId} UserService.getUser +POST /users UserService.createUser +PUT /users UserService.editUser +PUT /users/{userId}?token={} UserService.validateUserEmail # VoteCountingService -GET /polls/{pollId}/results action:VoteCountingService.getResult +GET /polls/{pollId}/results VoteCountingService.getResult # VoterListService -PUT /polls/{pollId}/favoriteLists/{flId} action:VoterListService.importFavoriteList -GET /polls/{pollId}/voterLists action:VoterListService.getVoterLists -GET /voterLists/{vlId} action:VoterListService.getVoterList -PUT /voterLists/{vlId} action:VoterListService.editVoterList -DELETE /polls/{pollId}/voterLists/{vlId} action:VoterListService.deleteVoterList +PUT /polls/{pollId}/favoriteLists/{flId} VoterListService.importFavoriteList +GET /polls/{pollId}/voterLists VoterListService.getVoterLists +GET /voterLists/{vlId} VoterListService.getVoterList +PUT /voterLists/{vlId} VoterListService.editVoterList +DELETE /polls/{pollId}/voterLists/{vlId} VoterListService.deleteVoterList -GET /voterLists/{vlId}/members action:VoterListService.getMembers -GET /voterListMembers/{mId} action:VoterListService.getMember -POST /voterLists/{vlId}/members action:VoterListService.addMember -PUT /voterListMembers action:VoterListService.editMember -DELETE /voterLists/{vlId}/members/{mId} action:VoterListService.deleteMember +GET /voterLists/{vlId}/members VoterListService.getMembers +GET /voterListMembers/{mId} VoterListService.getMember +POST /voterLists/{vlId}/members VoterListService.addMember +PUT /voterListMembers VoterListService.editMember +DELETE /voterLists/{vlId}/members/{mId} VoterListService.deleteMember # VoteService -GET /polls/{pollId}/votes action:VoteService.getVotes -PUT /polls/{pollId}/votes action:VoteService.addVote -GET /votes/{voteId} action:VoteService.getVote -PUT /votes action:VoteService.editVote -DELETE /polls/{pollId}/votes/{voteId} action:VoteService.deleteVote +GET /polls/{pollId}/votes VoteService.getVotes +PUT /polls/{pollId}/votes VoteService.addVote +GET /votes/{voteId} VoteService.getVote +PUT /votes VoteService.editVote +DELETE /polls/{pollId}/votes/{voteId} VoteService.deleteVote Modified: branches/pollen-2.0/pollen-rest-api/src/main/webapp/WEB-INF/web.xml =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/webapp/WEB-INF/web.xml 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/main/webapp/WEB-INF/web.xml 2013-06-11 09:05:14 UTC (rev 3820) @@ -22,17 +22,22 @@ #L% --> -<web-app version="3.0" +<web-app version="3.0" id="pollen" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> - <!-- workarround for webmotion bug http://projects.debux.org/issues/293 --> - <!--session-config> - <tracking-mode>COOKIE</tracking-mode> - </session-config--> + <display-name>Pollen REST Api</display-name> + <listener> + <listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class> + </listener> + + <listener> + <listener-class>org.debux.webmotion.server.WebMotionServletContextListener</listener-class> + </listener> + <filter> <filter-name>jpaTransaction</filter-name> <filter-class> @@ -40,8 +45,24 @@ </filter-class> </filter> + <filter> + <filter-name>WebMotionServer</filter-name> + <filter-class>org.debux.webmotion.server.WebMotionServer</filter-class> + <async-supported>true</async-supported> + </filter> + <filter-mapping> <filter-name>jpaTransaction</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> + + <filter-mapping> + <filter-name>WebMotionServer</filter-name> + <url-pattern>/*</url-pattern> + <dispatcher>REQUEST</dispatcher> + <dispatcher>INCLUDE</dispatcher> + <dispatcher>FORWARD</dispatcher> + <dispatcher>ERROR</dispatcher> + </filter-mapping> + </web-app> \ No newline at end of file Added: branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java (rev 0) +++ branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -0,0 +1,121 @@ +package org.chorem.pollen.rest.api; + +/* + * #%L + * Pollen :: Rest Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 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.JpaPollenPersistenceContext; +import org.chorem.pollen.rest.PollenApplicationContext; +import org.chorem.pollen.service.FixturesService; +import org.chorem.pollen.service.PollenFixtures; +import org.chorem.pollen.service.PollenServiceContext; +import org.debux.webmotion.unittest.WebMotionTest; +import org.junit.Before; +import org.junit.Rule; +import org.nuiton.jpa.junit.JpaEntityManagerRule; + +import javax.persistence.EntityManager; +import java.util.Map; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class AbstractPollenRestApiTest extends WebMotionTest { + + protected JpaEntityManagerRule jpaEntityManagerRule; + + protected PollenFixtures fixtures; + + protected PollenApplicationContext applicationContext; + + public PollenApplicationContext getApplicationContext() { + if (applicationContext == null) { + applicationContext = + new PollenApplicationContext() { + @Override + public EntityManager newEntityManager() { + return getJpaEntityManagerRule().getEntityManager(); + } + + @Override + public PollenServiceContext newServiceContext(EntityManager entityManager) { + + FakePollenServiceContext serviceContext = new FakePollenServiceContext(); + + JpaPollenPersistenceContext jpaMagaliePersistenceContext = + new JpaPollenPersistenceContext(entityManager); + + serviceContext.setPersistenceContext(jpaMagaliePersistenceContext); + + serviceContext.setPollenServiceConfig(applicationConfig); + + return serviceContext; + + } + }; + } + return applicationContext; + } + + @Rule + public JpaEntityManagerRule getJpaEntityManagerRule() { + + if (jpaEntityManagerRule == null) { + + Map<String, String> jpaParameters = getApplicationContext().getPollenServiceConfig().getJpaParameters(); + + jpaEntityManagerRule = new JpaEntityManagerRule("pollenPersistenceUnit", jpaParameters); + } + + return jpaEntityManagerRule; + } + + protected PollenServiceContext getServiceContext() { + + EntityManager entityManager = getJpaEntityManagerRule().getEntityManager(); + + PollenServiceContext result = applicationContext.newServiceContext(entityManager); + return result; + } + + protected void loadFixtures(String fixturesSetName) { + + FixturesService fixturesService = getServiceContext().getFixturesService(); + + fixtures = fixturesService.loadFixtures(fixturesSetName); + + } + + protected <E> E fixture(String id) { + + return fixtures.fixture(id); + + } + + @Before + public void launchServer() throws Exception { + runServer(); + } +} Property changes on: branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/FakePollenServiceContext.java (from rev 3817, branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/FakePollenServiceContext.java) =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/FakePollenServiceContext.java (rev 0) +++ branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/FakePollenServiceContext.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -0,0 +1,53 @@ +package org.chorem.pollen.rest.api; + +/* + * #%L + * Pollen :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 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.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.service.DefaultPollenServiceContext; + +import java.util.Date; + +public class FakePollenServiceContext extends DefaultPollenServiceContext { + + private static final Log log = + LogFactory.getLog(FakePollenServiceContext.class); + + protected Date date; + + @Override + public Date getNow() { + Preconditions.checkState(date != null, "you must provide a date before running service test"); + if (log.isTraceEnabled()) { + log.trace("injecting fake date in service: " + date); + } + return date; + } + + public void setDate(Date date) { + this.date = date; + } + +} Added: branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/UserServiceTest.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/UserServiceTest.java (rev 0) +++ branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/UserServiceTest.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -0,0 +1,84 @@ +package org.chorem.pollen.rest.api; + +/* + * #%L + * Pollen :: Rest Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 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.http.client.fluent.Request; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +@Ignore +public class UserServiceTest extends AbstractPollenRestApiTest { + + + @Before + public void setUp() throws Exception { + + loadFixtures("fixtures"); + } + + @Test + public void testGetUsers() throws Exception { + + Request request = createRequest("/users").Get(); + String result = request.execute().returnContent().asString(); + + assertTrue(result.contains("users")); + + + } + +// @Test +// public void testGetUser() throws Exception { +// +// } +// +// @Test +// public void testCreateUser() throws Exception { +// +// } +// +// @Test +// public void testEditUser() throws Exception { +// +// } +// +// @Test +// public void testValidateUserEmail() throws Exception { +// +// } +// +// @Test +// public void testChangePassword() throws Exception { +// +// } +} Property changes on: branches/pollen-2.0/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/UserServiceTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-rest-api/src/test/resources/log4j.properties =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/test/resources/log4j.properties 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-rest-api/src/test/resources/log4j.properties 2013-06-11 09:05:14 UTC (rev 3820) @@ -20,12 +20,13 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # #L% ### -log4j.rootCategory=ERROR, console +log4j.rootCategory=DEBUG, console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{hh:mm:ss} %5p (%F:%L) %m%n -# log4j.logger.org.chorem.pollen=TRACE +log4j.logger.org.chorem.pollen=TRACE +log4j.logger.org.debux=TRACE # log4j.logger.org.hibernate.tool.hbm2ddl.SchemaExport=FATAL Added: branches/pollen-2.0/pollen-rest-api/src/test/resources/pollen.properties =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/test/resources/pollen.properties (rev 0) +++ branches/pollen-2.0/pollen-rest-api/src/test/resources/pollen.properties 2013-06-11 09:05:14 UTC (rev 3820) @@ -0,0 +1,9 @@ +javax.persistence.jdbc.driver=org.h2.Driver +javax.persistence.jdbc.url=jdbc:h2:file:${testDirectory}/pollen/h2data +javax.persistence.jdbc.user=sa +javax.persistence.jdbc.password= +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.hbm2ddl.auto=update +hibernate.show_sql=false +hibernate.format_sql=true +hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy Property changes on: branches/pollen-2.0/pollen-rest-api/src/test/resources/pollen.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/DefaultPollenServiceContext.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/DefaultPollenServiceContext.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/DefaultPollenServiceContext.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -118,11 +118,16 @@ } @Override + public FixturesService getFixturesService() { + return newService(FixturesService.class); + } + + @Override public String encodePassword(String password) { return StringUtil.encodeMD5(password); } - protected <E extends PollenServiceSupport> E newService(Class<E> serviceClass) { + public <E extends PollenServiceSupport> E newService(Class<E> serviceClass) { E service; Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollService.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollService.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -24,6 +24,7 @@ */ import com.google.common.base.Preconditions; +import com.google.common.collect.Sets; import org.chorem.pollen.persistence.entity.Choice; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.PollenUser; @@ -197,4 +198,8 @@ getPersistenceContext().commit(); } + + public Set<Poll> getPolls(String userId) throws EntityNotFoundException { + return Sets.newHashSet(getPersistenceContext().getPollDao().findAll()); + } } Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollenServiceContext.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollenServiceContext.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollenServiceContext.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -60,6 +60,8 @@ EmailService getEmailService(); + FixturesService getFixturesService(); + String generatePassword(); String encodePassword(String password); Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/UserService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/UserService.java 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/UserService.java 2013-06-11 09:05:14 UTC (rev 3820) @@ -199,6 +199,18 @@ getPersistenceContext().commit(); } + public void createDefaultUsers() throws UserEmailAlreadyUsedException, UserLoginAlreadyUsedException { + + PollenUser user = getPollenUserDao().newInstance(); + + user.setAdministrator(true); + String login = "admin"; + user.setLogin(login); + user.setEmail("admin@pollen.org"); + user.setPassword("admin"); + createUser(user, false); + } + protected void notifyUserCreated(PollenUser user) { //TODO Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/css/bootstrap.min.css ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/css/style.css ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/img/glyphicons-halflings-white.png ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/img/glyphicons-halflings.png ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/index.html ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/controls/menu.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/controls/poll_form.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/controls/poll_summary.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/controls/vote.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/bootstrap.min.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.fixture.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.jquery.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.jquery.min.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.object.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.observe.attributes.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/libs/can.observe.delegate.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/models/comments.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/models/polls.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/models/votes.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/js/route.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/views/menu.ejs ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/views/poll_form.ejs ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/views/summary.ejs ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: branches/pollen-2.0/pollen-ui-js/src/main/webapp/views/vote.ejs ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pom.xml =================================================================== --- branches/pollen-2.0/pom.xml 2013-06-08 12:12:35 UTC (rev 3819) +++ branches/pollen-2.0/pom.xml 2013-06-11 09:05:14 UTC (rev 3820) @@ -169,7 +169,7 @@ <nuitonI18nVersion>2.5.1</nuitonI18nVersion> <nuitonWebVersion>1.13</nuitonWebVersion> - <nuitonUtilsVersion>2.6.12</nuitonUtilsVersion> + <nuitonUtilsVersion>2.6.13-SNAPSHOT</nuitonUtilsVersion> <h2Version>1.3.172</h2Version> <postgresqlVersion>9.1-901-1.jdbc4</postgresqlVersion> <!--<struts2Version>2.3.14.2</struts2Version>--> @@ -406,6 +406,13 @@ </dependency> <dependency> + <groupId>javax</groupId> + <artifactId>javaee-api</artifactId> + <version>6.0</version> + <scope>provided</scope> + </dependency> + + <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-runner</artifactId> <version>${jettyVersion}</version>