Subversion Repositories bacoAlunos

Rev

Rev 1692 | Rev 1755 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.domain;

import com.owlike.genson.Genson;
import com.owlike.genson.TransformationException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by jorgemachado on 23/02/17.
 */

public class RepositoryDocumentCollectionImpl extends RepositoryDocumentCollection {
    @1.5.0/docs/api/java/lang/Override.html">Override
    public 1.5.0/docs/api/java/io/Serializable.html">Serializable getSerializable() {
        return getId();
    }




    static Genson genson;
    static Genson gensonSimpleFields;

    static {
        Genson.Builder gensonBuilder =  new Genson.Builder()
            .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
            .include(RepositoryDocumentCollectionImpl.class)
            .include("name", RepositoryDocumentCollection.class)
            .include("description",RepositoryDocumentCollection.class)
            .include("legacyUrl", RepositoryDocumentCollection.class)
            .include("treeName", RepositoryDocumentCollection.class)
            .include("pathName", RepositoryDocumentCollection.class)
            .include("treeHiffens", RepositoryDocumentCollection.class)
            .include("collectionManagers", RepositoryDocumentCollection.class)
            .include("id",UserImpl.class)
            .include("name",UserImpl.class)
            .include("sigesCode",SigesUserImpl.class)
            .exclude("parent", RepositoryDocumentCollection.class);

        genson = gensonBuilder.create();

        Genson.Builder gensonBuilderSimpleFields =  new Genson.Builder()
                .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
                .include(RepositoryDocumentCollectionImpl.class)
                .include("name", RepositoryDocumentCollection.class)
                .include("description",RepositoryDocumentCollection.class)
                .include("legacyUrl", RepositoryDocumentCollection.class)
                .include("treeName", RepositoryDocumentCollection.class)
                .include("pathName", RepositoryDocumentCollection.class)
                .include("treeHiffens", RepositoryDocumentCollection.class)
                .exclude("parent", RepositoryDocumentCollection.class);

        gensonSimpleFields = gensonBuilderSimpleFields.create();

    }
    public 1.5.0/docs/api/java/lang/String.html">String toJson() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
    {
        return genson.serialize(this);
    }

    public static 1.5.0/docs/api/java/lang/String.html">String toJson(List<RepositoryDocumentCollection> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
    {
        return genson.serialize(collections);
    }
    public static JSONArray toJsonArray(Set<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException {
        ArrayList<RepositoryDocumentCollectionImpl> collectionsList  = new ArrayList<RepositoryDocumentCollectionImpl>(collections);
        return toJsonArray(collectionsList);
    }

    public static JSONArray toJsonArray(List<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException {
        return new JSONArray(genson.serialize(collections));
    }

    public static JSONArray toJsonArraySimpleFields(List<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException {
        return new JSONArray(gensonSimpleFields.serialize(collections));
    }

    public static RepositoryDocumentCollectionImpl loadFromJson(1.5.0/docs/api/java/lang/String.html">String json) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException {
        return genson.deserialize(json, RepositoryDocumentCollectionImpl.class);
    }

    public JSONObject toJsonObject() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException {
        return new JSONObject(toJson());
    }

    public 1.5.0/docs/api/java/lang/String.html">String getPathName()
    {
        if(getParent()==null)
            return getName();
        else
            return getParent().getPathName() + " - " + getName();
    }

    private 1.5.0/docs/api/java/lang/String.html">String getTreeHiffens()
    {
        if(getParent() == null)
            return "-";
        else
            return getParent().getTreeHiffens();
    }

    public 1.5.0/docs/api/java/lang/String.html">String getTreeName()
    {
        if(getParent() == null)
            return getName();
        else
            return getParent().getTreeHiffens() + getName();
    }
    public void setTreeName(1.5.0/docs/api/java/lang/String.html">String name)
    {}


    public boolean isManager(UserSession userSession)
    {
        if(userSession.getUser() == null)
            return false;
        return checkCollectionManagerInTree(userSession.getUser());
    }

    private boolean checkCollectionManagerInTree(User u)
    {
        if(getCollectionManagers() != null)
        {
            for(User manager :getCollectionManagers())
            {
                if(manager.getId() == u.getId())
                    return true;
            }
            return getParent() != null && getParent().checkCollectionManagerInTree(u);
        }
        return false;
    }
}