Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.jens.myapplication.apimanager.manager; // w w w . j a v a 2 s .com import com.example.jens.myapplication.apimanager.ApiConnection; import com.example.jens.myapplication.apimanager.CancellableTask; import com.example.jens.myapplication.apimanager.JSONParsingTask; import com.example.jens.myapplication.apimanager.PostRequestTask; import com.example.jens.myapplication.apimanager.RequestParams; import com.example.jens.myapplication.apimanager.SimpleRequestTask; import com.example.jens.myapplication.apimanager.filter.PersonFilter; import com.example.jens.myapplication.domain.Person; import com.example.jens.myapplication.domain.User; import com.example.jens.myapplication.sam.JoetzApplication; import com.example.jens.myapplication.util.AESEncryption; import com.example.jens.myapplication.util.JsonUtils; import com.fasterxml.jackson.annotation.JsonFilter; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ser.FilterProvider; import java.io.IOException; /** * Manager to manage the profile requests */ public class ProfileManager { private static final String ACTION_CHANGE_STANDARD = "api/users"; private static final String ACTION_CHANGE_ADDRESS = "api/users/address"; private static final String ACTION_CHANGE_PASSWORD = "api/users/password"; private static final String ACTION_CHANGE_SOCIAL_SECURITY = "api/users/socialsecurity"; private static final String ACTION_ADD_PERSON = "api/users/people"; private static final String ACTION_EDIT_PERSON = "api/users/people/%d"; private static final String ACTION_REMOVE_PERSON = "api/users/people/%d"; private static final String FILTER_STANDARD = "standardFilter"; private static final String FILTER_ADDRESS = "addressFilter"; private static final String FILTER_BOND = "bondFilter"; private static final String FILTER_ADDPERSON = "addPersonFilter"; private static final String FILTER_EDITPERSON = "editPersonFilter"; /** * Chance the standard information of a user * @param person Person object containing the new data * @param afterTask Task to be executed when the request is finished * @return the task */ public static CancellableTask changeStandardInformation(Person person, final SimpleRequestTask afterTask){ String rawBody; try{ class Model{ @JsonUnwrapped @JsonProperty @JsonFilter(FILTER_STANDARD) Person person; @JsonProperty("SubscribeToNewsletter") boolean subscribe = false; } Model model = new Model(); model.person = person; //StandardInformationModel model = new StandardInformationModel(person); FilterProvider filter = JsonUtils.createSimpleFilter( FILTER_STANDARD, PersonFilter.STANDARD_INFORMATION.fields()); rawBody = new ObjectMapper().writer(filter).writeValueAsString(model); } catch (IOException e) { e.printStackTrace(); return null; } RequestParams requestParams = new RequestParams() .setRawBody(rawBody) .setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey()); JSONParsingTask<User> jsonTask = new JSONParsingTask<User>() { @Override public User handleJSON(String json) throws IOException { return new ObjectMapper().readValue(json, User.class); } }; PostRequestTask<User> postTask = new PostRequestTask<User>() { @Override public void doTask(User u, int statusCode) { if(u != null){ JoetzApplication.getContext().getLoginManager().setUser(u); JoetzApplication.getContext().getLoginManager().emailChanged(u.getPerson().getEmail()); } afterTask.doTask(statusCode); } }; return ApiConnection.put(ACTION_CHANGE_STANDARD, jsonTask, postTask, requestParams); } /** * Chance the address information of a user * @param person Person object containing the new data * @param afterTask Task to be executed when the request is finished * @return the task */ public static CancellableTask changeAddress(Person person, final SimpleRequestTask afterTask){ String rawBody = null; try { class Model{ @JsonProperty @JsonUnwrapped @JsonFilter(FILTER_ADDRESS) Person person; } Model model = new Model(); model.person = person; FilterProvider filter = JsonUtils.createSimpleFilter( FILTER_ADDRESS, PersonFilter.ADDRESS_INFORMATION.fields()); rawBody = new ObjectMapper().writer(filter).writeValueAsString(model); } catch (IOException e) { e.printStackTrace(); } RequestParams requestParams = new RequestParams(); requestParams.setRawBody(rawBody); requestParams.setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey()); /* PostRequestTask<User> postTask = new PostRequestTask<User>() { @Override public void doTask(User u, int statusCode) { if(u != null){ JoetzApplication.getContext().getLoginManager().setUser(u); } afterTask.doTask(statusCode); } };*/ return ApiConnection.put(ACTION_CHANGE_ADDRESS, JsonUtils.createSimpleUserTask(), JsonUtils.createSimpleRefreshUserTask(afterTask), requestParams); } /** * Change the password of the user * @param oldPass The old password of the user * @param newPass The new password of the user * @param afterTask The task to be executed when the request is finished * @return the task */ public static CancellableTask changePassword(String oldPass, final String newPass, final SimpleRequestTask afterTask){ class PassModel{ @JsonProperty("OldPassword") String oldPass; @JsonProperty("NewPassword") String newPass; } String rawBody; try { PassModel model = new PassModel(); model.oldPass = AESEncryption.encrypt(oldPass); model.newPass = AESEncryption.encrypt(newPass); rawBody = new ObjectMapper().writeValueAsString(model); } catch (IOException e) { e.printStackTrace(); return null; } RequestParams requestParams = new RequestParams() .setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey()) .setRawBody(rawBody); PostRequestTask postTask = new PostRequestTask() { @Override public void doTask(Object obj, int statusCode) { if(ApiConnection.isSuccessCode(statusCode)){ JoetzApplication.getContext().getLoginManager().passwordChanged(newPass); } afterTask.doTask(statusCode); } }; return ApiConnection.put(ACTION_CHANGE_PASSWORD, JsonUtils.createSimpleUserTask(), postTask, requestParams); } /** * Chance the bond of a user * @param person Person object containing the new data * @param afterTask Task to be executed when the request is finished * @return the task */ public static CancellableTask changeSocialSecurity(Person person, final SimpleRequestTask afterTask){ String rawBody; try { class Model{ @JsonUnwrapped @JsonProperty @JsonFilter(FILTER_BOND) Person person; } Model model = new Model(); model.person = person; FilterProvider filter = JsonUtils.createSimpleFilter( FILTER_BOND, PersonFilter.SOCIAL_SECURITY.fields()); rawBody = new ObjectMapper().writer(filter).writeValueAsString(model); } catch (IOException e) { e.printStackTrace(); return null; } RequestParams requestParams = new RequestParams() .setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey()) .setRawBody(rawBody); /*PostRequestTask postTask = new PostRequestTask() { @Override public void doTask(Object obj, int statusCode) { afterTask.doTask(statusCode); } };*/ return ApiConnection.put(ACTION_CHANGE_SOCIAL_SECURITY, JsonUtils.createSimpleUserTask(), JsonUtils.createSimpleRefreshUserTask(afterTask), requestParams); } /** * Add a person to the user * @param person The person to be added * @param afterTask The task to be executed when the request is finished * @return the task */ public static CancellableTask addPerson(Person person, final SimpleRequestTask afterTask){ String rawBody; try{ class Model{ @JsonUnwrapped @JsonProperty @JsonFilter(FILTER_ADDPERSON) Person person; } Model model = new Model(); model.person = person; FilterProvider filter = JsonUtils.createSimpleFilter( FILTER_ADDPERSON, PersonFilter.ANONYMOUS_PARTICIPANT.fields()); rawBody = new ObjectMapper().writer(filter).writeValueAsString(model); }catch(IOException e){ e.printStackTrace(); return null; } RequestParams params = new RequestParams() .setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey()) .setRawBody(rawBody); return ApiConnection.post(ACTION_ADD_PERSON, JsonUtils.createSimpleUserTask(), JsonUtils.createSimpleRefreshUserTask(afterTask), params); } /** * Edit a person * @param person The new person information * @param afterTask The task to be executed when the request is finished * @return the task */ public static CancellableTask editPerson(Person person, final SimpleRequestTask afterTask){ String action = String.format(ACTION_EDIT_PERSON, person.getId()); String rawBody; try{ class Model{ @JsonUnwrapped @JsonProperty @JsonFilter(FILTER_EDITPERSON) Person person; } Model model = new Model(); model.person = person; FilterProvider filter = JsonUtils.createSimpleFilter( FILTER_EDITPERSON, PersonFilter.ANONYMOUS_PARTICIPANT.fields()); rawBody = new ObjectMapper().writer(filter).writeValueAsString(model); }catch(IOException e){ e.printStackTrace(); return null; } return ApiConnection.put(action, JsonUtils.createSimpleUserTask(), JsonUtils.createSimpleRefreshUserTask(afterTask), new RequestParams() .setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey()) .setRawBody(rawBody)); } /** * Remove a person from the user * @param id The id of the user to be removed * @param afterTask The task to be executed when the request is finished * @return the task */ public static CancellableTask removePerson(long id, final SimpleRequestTask afterTask){ String action = String.format(ACTION_REMOVE_PERSON, id); PostRequestTask postTask = new PostRequestTask() { @Override public void doTask(Object obj, int statusCode) { if(afterTask != null){ afterTask.doTask(statusCode); } } }; String authKey = JoetzApplication.getContext().getLoginManager().getAuthKey(); return ApiConnection.delete(action, null, postTask, new RequestParams().setAuthKey(authKey)); } }