=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java 2016-01-21 05:06:57 +0000 @@ -0,0 +1,96 @@ +package org.hisp.dhis.preheat; + +/* + * Copyright (c) 2004-2016, 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 org.hisp.dhis.common.IdentifiableObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Morten Olav Hansen + */ +public class Preheat +{ + private Map, Map>> map = new HashMap<>(); + + public Preheat() + { + } + + public boolean isEmpty() + { + return map.isEmpty(); + } + + public boolean isEmpty( PreheatIdentifier identifier ) + { + return !map.containsKey( identifier ) || map.get( identifier ).isEmpty(); + } + + public boolean isEmpty( PreheatIdentifier identifier, Class klass ) + { + return isEmpty( identifier ) || !map.get( identifier ).containsKey( klass ) || map.get( identifier ).get( klass ).isEmpty(); + } + + public Preheat put( PreheatIdentifier identifier, T object ) + { + if ( object == null ) return this; + if ( !map.containsKey( identifier ) ) map.put( identifier, new HashMap<>() ); + if ( !map.get( identifier ).containsKey( object.getClass() ) ) map.get( identifier ).put( object.getClass(), new HashMap<>() ); + + Map identifierMap = map.get( identifier ).get( object.getClass() ); + String key = identifier.getIdentifier( object ); + + if ( identifierMap.containsKey( key ) ) + { + throw new PreheatException( "Duplicate key " + key + " for class " + object.getClass().getName() + "." ); + } + + identifierMap.put( key, object ); + + return this; + } + + @SuppressWarnings( "unchecked" ) + public T get( PreheatIdentifier identifier, Class klass, String key ) + { + if ( !containsKey( identifier, klass, key ) ) + { + return null; + } + + return (T) map.get( identifier ).get( klass ).get( key ); + } + + public boolean containsKey( PreheatIdentifier identifier, Class klass, String key ) + { + return !(isEmpty() || isEmpty( identifier ) || isEmpty( identifier, klass )) && map.get( identifier ).get( klass ).containsKey( key ); + } +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatException.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatException.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatException.java 2016-01-21 05:06:57 +0000 @@ -0,0 +1,40 @@ +package org.hisp.dhis.preheat; + +/* + * Copyright (c) 2004-2016, 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. + */ + +/** + * @author Morten Olav Hansen + */ +public class PreheatException extends RuntimeException +{ + public PreheatException( String message ) + { + super( message ); + } +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java 2016-01-21 05:06:57 +0000 @@ -0,0 +1,60 @@ +package org.hisp.dhis.preheat; + +/* + * Copyright (c) 2004-2016, 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 org.hisp.dhis.common.IdentifiableObject; + +/** + * @author Morten Olav Hansen + */ +public enum PreheatIdentifier +{ + /** + * Preheat using UID identifiers. + */ + UID, + + /** + * Preheat using CODE identifiers. + */ + CODE; + + String getIdentifier( T object ) + { + switch ( this ) + { + case UID: + return object.getUid(); + case CODE: + return object.getCode(); + } + + throw new RuntimeException( "Unhandled identifier type." ); + } +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatMode.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatMode.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatMode.java 2016-01-21 05:06:57 +0000 @@ -0,0 +1,45 @@ +package org.hisp.dhis.preheat; + +/* + * Copyright (c) 2004-2016, 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. + */ + +/** + * @author Morten Olav Hansen + */ +public enum PreheatMode +{ + /** + * Scan objects for references. + */ + REFERENCE, + + /** + * Load inn all object of given types. + */ + ALL; +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatParams.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatParams.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatParams.java 2016-01-21 05:06:57 +0000 @@ -0,0 +1,83 @@ +package org.hisp.dhis.preheat; + +/* + * Copyright (c) 2004-2016, 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.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Morten Olav Hansen + */ +public class PreheatParams +{ + private PreheatMode preheatMode = PreheatMode.ALL; + + private Collection> classes = new ArrayList<>(); + + private Map, Collection> references = new HashMap<>(); + + public PreheatParams() + { + } + + public PreheatMode getPreheatMode() + { + return preheatMode; + } + + public PreheatParams setPreheatMode( PreheatMode preheatMode ) + { + this.preheatMode = preheatMode; + return this; + } + + public Collection> getClasses() + { + return classes; + } + + public PreheatParams setClasses( Collection> classes ) + { + this.classes = classes; + return this; + } + + public Map, Collection> getReferences() + { + return references; + } + + public PreheatParams setReferences( Map, Collection> references ) + { + this.references = references; + return this; + } +} === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java 2016-01-21 05:06:57 +0000 @@ -28,10 +28,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import java.util.Collection; -import java.util.Map; -import java.util.Set; - /** * @author Morten Olav Hansen */ @@ -40,14 +36,14 @@ /** * Preheat a set of pre-defined classes. If size == 0, then preheat all metadata classes automatically. * - * @param classes Classes to preheat + * @param params Params for preheating */ - void preheat( Set> classes ); + Preheat preheat( PreheatParams params ); /** - * Preheat a specified set of UIDs for a set of classes. + * Validate PreheatParams. * - * @param classes Class => UID Collection map + * @param params PreheatParams */ - void preheat( Map, Collection> classes ); + void validate( PreheatParams params ) throws PreheatException; } === added directory 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/preheat' === added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/preheat/PreheatTest.java' --- dhis-2/dhis-api/src/test/java/org/hisp/dhis/preheat/PreheatTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/preheat/PreheatTest.java 2016-01-21 05:06:57 +0000 @@ -0,0 +1,113 @@ +package org.hisp.dhis.preheat; + +/* + * Copyright (c) 2004-2016, 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 org.hisp.dhis.dataelement.DataElement; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Morten Olav Hansen + */ +public class PreheatTest +{ + @Test + public void testAllEmpty() + { + Preheat preheat = new Preheat(); + + assertTrue( preheat.isEmpty() ); + assertTrue( preheat.isEmpty( PreheatIdentifier.UID ) ); + assertTrue( preheat.isEmpty( PreheatIdentifier.CODE ) ); + } + + @Test + public void testPutUid() + { + Preheat preheat = new Preheat(); + + DataElement de1 = new DataElement( "dataElementA" ); + DataElement de2 = new DataElement( "dataElementB" ); + DataElement de3 = new DataElement( "dataElementC" ); + + de1.setAutoFields(); + de2.setAutoFields(); + de3.setAutoFields(); + + preheat.put( PreheatIdentifier.UID, de1 ); + preheat.put( PreheatIdentifier.UID, de2 ); + preheat.put( PreheatIdentifier.UID, de3 ); + + assertFalse( preheat.isEmpty() ); + assertFalse( preheat.isEmpty( PreheatIdentifier.UID ) ); + assertTrue( preheat.isEmpty( PreheatIdentifier.CODE ) ); + + assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de1.getUid() ) ); + assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de2.getUid() ) ); + assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de3.getUid() ) ); + + assertEquals( de1.getUid(), preheat.get( PreheatIdentifier.UID, DataElement.class, de1.getUid() ).getUid() ); + assertEquals( de2.getUid(), preheat.get( PreheatIdentifier.UID, DataElement.class, de2.getUid() ).getUid() ); + assertEquals( de3.getUid(), preheat.get( PreheatIdentifier.UID, DataElement.class, de3.getUid() ).getUid() ); + } + + @Test + public void testPutCode() + { + Preheat preheat = new Preheat(); + + DataElement de1 = new DataElement( "dataElementA" ); + DataElement de2 = new DataElement( "dataElementB" ); + DataElement de3 = new DataElement( "dataElementC" ); + + de1.setAutoFields(); + de1.setCode( "Code1" ); + de2.setAutoFields(); + de2.setCode( "Code2" ); + de3.setAutoFields(); + de3.setCode( "Code3" ); + + preheat.put( PreheatIdentifier.CODE, de1 ); + preheat.put( PreheatIdentifier.CODE, de2 ); + preheat.put( PreheatIdentifier.CODE, de3 ); + + assertFalse( preheat.isEmpty() ); + assertFalse( preheat.isEmpty( PreheatIdentifier.CODE ) ); + assertTrue( preheat.isEmpty( PreheatIdentifier.UID ) ); + + assertTrue( preheat.containsKey( PreheatIdentifier.CODE, DataElement.class, de1.getCode() ) ); + assertTrue( preheat.containsKey( PreheatIdentifier.CODE, DataElement.class, de2.getCode() ) ); + assertTrue( preheat.containsKey( PreheatIdentifier.CODE, DataElement.class, de3.getCode() ) ); + + assertEquals( de1.getCode(), preheat.get( PreheatIdentifier.CODE, DataElement.class, de1.getCode() ).getCode() ); + assertEquals( de2.getCode(), preheat.get( PreheatIdentifier.CODE, DataElement.class, de2.getCode() ).getCode() ); + assertEquals( de3.getCode(), preheat.get( PreheatIdentifier.CODE, DataElement.class, de3.getCode() ).getCode() ); + } +}