Android Open Source - Joetz-Android-V2 Profile Manager






From Project

Back to project page Joetz-Android-V2.

License

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.

Java Source Code

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));
    }
}




Java Source Code List

com.example.jens.myapplication.ApplicationTest.java
com.example.jens.myapplication.account.FragmentAccount.java
com.example.jens.myapplication.account.LoginDialogActivity.java
com.example.jens.myapplication.account.LoginFragment.java
com.example.jens.myapplication.account.PersonsFragment.java
com.example.jens.myapplication.account.ProfileFragment.java
com.example.jens.myapplication.account.RegisterDialogActivity.java
com.example.jens.myapplication.account.RegisterFragment.java
com.example.jens.myapplication.account.TabsPagerAdapter.java
com.example.jens.myapplication.activities.ActivityDetailActivity.java
com.example.jens.myapplication.activities.ActivityDetailFragment.java
com.example.jens.myapplication.activities.FragmentListActivities.java
com.example.jens.myapplication.activities.JoetzActivitiesAdapter.java
com.example.jens.myapplication.activities.JoetzActivity.java
com.example.jens.myapplication.activities.StartActivity.java
com.example.jens.myapplication.apimanager.ApiConnection.java
com.example.jens.myapplication.apimanager.CancellableTask.java
com.example.jens.myapplication.apimanager.JSONParsingTask.java
com.example.jens.myapplication.apimanager.PostRequestTask.java
com.example.jens.myapplication.apimanager.RequestParams.java
com.example.jens.myapplication.apimanager.SimpleRequestTask.java
com.example.jens.myapplication.apimanager.filter.PersonFilter.java
com.example.jens.myapplication.apimanager.manager.ActivityManager.java
com.example.jens.myapplication.apimanager.manager.AuthToken.java
com.example.jens.myapplication.apimanager.manager.BookingManager.java
com.example.jens.myapplication.apimanager.manager.CampManager.java
com.example.jens.myapplication.apimanager.manager.ImageManager.java
com.example.jens.myapplication.apimanager.manager.LoginManager.java
com.example.jens.myapplication.apimanager.manager.ProfileManager.java
com.example.jens.myapplication.apimanager.manager.RegisterManager.java
com.example.jens.myapplication.booking.AddPersonActivity.java
com.example.jens.myapplication.booking.BookingActivity.java
com.example.jens.myapplication.booking.BookingBondFragment.java
com.example.jens.myapplication.booking.BookingContactFragment.java
com.example.jens.myapplication.booking.BookingExtraFragment.java
com.example.jens.myapplication.booking.BookingParticipantsFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailActivity.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryOverviewFragment.java
com.example.jens.myapplication.bookingHistory.BookingsAdapter.java
com.example.jens.myapplication.calendar.FragmentCalendar.java
com.example.jens.myapplication.calendar.KalenderAdapter.java
com.example.jens.myapplication.calendar.KalenderItem.java
com.example.jens.myapplication.camps.CampAdapter.java
com.example.jens.myapplication.camps.CampDetailActivity.java
com.example.jens.myapplication.camps.CampFilterActivity.java
com.example.jens.myapplication.camps.FilterFragment.java
com.example.jens.myapplication.camps.FragmentListCamps.java
com.example.jens.myapplication.camps.ImageActivity.java
com.example.jens.myapplication.camps.ImagePagerFragment.java
com.example.jens.myapplication.camps.InfoFragment.java
com.example.jens.myapplication.camps.PhotoFragment.java
com.example.jens.myapplication.camps.RecyclerItemClickListener.java
com.example.jens.myapplication.camps.SquareImageView.java
com.example.jens.myapplication.camps.TabsPagerAdapter.java
com.example.jens.myapplication.domain.BookingStatus.java
com.example.jens.myapplication.domain.Booking.java
com.example.jens.myapplication.domain.Camp.java
com.example.jens.myapplication.domain.PeriodCategory.java
com.example.jens.myapplication.domain.Person.java
com.example.jens.myapplication.domain.UserRole.java
com.example.jens.myapplication.domain.User.java
com.example.jens.myapplication.domain.binding.PersonValidatorBinding.java
com.example.jens.myapplication.domain.binding.UserValidatorBinding.java
com.example.jens.myapplication.domain.binding.ValidatorBinding.java
com.example.jens.myapplication.domain.converter.ErrorConverter.java
com.example.jens.myapplication.domain.converter.MyStringParser.java
com.example.jens.myapplication.domain.converter.UnixDateTimeConverter.java
com.example.jens.myapplication.domain.validator.PersonValidator.java
com.example.jens.myapplication.domain.validator.UserValidator.java
com.example.jens.myapplication.domain.validator.Validator.java
com.example.jens.myapplication.home.AanbevolenAdapter.java
com.example.jens.myapplication.home.HomeFragment.java
com.example.jens.myapplication.home.ImageSlideAdapter.java
com.example.jens.myapplication.home.Reclame.java
com.example.jens.myapplication.lib.RangeSeekBar.java
com.example.jens.myapplication.main.MainActivity.java
com.example.jens.myapplication.main.NavDrawerAdapter.java
com.example.jens.myapplication.main.NavDrawerItem.java
com.example.jens.myapplication.main.NavMenuItem.java
com.example.jens.myapplication.main.NavMenuSection.java
com.example.jens.myapplication.sam.JoetzApplication.java
com.example.jens.myapplication.sam.MyFragment.java
com.example.jens.myapplication.sam.MySaveableFragment.java
com.example.jens.myapplication.util.AESEncryption.java
com.example.jens.myapplication.util.ActivityUtils.java
com.example.jens.myapplication.util.BindingUtils.java
com.example.jens.myapplication.util.DateTimeStringConverter.java
com.example.jens.myapplication.util.JsonUtils.java
com.example.jens.myapplication.util.MyMonths.java
com.example.jens.myapplication.util.StringUtils.java
com.example.jens.myapplication.view.MyLinearLayout.java