=== added directory 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue' === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValue.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValue.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValue.java 2016-02-02 15:41:15 +0000 @@ -0,0 +1,88 @@ +package org.hisp.dhis.userkeyjsonvalue; + +/* + * 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 com.fasterxml.jackson.annotation.JsonProperty; +import org.hisp.dhis.common.BaseIdentifiableObject; +import org.hisp.dhis.user.User; + +/** + * @author Stian Sandvold + */ +public class UserKeyJsonValue + extends BaseIdentifiableObject +{ + /** + * The user which owns this UserKeyJsonValue + */ + private User user; + + /** + * A key belongs to a namespace, and represent a value + */ + private String key; + + /** + * A value referenced by a key and user, json-formatted data stored as a string. + */ + private String value; + + @JsonProperty + public String getKey() + { + return key; + } + + public void setKey( String key ) + { + this.key = key; + } + + @JsonProperty + public User getUser() + { + return user; + } + + public void setUser( User user ) + { + this.user = user; + } + + @JsonProperty + public String getValue() + { + return value; + } + + public void setValue( String value ) + { + this.value = value; + } +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueService.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueService.java 2016-02-02 15:41:15 +0000 @@ -0,0 +1,80 @@ +package org.hisp.dhis.userkeyjsonvalue; + +/* + * 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.user.User; + +import java.util.List; + +/** + * @author Stian Sandvold + */ +public interface UserKeyJsonValueService +{ + + /** + * Retrieves a list of keys from a user + * @param user the namespace to retrieve keys from + * @return a list of strings representing the keys from the user + */ + List getKeysByUser( User user ); + + /** + * Deletes all keys associated with a given user + * @param user the user to delete + */ + void cleanUserData( User user ); + + /** + * Retrieves a KeyJsonValue based on a user and key + * @param user the user where the key is associated + * @param key the key referencing the value + * @return the UserKeyJsonValue matching the key and namespace + */ + UserKeyJsonValue getUserKeyJsonValue( User user, String key ); + + /** + * Adds a new UserKeyJsonValue + * @param userKeyJsonValue the UserKeyJsonValue to be stored + * @return the id of the UserKeyJsonValue stored + */ + int addUserKeyJsonValue( UserKeyJsonValue userKeyJsonValue ); + + /** + * Updates a UserKeyJsonValue + * @param userKeyJsonValue the updated UserKeyJsonValue + */ + void updateUserKeyJsonValue( UserKeyJsonValue userKeyJsonValue ); + + /** + * Deletes a UserKeyJsonValue + * @param userKeyJsonValue the UserKeyJsonValue to be deleted. + */ + void deleteUserKeyJsonValue( UserKeyJsonValue userKeyJsonValue ); +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueStore.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueStore.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueStore.java 2016-02-02 15:41:15 +0000 @@ -0,0 +1,63 @@ +package org.hisp.dhis.userkeyjsonvalue; + +/* + * 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.GenericIdentifiableObjectStore; +import org.hisp.dhis.user.User; + +import java.util.List; + +/** + * @author Stian Sandvold + */ +public interface UserKeyJsonValueStore + extends GenericIdentifiableObjectStore +{ + /** + * Retrieves a list of keys associated with a given user. + * @param user the user to retrieve keys from + * @return a list of strings representing the different keys stored on the user + */ + List getKeysByUser( User user ); + + /** + * Retrieves a KeyJsonValue based on the associated key and user + * @param user the user where the key is stored + * @param key the key referencing the value + * @return the KeyJsonValue retrieved + */ + UserKeyJsonValue getUserKeyJsonValue( User user, String key ); + + /** + * Retrieves all UserKeyJsonValues owned by user + * @param user that owns UserKeyJsonValues + * @return list of UserKeyJsonValues + */ + List getUserKeyJsonValueByUser ( User user ); +} === added directory 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue' === added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/DefaultUserKeyJsonValueService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/DefaultUserKeyJsonValueService.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/DefaultUserKeyJsonValueService.java 2016-02-02 15:41:15 +0000 @@ -0,0 +1,90 @@ +package org.hisp.dhis.userkeyjsonvalue; + +/* + * 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.user.User; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** + * @author Stian Sandvold + */ +@Transactional +public class DefaultUserKeyJsonValueService + implements UserKeyJsonValueService +{ + private UserKeyJsonValueStore userKeyJsonValueStore; + + public void setUserKeyJsonValueStore( UserKeyJsonValueStore userKeyJsonValueStore ) + { + this.userKeyJsonValueStore = userKeyJsonValueStore; + } + + public UserKeyJsonValueStore getUserKeyJsonValueStore() { + return this.userKeyJsonValueStore; + } + + @Override + public List getKeysByUser( User user ) + { + return userKeyJsonValueStore.getKeysByUser( user ); + } + + @Override + public void cleanUserData( User user ) + { + userKeyJsonValueStore.getUserKeyJsonValueByUser( user ).forEach( userKeyJsonValueStore :: delete ); + } + + @Override + public UserKeyJsonValue getUserKeyJsonValue( User user, String key ) + { + return userKeyJsonValueStore.getUserKeyJsonValue( user, key ); + } + + @Override + public int addUserKeyJsonValue( UserKeyJsonValue userKeyJsonValue ) + { + return userKeyJsonValueStore.save( userKeyJsonValue ); + } + + @Override + public void updateUserKeyJsonValue( UserKeyJsonValue userKeyJsonValue ) + { + userKeyJsonValueStore.update( userKeyJsonValue ); + } + + @Override + public void deleteUserKeyJsonValue( UserKeyJsonValue userKeyJsonValue ) + { + userKeyJsonValueStore.delete( userKeyJsonValue ); + } + +} === added directory 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/hibernate' === added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/hibernate/HibernateUserKeyJsonValueStore.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/hibernate/HibernateUserKeyJsonValueStore.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/hibernate/HibernateUserKeyJsonValueStore.java 2016-02-02 15:41:15 +0000 @@ -0,0 +1,72 @@ +package org.hisp.dhis.userkeyjsonvalue.hibernate; + +/* + * 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.hibernate.criterion.Restrictions; +import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore; +import org.hisp.dhis.user.User; +import org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue; +import org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValueStore; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author Stian Sandvold + */ +public class HibernateUserKeyJsonValueStore + extends HibernateIdentifiableObjectStore + implements UserKeyJsonValueStore +{ + @Override + public List getKeysByUser( User user ) + { + return (List) getCriteria( + Restrictions.eq( "user", user ) + ).list().stream().map( o -> ((UserKeyJsonValue) o).getKey() ).collect( Collectors.toList()); + } + + @Override + public UserKeyJsonValue getUserKeyJsonValue( User user, String key ) + { + return (UserKeyJsonValue) getCriteria( + Restrictions.eq( "user", user ), + Restrictions.eq( "key", key ) + ).uniqueResult(); + } + + @Override + public List getUserKeyJsonValueByUser( User user ) + { + return getCriteria( + Restrictions.eq( "user", user ) + ).list(); + } + +} === 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 2016-02-01 07:36:06 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml 2016-02-02 10:56:40 +0000 @@ -612,6 +612,11 @@ + + + + + @@ -1296,6 +1301,10 @@ + + + + === added directory 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/userkeyjsonvalue' === added directory 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/userkeyjsonvalue/hibernate' === added file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/userkeyjsonvalue/hibernate/UserKeyJsonValue.hbm.xml' --- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/userkeyjsonvalue/hibernate/UserKeyJsonValue.hbm.xml 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/userkeyjsonvalue/hibernate/UserKeyJsonValue.hbm.xml 2016-02-02 15:41:15 +0000 @@ -0,0 +1,28 @@ + +]> + + + + + + + + + + &identifiableProperties; + + + + + + + + + + === added directory 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/userkeyjsonvalue' === added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueStoreTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueStoreTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueStoreTest.java 2016-02-02 16:38:55 +0000 @@ -0,0 +1,109 @@ +package org.hisp.dhis.userkeyjsonvalue; + +/* + * 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.DhisSpringTest; +import org.hisp.dhis.user.User; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +import static org.junit.Assert.*; + +/** + * @author Stian Sandvold. + */ +public class UserKeyJsonValueStoreTest extends DhisSpringTest +{ + + @Autowired + private UserKeyJsonValueStore userKeyJsonValueStore; + + private User user = userService.getAllUsers().get( 0 ); + + @Test + public void testAddUserKeyJsonValue() + { + UserKeyJsonValue userKeyJsonValue = new UserKeyJsonValue(); + + userKeyJsonValue.setValue( "{}" ); + userKeyJsonValue.setKey( "test" ); + userKeyJsonValue.setUser( user ); + + int id = userKeyJsonValueStore.save( userKeyJsonValue ); + + assertNotNull( userKeyJsonValue ); + assertEquals( userKeyJsonValue, userKeyJsonValueStore.get( id ) ); + } + + @Test + public void testAddUserKeyJsonValuesAndGetKeysByUser() + { + + UserKeyJsonValue userKeyJsonValueA = new UserKeyJsonValue(); + + userKeyJsonValueA.setValue( "{}" ); + userKeyJsonValueA.setKey( "test_a" ); + userKeyJsonValueA.setUser( user ); + + UserKeyJsonValue userKeyJsonValueB = new UserKeyJsonValue(); + + userKeyJsonValueB.setValue( "{}" ); + userKeyJsonValueB.setKey( "test_b" ); + userKeyJsonValueB.setUser( user ); + + List list = userKeyJsonValueStore.getKeysByUser( user ); + + assertTrue( list.contains( "test_a" ) ); + assertTrue( list.contains( "test_b" ) ); + } + + @Test + public void testAddUserKeyJsonValuesAndGetUserKEyJsonValuesByUser() + { + + UserKeyJsonValue userKeyJsonValueA = new UserKeyJsonValue(); + + userKeyJsonValueA.setValue( "{}" ); + userKeyJsonValueA.setKey( "test_a" ); + userKeyJsonValueA.setUser( user ); + + UserKeyJsonValue userKeyJsonValueB = new UserKeyJsonValue(); + + userKeyJsonValueB.setValue( "{}" ); + userKeyJsonValueB.setKey( "test_b" ); + userKeyJsonValueB.setUser( user ); + + List list = userKeyJsonValueStore.getUserKeyJsonValueByUser( user ); + + assertTrue( list.contains( userKeyJsonValueA ) ); + assertTrue( list.contains( userKeyJsonValueB ) ); + } +} === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/UserKeyJsonValueController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/UserKeyJsonValueController.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/UserKeyJsonValueController.java 2016-02-02 15:41:15 +0000 @@ -0,0 +1,196 @@ +package org.hisp.dhis.webapi.controller; + +/* + * 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.dxf2.webmessage.WebMessageException; +import org.hisp.dhis.render.RenderService; +import org.hisp.dhis.user.CurrentUserService; +import org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue; +import org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValueService; +import org.hisp.dhis.webapi.service.WebMessageService; +import org.hisp.dhis.webapi.utils.WebMessageUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.List; + +/** + * @author Stian Sandvold + */ +@Controller +@RequestMapping( "/userDataStore" ) +public class UserKeyJsonValueController +{ + @Autowired + private UserKeyJsonValueService userKeyJsonValueService; + + @Autowired + private RenderService renderService; + + @Autowired + private WebMessageService messageService; + + @Autowired + private CurrentUserService currentUserService; + + /** + * Returns a JSON array of strings representing the different keys used. + * If no keys exist, an empty array is returned. + */ + @RequestMapping( value = "", method = RequestMethod.GET, produces = "application/json" ) + public + @ResponseBody + List getKeys( HttpServletResponse response ) + throws IOException + { + return userKeyJsonValueService.getKeysByUser( currentUserService.getCurrentUser() ); + } + + /** + * Deletes all keys with the given user. + */ + @RequestMapping( value = "/", method = RequestMethod.DELETE ) + public void deleteKeys( HttpServletResponse response ) + throws WebMessageException + { + userKeyJsonValueService.cleanUserData( currentUserService.getCurrentUser() ); + + messageService.sendJson( WebMessageUtils.ok( "All keys deleted." ), response ); + } + + /** + * Retrieves the value of the KeyJsonValue represented by the given key from + * the current user. + */ + @RequestMapping( value = "/{key}", method = RequestMethod.GET, produces = "application/json" ) + public + @ResponseBody + String getUserKeyJsonValue( + @PathVariable String key, HttpServletResponse response ) + throws IOException, WebMessageException + { + UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue( + currentUserService.getCurrentUser(), key ); + + if ( userKeyJsonValue == null ) + { + throw new WebMessageException( WebMessageUtils + .notFound( "The key '" + key + "' was not found." ) ); + } + + return userKeyJsonValue.getValue(); + } + + /** + * Creates a new KeyJsonValue Object on the current user with the key and value supplied. + */ + @RequestMapping( value = "/{key}", method = RequestMethod.POST, produces = "application/json", consumes = "application/json" ) + public void addUserKeyJsonValue( + @PathVariable String key, @RequestBody String body, HttpServletResponse response ) + throws IOException, WebMessageException + { + if ( userKeyJsonValueService.getUserKeyJsonValue( currentUserService.getCurrentUser(), key ) != null ) + { + throw new WebMessageException( WebMessageUtils + .conflict( "The key '" + key + "' already exists." ) ); + } + + if ( !renderService.isValidJson( body ) ) + { + throw new WebMessageException( WebMessageUtils.badRequest( "The data is not valid JSON." ) ); + } + + UserKeyJsonValue userKeyJsonValue = new UserKeyJsonValue(); + + userKeyJsonValue.setKey( key ); + userKeyJsonValue.setUser( currentUserService.getCurrentUser() ); + userKeyJsonValue.setValue( body ); + + userKeyJsonValueService.addUserKeyJsonValue( userKeyJsonValue ); + + response.setStatus( HttpServletResponse.SC_CREATED ); + messageService.sendJson( WebMessageUtils.created( "Key '" + key + "' created." ), response ); + } + + /** + * Update a key. + */ + @RequestMapping( value = "/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json" ) + public void updateUserKeyJsonValue( @PathVariable String key, @RequestBody String body, + HttpServletRequest request, HttpServletResponse response ) + throws WebMessageException, IOException + { + UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue( + currentUserService.getCurrentUser(), key ); + + if ( userKeyJsonValue == null ) + { + throw new WebMessageException( WebMessageUtils + .notFound( "The key '" + key + "' was not found." ) ); + } + + if ( !renderService.isValidJson( body ) ) + { + throw new WebMessageException( WebMessageUtils.badRequest( "The data is not valid JSON." ) ); + } + + userKeyJsonValue.setValue( body ); + + userKeyJsonValueService.updateUserKeyJsonValue( userKeyJsonValue ); + + response.setStatus( HttpServletResponse.SC_OK ); + messageService.sendJson( WebMessageUtils.ok( "Key '" + key + "' updated." ), response ); + } + + /** + * Delete a key. + */ + @RequestMapping( value = "/{key}", method = RequestMethod.DELETE, produces = "application/json" ) + public void deleteUserKeyJsonValue( + @PathVariable String key, HttpServletResponse response ) + throws WebMessageException + { + UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue( + currentUserService.getCurrentUser(), key ); + + if ( userKeyJsonValue == null ) + { + throw new WebMessageException( WebMessageUtils + .notFound( "The key '" + key + "' was not found." ) ); + } + + userKeyJsonValueService.deleteUserKeyJsonValue( userKeyJsonValue ); + + messageService.sendJson( WebMessageUtils.ok( "Key '" + key + "' deleted." ), response ); + } +}