← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3304: Implemented sequence generator

 

------------------------------------------------------------
revno: 3304
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-04-06 20:25:26 +0200
message:
  Implemented sequence generator
added:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/DefaultSequenceGenerator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/Sequence.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/SequenceGenerator.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sequence/
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sequence/hibernate/
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sequence/hibernate/Sequence.hbm.xml
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/sequence/
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/sequence/SequenceGeneratorTest.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== added directory 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence'
=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/DefaultSequenceGenerator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/DefaultSequenceGenerator.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/DefaultSequenceGenerator.java	2011-04-06 18:25:26 +0000
@@ -0,0 +1,63 @@
+package org.hisp.dhis.sequence;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.Iterator;
+
+import org.hisp.dhis.common.GenericStore;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
+public class DefaultSequenceGenerator
+    implements SequenceGenerator
+{
+    private GenericStore<Sequence> sequenceStore;
+    
+    public void setSequenceStore( GenericStore<Sequence> sequenceStore )
+    {
+        this.sequenceStore = sequenceStore;
+    }
+    
+    @Transactional(propagation=Propagation.REQUIRES_NEW)
+    public int getNextValue()
+    {
+        Iterator<Sequence> sequences = sequenceStore.getAll().iterator();
+        
+        if ( sequences.hasNext() )
+        {
+            Sequence sequence = sequences.next();
+            sequenceStore.update( sequence.increment() );
+            return sequence.getCurrentValue();
+        }
+        else
+        {
+            sequenceStore.save( new Sequence( BASE_VALUE ) );
+            return BASE_VALUE;
+        }
+    }
+}

=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/Sequence.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/Sequence.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/Sequence.java	2011-04-06 18:25:26 +0000
@@ -0,0 +1,70 @@
+package org.hisp.dhis.sequence;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+public class Sequence
+{
+    private int id;
+    
+    private int currentValue;
+
+    public Sequence()
+    {
+    }
+    
+    public Sequence( int currentValue )
+    {
+        this.currentValue = currentValue;
+    }
+    
+    public Sequence increment()
+    {
+        ++currentValue;
+        return this;
+    }
+    
+    public int getId()
+    {
+        return id;
+    }
+
+    public void setId( int id )
+    {
+        this.id = id;
+    }
+
+    public int getCurrentValue()
+    {
+        return currentValue;
+    }
+
+    public void setCurrentValue( int currentValue )
+    {
+        this.currentValue = currentValue;
+    }
+}

=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/SequenceGenerator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/SequenceGenerator.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sequence/SequenceGenerator.java	2011-04-06 18:25:26 +0000
@@ -0,0 +1,41 @@
+package org.hisp.dhis.sequence;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+public interface SequenceGenerator
+{
+    final int BASE_VALUE = 1;
+    
+    /**
+     * Returns the next value in the sequence. The value is guaranteed to be 
+     * unique in a system scope. This method will create a new physical transaction
+     * which will be committed when the method returns to avoid a race condition
+     * with other write transactions.
+     */
+    int getNextValue();
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2011-04-01 15:44:51 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2011-04-06 18:25:26 +0000
@@ -220,6 +220,11 @@
 		<property name="clazz" value="org.hisp.dhis.message.UserMessage"/>
 	</bean>
 	
+	<bean id="org.hisp.dhis.sequence.SequenceStore" class="org.hisp.dhis.hibernate.HibernateGenericStore">
+		<property name="sessionFactory" ref="sessionFactory"/>
+		<property name="clazz" value="org.hisp.dhis.sequence.Sequence"/>
+	</bean>
+	
 	<!-- Service definitions -->
 
 	<bean id="org.hisp.dhis.dataelement.DataElementOperandService"
@@ -436,6 +441,10 @@
 		<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService"/>
 	</bean>
 	
+	<bean id="org.hisp.dhis.sequence.SequenceGenerator" class="org.hisp.dhis.sequence.DefaultSequenceGenerator">
+		<property name="sequenceStore" ref="org.hisp.dhis.sequence.SequenceStore"/>
+	</bean>
+	
 	<!-- Concept Name -->
 	
 	<bean id="org.hisp.dhis.concept.ConceptStore"

=== added directory 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sequence'
=== added directory 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sequence/hibernate'
=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sequence/hibernate/Sequence.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sequence/hibernate/Sequence.hbm.xml	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sequence/hibernate/Sequence.hbm.xml	2011-04-06 18:25:26 +0000
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd";>
+
+<hibernate-mapping>
+
+  <class name="org.hisp.dhis.sequence.Sequence" table="systemsequence">
+    
+    <id name="id" column="systemsequenceid">
+      <generator class="native"/>
+    </id>
+
+    <property name="currentValue" not-null="true"/>
+
+  </class>
+</hibernate-mapping>
\ No newline at end of file

=== added directory 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/sequence'
=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/sequence/SequenceGeneratorTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/sequence/SequenceGeneratorTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/sequence/SequenceGeneratorTest.java	2011-04-06 18:25:26 +0000
@@ -0,0 +1,54 @@
+package org.hisp.dhis.sequence;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static junit.framework.Assert.assertEquals;
+
+import org.hisp.dhis.DhisSpringTest;
+import org.junit.Test;
+
+public class SequenceGeneratorTest
+    extends DhisSpringTest
+{
+    private SequenceGenerator generator;
+
+    @Override
+    public void setUpTest()
+    {
+        generator = (SequenceGenerator) getBean( SequenceGenerator.class.getName() );
+    }
+    
+    @Test
+    public void testGetNextValue()
+    {
+        assertEquals( SequenceGenerator.BASE_VALUE, generator.getNextValue() );
+        assertEquals( SequenceGenerator.BASE_VALUE + 1, generator.getNextValue() );
+        assertEquals( SequenceGenerator.BASE_VALUE + 2, generator.getNextValue() );
+        assertEquals( SequenceGenerator.BASE_VALUE + 3, generator.getNextValue() );
+    }
+}