All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hypertrack.factory.UserFactory Maven / Gradle / Ivy

The newest version!
package io.hypertrack.factory;

import io.hypertrack.model.User;
import io.hypertrack.net.HyperTrackClient;
import io.hypertrack.net.HyperTrackException;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Factory class for User model.
 */
public class UserFactory extends HyperTrackFactory {
    private static String modelUrl = "users/";

    public UserFactory(HyperTrackClient client) {
        super(client);
    }

    @Override
    protected User makeNew(Map params) {
        return new User(params);
    }

    @Override
    protected String getModelUrl() {
        return modelUrl;
    }

    /**
     * Patch user instance with a photo.
     *
     * @param user  User instance to be patched
     * @param photo Photo file
     */
    public void patchWithPhoto(User user, File photo) {
        String userId = user.getId();
        HyperTrackClient client = getClient();
        try {
            Map fileParam = new HashMap<>();
            fileParam.put("photo", photo);
            String response = client.request(getInstanceUrl(userId), fileParam);
            System.out.println("User photo updated: " + response);
            updateFromParams(user, getMapFromResponse(response));
        } catch (HyperTrackException e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * Assign actions to a user
     *
     * @param user      User instance to be assigned actions
     * @param actionIds List of action ids
     */
    public void assignActions(User user, List actionIds) {
        String userId = user.getId();
        HyperTrackClient client = getClient();
        try {
            Map params = new HashMap<>();
            params.put("action_ids", actionIds);
            String response = client.request(getInstanceUrl(userId) + "assign_actions/", "POST", params);
            System.out.println("Action Has been assigned to user: " + response);
            updateFromParams(user, getMapFromResponse(response));
        } catch (HyperTrackException e) {
            e.printStackTrace();
            throw new HyperTrackException("Could not assign actions", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy