=== modified 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 2016-02-02 22:45:48 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValue.java 2016-02-10 12:43:15 +0000 @@ -44,15 +44,30 @@ private User user; /** - * A key belongs to a namespace, and represent a value + * A key belongs to a namespace and user, and represent a value */ private String key; /** - * A value referenced by a key and user, JSON-formatted data stored as a string. + * A value referenced by a key, namespace and user, JSON-formatted data stored as a string */ private String value; + /** + * A namespace is a collection of keys for a given user + */ + private String namespace; + + public String getNamespace() + { + return namespace; + } + + public void setNamespace( String namespace ) + { + this.namespace = namespace; + } + @JsonProperty public String getKey() { === modified 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 2016-02-02 22:45:48 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueService.java 2016-02-10 12:43:15 +0000 @@ -37,26 +37,14 @@ */ 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 + * @param namespace the namespace associated with the key + * @param key the key referencing the value @return the UserKeyJsonValue matching the key and namespace */ - UserKeyJsonValue getUserKeyJsonValue( User user, String key ); + UserKeyJsonValue getUserKeyJsonValue( User user, String namespace, String key ); /** * Adds a new UserKeyJsonValue @@ -76,4 +64,27 @@ * @param userKeyJsonValue the UserKeyJsonValue to be deleted. */ void deleteUserKeyJsonValue( UserKeyJsonValue userKeyJsonValue ); + + /** + * Returns a list of namespaces connected to the given user + * @param user the user connected to the namespaces + * @return List of strings representing namespaces or an empty list if no namespaces are found + */ + List getNamespacesByUser( User user ); + + /** + * Returns a list of keys in the given namespace connected to the given user + * @param user connected to keys + * @param namespace to fetch keys from + * @return a list of keys or an empty list if no keys are found + */ + List getKeysByUserAndNamespace( User user, String namespace ); + + + /** + * Deletes all keys associated with a given user and namespace + * @param user the user associated with namespace to delete + * @param namespace the namespace to delete + */ + void deleteNamespaceFromUser( User user, String namespace ); } === modified 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 2016-02-02 15:41:15 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueStore.java 2016-02-10 12:43:15 +0000 @@ -40,24 +40,33 @@ 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 namespace the namespace referencing the value * @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 ); + UserKeyJsonValue getUserKeyJsonValue( User user, String namespace, String key ); + + /** + * Retrieves a list of namespaces associated with a user + * @param user to search namespaces for + * @return a list of strings representing namespaces + */ + List getNamespacesByUser( User user ); + + /** + * Retrieves a list of keys associated with a given user and namespace. + * @param user the user to retrieve keys from + * @param namespace the namespace to search + * @return a list of strings representing the different keys stored on the user + */ + List getKeysByUserAndNamespace( User user, String namespace ); + + /** + * Retrieves all UserKeyJsonvalues from a given user and namespace + * @param user to search + * @param namespace to search + */ + List getUserKeyJsonValueByUserAndNamespace( User user, String namespace ); } === modified 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 2016-02-02 22:45:48 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/DefaultUserKeyJsonValueService.java 2016-02-10 12:43:15 +0000 @@ -53,21 +53,9 @@ } @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 ); + public UserKeyJsonValue getUserKeyJsonValue( User user, String namespace, String key ) + { + return userKeyJsonValueStore.getUserKeyJsonValue( user, namespace, key ); } @Override @@ -87,4 +75,23 @@ { userKeyJsonValueStore.delete( userKeyJsonValue ); } + + @Override + public List getNamespacesByUser( User user ) + { + return userKeyJsonValueStore.getNamespacesByUser( user ); + } + + @Override + public List getKeysByUserAndNamespace( User user, String namespace ) + { + return userKeyJsonValueStore.getKeysByUserAndNamespace( user, namespace ); + } + + @Override + public void deleteNamespaceFromUser( User user, String namespace ) + { + userKeyJsonValueStore.getUserKeyJsonValueByUserAndNamespace( user, namespace ).forEach( + userKeyJsonValueStore::delete ); + } } === modified 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 2016-02-02 22:45:48 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/userkeyjsonvalue/hibernate/HibernateUserKeyJsonValueStore.java 2016-02-10 12:43:15 +0000 @@ -44,28 +44,39 @@ extends HibernateIdentifiableObjectStore implements UserKeyJsonValueStore { - @Override - @SuppressWarnings("unchecked") - 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 ) + public UserKeyJsonValue getUserKeyJsonValue( User user, String namespace, String key ) { return (UserKeyJsonValue) getCriteria( Restrictions.eq( "user", user ), + Restrictions.eq( "namespace", namespace ), Restrictions.eq( "key", key ) ).uniqueResult(); } @Override - @SuppressWarnings("unchecked") - public List getUserKeyJsonValueByUser( User user ) - { - return getCriteria( - Restrictions.eq( "user", user ) ).list(); + public List getNamespacesByUser( User user ) + { + List queryResult = getCriteria( Restrictions.eq( "user", user ) ).list(); + List namespaces = queryResult.stream().map( UserKeyJsonValue::getNamespace ).distinct() + .collect( Collectors.toList() ); + + return namespaces; + } + + @Override + public List getKeysByUserAndNamespace( User user, String namespace ) + { + return (getUserKeyJsonValueByUserAndNamespace( user, namespace )).stream().map( UserKeyJsonValue::getKey ) + .collect( Collectors.toList() ); + } + + @Override + public List getUserKeyJsonValueByUserAndNamespace( User user, String namespace ) + { + return (List) getCriteria( + Restrictions.eq( "user", user ), + Restrictions.eq( "namespace", namespace ) + ).list(); } } === modified 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 2016-02-02 15:41:15 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/userkeyjsonvalue/hibernate/UserKeyJsonValue.hbm.xml 2016-02-10 12:43:15 +0000 @@ -16,12 +16,14 @@ + unique-key="userkeyjsonvalue_unique_key_on_user_and_namespace"/> + unique-key="userkeyjsonvalue_unique_key_on_user_and_namespace"/> + + === modified 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 2016-02-03 10:14:04 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/userkeyjsonvalue/UserKeyJsonValueStoreTest.java 2016-02-10 12:43:15 +0000 @@ -65,6 +65,7 @@ userKeyJsonValue.setValue( "{}" ); userKeyJsonValue.setKey( "test" ); + userKeyJsonValue.setNamespace( "a" ); userKeyJsonValue.setUser( user ); int id = userKeyJsonValueStore.save( userKeyJsonValue ); @@ -74,12 +75,13 @@ } @Test - public void testAddUserKeyJsonValuesAndGetKeysByUser() + public void testAddUserKeyJsonValuesAndGetNamespacesByUser() { UserKeyJsonValue userKeyJsonValueA = new UserKeyJsonValue(); userKeyJsonValueA.setValue( "{}" ); - userKeyJsonValueA.setKey( "test_a" ); + userKeyJsonValueA.setNamespace( "test_a" ); + userKeyJsonValueA.setKey( "a" ); userKeyJsonValueA.setUser( user ); userKeyJsonValueStore.save(userKeyJsonValueA); @@ -87,12 +89,13 @@ UserKeyJsonValue userKeyJsonValueB = new UserKeyJsonValue(); userKeyJsonValueB.setValue( "{}" ); - userKeyJsonValueB.setKey( "test_b" ); + userKeyJsonValueB.setNamespace( "test_b" ); + userKeyJsonValueB.setKey( "b" ); userKeyJsonValueB.setUser( user ); userKeyJsonValueStore.save(userKeyJsonValueB); - List list = userKeyJsonValueStore.getKeysByUser( user ); + List list = userKeyJsonValueStore.getNamespacesByUser( user ); assertTrue( list.contains( "test_a" ) ); assertTrue( list.contains( "test_b" ) ); @@ -104,6 +107,7 @@ UserKeyJsonValue userKeyJsonValueA = new UserKeyJsonValue(); userKeyJsonValueA.setValue( "{}" ); + userKeyJsonValueA.setNamespace( "a" ); userKeyJsonValueA.setKey( "test_a" ); userKeyJsonValueA.setUser( user ); @@ -112,12 +116,13 @@ UserKeyJsonValue userKeyJsonValueB = new UserKeyJsonValue(); userKeyJsonValueB.setValue( "{}" ); + userKeyJsonValueB.setNamespace( "a" ); userKeyJsonValueB.setKey( "test_b" ); userKeyJsonValueB.setUser( user ); userKeyJsonValueStore.save(userKeyJsonValueB); - List list = userKeyJsonValueStore.getUserKeyJsonValueByUser( user ); + List list = userKeyJsonValueStore.getUserKeyJsonValueByUserAndNamespace( user, "a" ); assertTrue( list.contains( userKeyJsonValueA ) ); assertTrue( list.contains( userKeyJsonValueB ) ); === modified 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 2016-02-02 15:41:15 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/UserKeyJsonValueController.java 2016-02-10 12:43:15 +0000 @@ -64,65 +64,81 @@ private CurrentUserService currentUserService; /** - * Returns a JSON array of strings representing the different keys used. - * If no keys exist, an empty array is returned. + * Returns a JSON array of strings representing the different namespaces used. + * If no namespaces 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 ) + List getNamespaces( HttpServletResponse response ) + throws IOException + { + return userKeyJsonValueService.getNamespacesByUser( currentUserService.getCurrentUser() ); + } + + /** + * Returns a JSON array of strings representing the different keys used in a given namespace. + * If no namespaces exist, an empty array is returned. + */ + @RequestMapping( value = "/{namespace}", method = RequestMethod.GET, produces = "application/json" ) + public + @ResponseBody + List getKeys( @PathVariable String namespace, HttpServletResponse response ) + throws IOException + { + return userKeyJsonValueService.getKeysByUserAndNamespace( currentUserService.getCurrentUser(), namespace ); + } + + /** + * Deletes all keys with the given user and namespace. + */ + @RequestMapping( value = "/{namespace}", method = RequestMethod.DELETE ) + public void deleteKeys( @PathVariable String namespace, HttpServletResponse response ) throws WebMessageException { - userKeyJsonValueService.cleanUserData( currentUserService.getCurrentUser() ); + userKeyJsonValueService.deleteNamespaceFromUser( currentUserService.getCurrentUser(), namespace ); - messageService.sendJson( WebMessageUtils.ok( "All keys deleted." ), response ); + messageService + .sendJson( WebMessageUtils.ok( "All keys from namespace '" + namespace + "' deleted." ), response ); } /** - * Retrieves the value of the KeyJsonValue represented by the given key from + * Retrieves the value of the KeyJsonValue represented by the given key and namespace from * the current user. */ - @RequestMapping( value = "/{key}", method = RequestMethod.GET, produces = "application/json" ) + @RequestMapping( value = "/{namespace}/{key}", method = RequestMethod.GET, produces = "application/json" ) public @ResponseBody String getUserKeyJsonValue( - @PathVariable String key, HttpServletResponse response ) + @PathVariable String namespace, @PathVariable String key, HttpServletResponse response ) throws IOException, WebMessageException { UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue( - currentUserService.getCurrentUser(), key ); + currentUserService.getCurrentUser(), namespace, key ); if ( userKeyJsonValue == null ) { throw new WebMessageException( WebMessageUtils - .notFound( "The key '" + key + "' was not found." ) ); + .notFound( "The key '" + key + "' was not found in the namespace '" + namespace + "'." ) ); } return userKeyJsonValue.getValue(); } /** - * Creates a new KeyJsonValue Object on the current user with the key and value supplied. + * Creates a new KeyJsonValue Object on the current user with the key, namespace and value supplied. */ - @RequestMapping( value = "/{key}", method = RequestMethod.POST, produces = "application/json", consumes = "application/json" ) + @RequestMapping( value = "/{namespace}/{key}", method = RequestMethod.POST, produces = "application/json", consumes = "application/json" ) public void addUserKeyJsonValue( - @PathVariable String key, @RequestBody String body, HttpServletResponse response ) + @PathVariable String namespace, @PathVariable String key, @RequestBody String body, + HttpServletResponse response ) throws IOException, WebMessageException { - if ( userKeyJsonValueService.getUserKeyJsonValue( currentUserService.getCurrentUser(), key ) != null ) + if ( userKeyJsonValueService.getUserKeyJsonValue( currentUserService.getCurrentUser(), namespace, key ) != + null ) { throw new WebMessageException( WebMessageUtils - .conflict( "The key '" + key + "' already exists." ) ); + .conflict( "The key '" + key + "' already exists in the namespace '" + namespace + "'." ) ); } if ( !renderService.isValidJson( body ) ) @@ -134,29 +150,33 @@ userKeyJsonValue.setKey( key ); userKeyJsonValue.setUser( currentUserService.getCurrentUser() ); + userKeyJsonValue.setNamespace( namespace ); userKeyJsonValue.setValue( body ); userKeyJsonValueService.addUserKeyJsonValue( userKeyJsonValue ); response.setStatus( HttpServletResponse.SC_CREATED ); - messageService.sendJson( WebMessageUtils.created( "Key '" + key + "' created." ), response ); + messageService + .sendJson( WebMessageUtils.created( "Key '" + key + "' in namespace '" + namespace + "' 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, + @RequestMapping( value = "/{namespace}/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json" ) + public void updateUserKeyJsonValue( @PathVariable String namespace, @PathVariable String key, + @RequestBody String body, HttpServletRequest request, HttpServletResponse response ) throws WebMessageException, IOException { UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue( - currentUserService.getCurrentUser(), key ); + currentUserService.getCurrentUser(), namespace, key ); if ( userKeyJsonValue == null ) { throw new WebMessageException( WebMessageUtils - .notFound( "The key '" + key + "' was not found." ) ); + .notFound( "The key '" + key + "' was not found in the namespace '" + namespace + "'." ) ); } if ( !renderService.isValidJson( body ) ) @@ -169,28 +189,31 @@ userKeyJsonValueService.updateUserKeyJsonValue( userKeyJsonValue ); response.setStatus( HttpServletResponse.SC_OK ); - messageService.sendJson( WebMessageUtils.ok( "Key '" + key + "' updated." ), response ); + messageService + .sendJson( WebMessageUtils.created( "Key '" + key + "' in namespace '" + namespace + "' updated." ), + response ); } /** * Delete a key. */ - @RequestMapping( value = "/{key}", method = RequestMethod.DELETE, produces = "application/json" ) + @RequestMapping( value = "/{namespace}/{key}", method = RequestMethod.DELETE, produces = "application/json" ) public void deleteUserKeyJsonValue( + @PathVariable String namespace, @PathVariable String key, HttpServletResponse response ) throws WebMessageException { UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue( - currentUserService.getCurrentUser(), key ); + currentUserService.getCurrentUser(), namespace, key ); if ( userKeyJsonValue == null ) { throw new WebMessageException( WebMessageUtils - .notFound( "The key '" + key + "' was not found." ) ); + .notFound( "The key '" + key + "' was not found in the namespace '" + namespace + "'." ) ); } userKeyJsonValueService.deleteUserKeyJsonValue( userKeyJsonValue ); - messageService.sendJson( WebMessageUtils.ok( "Key '" + key + "' deleted." ), response ); + messageService.sendJson( WebMessageUtils.ok( "Key '" + key + "' deleted from the namespace '" + namespace + "'." ), response ); } }