Author: cgeer
Date: Sun May  5 20:29:30 2013
New Revision: 1479383

URL: http://svn.apache.org/r1479383
Log:
RAVE-965
- Added an ID to the group object
- Created the Group Repository Interface
- Created the JPA Group Repository implementation and test

Added:
    
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/repository/GroupRepository.java
    
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaGroupRepository.java
    
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/repository/
    
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/repository/impl/
    
rave/trunk/rave-components/rave-jpa/src/test/java/org/apache/rave/portal/repository/impl/JpaGroupRepositoryTest.java
Modified:
    
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/model/Group.java
    
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/impl/GroupImpl.java
    
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/JpaGroup.java
    
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/conversion/JpaGroupConverter.java
    
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaPersonRepository.java

Modified: 
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/model/Group.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/model/Group.java?rev=1479383&r1=1479382&r2=1479383&view=diff
==============================================================================
--- 
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/model/Group.java
 (original)
+++ 
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/model/Group.java
 Sun May  5 20:29:30 2013
@@ -26,6 +26,10 @@ import java.util.List;
  */
 @XmlTransient
 public interface Group {
+    String getId();
+
+    void setId(String id);
+
     String getOwnerId();
 
     void setOwnerId(String ownerId);

Added: 
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/repository/GroupRepository.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/repository/GroupRepository.java?rev=1479383&view=auto
==============================================================================
--- 
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/repository/GroupRepository.java
 (added)
+++ 
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/repository/GroupRepository.java
 Sun May  5 20:29:30 2013
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rave.repository;
+
+import org.apache.rave.model.Group;
+
+public interface GroupRepository extends Repository<Group> {
+
+    Group findByTitle(String title);
+}

Modified: 
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/impl/GroupImpl.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/impl/GroupImpl.java?rev=1479383&r1=1479382&r2=1479383&view=diff
==============================================================================
--- 
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/impl/GroupImpl.java
 (original)
+++ 
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/impl/GroupImpl.java
 Sun May  5 20:29:30 2013
@@ -24,12 +24,23 @@ import java.util.List;
 
 public class GroupImpl implements Group {
 
+    protected String id;
     protected String description;
     protected String owner;
     protected String title;
     protected List<String> members;
 
     @Override
+    public String getId() {
+        return id;
+    }
+
+    @Override
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    @Override
     public String getOwnerId() {
         return owner;
     }

Modified: 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/JpaGroup.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/JpaGroup.java?rev=1479383&r1=1479382&r2=1479383&view=diff
==============================================================================
--- 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/JpaGroup.java
 (original)
+++ 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/JpaGroup.java
 Sun May  5 20:29:30 2013
@@ -32,12 +32,12 @@ import java.util.List;
 @Access(AccessType.FIELD)
 @Table(name = "groups")
 @NamedQueries(
-        @NamedQuery(name = JpaGroup.FIND_BY_TITLE, query="select g from 
JpaGroup g where g.title = :groupId")
+        @NamedQuery(name = JpaGroup.FIND_BY_TITLE, query="select g from 
JpaGroup g where g.title = :title")
 )
 public class JpaGroup implements BasicEntity, Group {
 
-    public static final String FIND_BY_TITLE = "Group.findById";
-    public static final String GROUP_ID_PARAM = "groupId";
+    public static final String FIND_BY_TITLE = "Group.findByTitle";
+    public static final String GROUP_TITLE_PARAM = "title";
 
     /**
      * The internal object ID used for references to this object. Should be 
generated by the
@@ -67,6 +67,16 @@ public class JpaGroup implements BasicEn
     @Column(name= "person_Id")
     private List<String> members;
 
+    @Override
+    public String getId() {
+        return entityId.toString();
+    }
+
+    @Override
+    public void setId(String id) {
+        entityId = Long.parseLong(id);
+    }
+
     public String getOwnerId() {
         return owner;
     }

Modified: 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/conversion/JpaGroupConverter.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/conversion/JpaGroupConverter.java?rev=1479383&r1=1479382&r2=1479383&view=diff
==============================================================================
--- 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/conversion/JpaGroupConverter.java
 (original)
+++ 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/model/conversion/JpaGroupConverter.java
 Sun May  5 20:29:30 2013
@@ -48,7 +48,7 @@ public class JpaGroupConverter implement
         JpaGroup converted = null;
         if (source != null) {
             TypedQuery<JpaGroup> query = 
manager.createNamedQuery(JpaGroup.FIND_BY_TITLE, JpaGroup.class);
-            query.setParameter(JpaGroup.GROUP_ID_PARAM, source.getTitle());
+            query.setParameter(JpaGroup.GROUP_TITLE_PARAM, source.getTitle());
             converted = getSingleResult(query.getResultList());
 
             if (converted == null) {

Added: 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaGroupRepository.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaGroupRepository.java?rev=1479383&view=auto
==============================================================================
--- 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaGroupRepository.java
 (added)
+++ 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaGroupRepository.java
 Sun May  5 20:29:30 2013
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rave.portal.repository.impl;
+
+import org.apache.rave.model.Group;
+import org.apache.rave.portal.model.JpaGroup;
+import org.apache.rave.portal.model.conversion.JpaGroupConverter;
+import org.apache.rave.repository.GroupRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Repository;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.TypedQuery;
+
+import static org.apache.rave.persistence.jpa.util.JpaUtil.getSingleResult;
+import static org.apache.rave.persistence.jpa.util.JpaUtil.saveOrUpdate;
+
+@Repository
+public class JpaGroupRepository implements GroupRepository {
+
+    @Autowired
+    private JpaGroupConverter converter;
+
+    @PersistenceContext
+    private EntityManager manager;
+
+    @Override
+    public Class<? extends Group> getType() {
+        return JpaGroup.class;
+    }
+
+    @Override
+    public Group get(String id) {
+        long primaryKey = Long.parseLong(id);
+        return manager.find(JpaGroup.class, primaryKey);
+    }
+
+    @Override
+    public Group save(Group item) {
+        JpaGroup converted = converter.convert(item);
+        return saveOrUpdate(converted.getEntityId(), manager, converted);
+    }
+
+    @Override
+    public void delete(Group item) {
+        manager.remove(converter.convert(item));
+    }
+
+    @Override
+    public Group findByTitle(String title) {
+        TypedQuery<JpaGroup> query = 
manager.createNamedQuery(JpaGroup.FIND_BY_TITLE, JpaGroup.class);
+        query.setParameter(JpaGroup.GROUP_TITLE_PARAM, title);
+        return getSingleResult(query.getResultList());
+    }
+}

Modified: 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaPersonRepository.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaPersonRepository.java?rev=1479383&r1=1479382&r2=1479383&view=diff
==============================================================================
--- 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaPersonRepository.java
 (original)
+++ 
rave/trunk/rave-components/rave-jpa/src/main/java/org/apache/rave/portal/repository/impl/JpaPersonRepository.java
 Sun May  5 20:29:30 2013
@@ -138,7 +138,7 @@ public class JpaPersonRepository impleme
     @Override
     public List<Person> findByGroup(String groupId) {
         TypedQuery<JpaGroup> query = 
manager.createNamedQuery(JpaGroup.FIND_BY_TITLE, JpaGroup.class);
-        query.setParameter(JpaGroup.GROUP_ID_PARAM, groupId);
+        query.setParameter(JpaGroup.GROUP_TITLE_PARAM, groupId);
         return getPeopleByIds(getSingleResult(query.getResultList()));
     }
 

Added: 
rave/trunk/rave-components/rave-jpa/src/test/java/org/apache/rave/portal/repository/impl/JpaGroupRepositoryTest.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-jpa/src/test/java/org/apache/rave/portal/repository/impl/JpaGroupRepositoryTest.java?rev=1479383&view=auto
==============================================================================
--- 
rave/trunk/rave-components/rave-jpa/src/test/java/org/apache/rave/portal/repository/impl/JpaGroupRepositoryTest.java
 (added)
+++ 
rave/trunk/rave-components/rave-jpa/src/test/java/org/apache/rave/portal/repository/impl/JpaGroupRepositoryTest.java
 Sun May  5 20:29:30 2013
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rave.portal.repository.impl;
+
+import org.apache.rave.model.Group;
+import org.apache.rave.portal.model.JpaGroup;
+import org.apache.rave.repository.GroupRepository;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+@Transactional
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {"classpath:test-dataContext.xml", 
"classpath:test-applicationContext.xml"})
+public class JpaGroupRepositoryTest {
+
+    private static final String GROUP_ID = "1";
+    private static final String GROUP_TITLE = "Party";
+
+    private static final String INVALID_GROUP_ID = "99";
+    private static final String INVALID_GROUP_TITLE = "bogus";
+
+    @PersistenceContext
+    private EntityManager manager;
+
+    @Autowired
+    private GroupRepository repository;
+
+    @Test
+    public void getById_validId() {
+        Group group = repository.get(GROUP_ID.toString());
+        assertThat(group, notNullValue());
+        assertThat(group.getTitle(), is(equalTo(GROUP_TITLE)));
+        assertThat(group.getMemberIds().get(0), equalTo("1"));
+        assertThat(group.getMemberIds().get(1), equalTo("5"));
+    }
+
+    @Test
+    public void getById_invalidId() {
+        Group group = repository.get(INVALID_GROUP_ID);
+        assertThat(group, is(nullValue()));
+    }
+
+    @Test
+    public void getType() {
+        assertEquals(repository.getType(), JpaGroup.class);
+    }
+
+    @Test
+    public void createGroup() {
+        JpaGroup newGroup = new JpaGroup();
+        newGroup.setTitle("TEST GROUP");
+        newGroup.setDescription("TEST GROUP");
+        newGroup.setOwnerId("1");
+        newGroup.setMemberIds(Arrays.asList(new String[] {"1", "5"}));
+
+        Group savedGroup = repository.save(newGroup);
+
+        assertThat(savedGroup.getId(), notNullValue());
+        assertThat(savedGroup.getTitle(), equalTo("TEST GROUP"));
+        assertThat(savedGroup.getMemberIds().size(), equalTo(2));
+    }
+
+    @Test
+    public void deleteGroup() {
+        JpaGroup newGroup = new JpaGroup();
+        newGroup.setTitle("TEST GROUP");
+        newGroup.setDescription("TEST GROUP");
+        newGroup.setOwnerId("1");
+        newGroup.setMemberIds(Arrays.asList(new String[] {"1", "5"}));
+
+        Group savedGroup = repository.save(newGroup);
+
+        String id = savedGroup.getId();
+
+        assertThat(savedGroup.getId(), notNullValue());
+
+        Group foundGroup = repository.get(id);
+
+        assertThat(foundGroup, notNullValue());
+
+        repository.delete(savedGroup);
+
+        foundGroup = repository.get(id);
+
+        assertThat(foundGroup, nullValue());
+    }
+
+    @Test
+    public void findByTitle() {
+        Group group = repository.findByTitle(GROUP_TITLE);
+
+        assertThat(group, notNullValue());
+        assertThat(group.getTitle(), equalTo(GROUP_TITLE));
+        assertThat(group.getId(), equalTo(GROUP_ID));
+    }
+
+    @Test
+    public void findByTitle_Invalid() {
+        Group group = repository.findByTitle(INVALID_GROUP_TITLE);
+
+        assertThat(group, nullValue());
+    }
+}


Reply via email to