Subversion Repositories bacoAlunos

Rev

Rev 1776 | Rev 1779 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1670 jmachado 1
package pt.estgp.estgweb.domain;
2
 
3
import com.owlike.genson.Genson;
1776 jmachado 4
import com.owlike.genson.GensonBuilder;
1675 jmachado 5
import org.json.JSONArray;
1670 jmachado 6
import org.json.JSONException;
7
import org.json.JSONObject;
8
 
9
import java.io.IOException;
10
import java.io.Serializable;
1675 jmachado 11
import java.util.ArrayList;
1670 jmachado 12
import java.util.List;
1675 jmachado 13
import java.util.Set;
1670 jmachado 14
 
15
/**
16
 * Created by jorgemachado on 23/02/17.
17
 */
18
public class RepositoryDocumentCollectionImpl extends RepositoryDocumentCollection {
19
    @1.5.0/docs/api/java/lang/Override.html">Override
20
    public 1.5.0/docs/api/java/io/Serializable.html">Serializable getSerializable() {
21
        return getId();
22
    }
23
 
24
 
1692 jmachado 25
 
26
 
1675 jmachado 27
    static Genson genson;
1732 jmachado 28
    static Genson gensonSimpleFields;
1675 jmachado 29
 
30
    static {
1776 jmachado 31
        GensonBuilder gensonBuilder =  new Genson.Builder()
1670 jmachado 32
            .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
1778 jmachado 33
            .include(1.5.0/docs/api/java/lang/Boolean.html">Boolean.class)
34
            .include(1.5.0/docs/api/java/lang/Integer.html">Integer.class)
35
            .include(1.5.0/docs/api/java/lang/Long.html">Long.class)
36
            .include(1.5.0/docs/api/java/lang/Double.html">Double.class)
37
            .include(1.5.0/docs/api/java/lang/Float.html">Float.class)
1670 jmachado 38
            .include(RepositoryDocumentCollectionImpl.class)
1732 jmachado 39
            .include("name", RepositoryDocumentCollection.class)
40
            .include("description",RepositoryDocumentCollection.class)
41
            .include("legacyUrl", RepositoryDocumentCollection.class)
42
            .include("treeName", RepositoryDocumentCollection.class)
1755 jmachado 43
            .include("sortField", RepositoryDocumentCollection.class)
1732 jmachado 44
            .include("pathName", RepositoryDocumentCollection.class)
45
            .include("treeHiffens", RepositoryDocumentCollection.class)
46
            .include("collectionManagers", RepositoryDocumentCollection.class)
47
            .include("id",UserImpl.class)
48
            .include("name",UserImpl.class)
49
            .include("sigesCode",SigesUserImpl.class)
1675 jmachado 50
            .exclude("parent", RepositoryDocumentCollection.class);
1732 jmachado 51
 
1675 jmachado 52
        genson = gensonBuilder.create();
1670 jmachado 53
 
1776 jmachado 54
        GensonBuilder gensonBuilderSimpleFields =  new Genson.Builder()
1732 jmachado 55
                .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
1778 jmachado 56
                .include(1.5.0/docs/api/java/lang/Boolean.html">Boolean.class)
57
                .include(1.5.0/docs/api/java/lang/Integer.html">Integer.class)
58
                .include(1.5.0/docs/api/java/lang/Long.html">Long.class)
59
                .include(1.5.0/docs/api/java/lang/Double.html">Double.class)
60
                .include(1.5.0/docs/api/java/lang/Float.html">Float.class)
1732 jmachado 61
                .include(RepositoryDocumentCollectionImpl.class)
62
                .include("name", RepositoryDocumentCollection.class)
63
                .include("description",RepositoryDocumentCollection.class)
64
                .include("legacyUrl", RepositoryDocumentCollection.class)
65
                .include("treeName", RepositoryDocumentCollection.class)
66
                .include("pathName", RepositoryDocumentCollection.class)
67
                .include("treeHiffens", RepositoryDocumentCollection.class)
1755 jmachado 68
                .include("sortField", RepositoryDocumentCollection.class)
1732 jmachado 69
                .exclude("parent", RepositoryDocumentCollection.class);
70
 
71
        gensonSimpleFields = gensonBuilderSimpleFields.create();
72
 
1675 jmachado 73
    }
1776 jmachado 74
    public 1.5.0/docs/api/java/lang/String.html">String toJson() throws 1.5.0/docs/api/java/io/IOException.html">IOException
1670 jmachado 75
    {
76
        return genson.serialize(this);
77
    }
78
 
1776 jmachado 79
    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
1670 jmachado 80
    {
81
        return genson.serialize(collections);
82
    }
1776 jmachado 83
    public static JSONArray toJsonArray(Set<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
1675 jmachado 84
        ArrayList<RepositoryDocumentCollectionImpl> collectionsList  = new ArrayList<RepositoryDocumentCollectionImpl>(collections);
85
        return toJsonArray(collectionsList);
86
    }
1670 jmachado 87
 
1776 jmachado 88
    public static JSONArray toJsonArray(List<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
1675 jmachado 89
        return new JSONArray(genson.serialize(collections));
90
    }
91
 
1776 jmachado 92
    public static JSONArray toJsonArraySimpleFields(List<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException , JSONException {
1732 jmachado 93
        return new JSONArray(gensonSimpleFields.serialize(collections));
94
    }
95
 
1776 jmachado 96
    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 {
1670 jmachado 97
        return genson.deserialize(json, RepositoryDocumentCollectionImpl.class);
98
    }
99
 
1776 jmachado 100
    public JSONObject toJsonObject() throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
1670 jmachado 101
        return new JSONObject(toJson());
102
    }
1675 jmachado 103
 
104
    public 1.5.0/docs/api/java/lang/String.html">String getPathName()
105
    {
106
        if(getParent()==null)
107
            return getName();
108
        else
109
            return getParent().getPathName() + " - " + getName();
110
    }
1692 jmachado 111
 
112
    private 1.5.0/docs/api/java/lang/String.html">String getTreeHiffens()
113
    {
114
        if(getParent() == null)
115
            return "-";
116
        else
117
            return getParent().getTreeHiffens();
118
    }
119
 
120
    public 1.5.0/docs/api/java/lang/String.html">String getTreeName()
121
    {
122
        if(getParent() == null)
123
            return getName();
124
        else
125
            return getParent().getTreeHiffens() + getName();
126
    }
127
    public void setTreeName(1.5.0/docs/api/java/lang/String.html">String name)
128
    {}
1732 jmachado 129
 
130
 
131
    public boolean isManager(UserSession userSession)
132
    {
133
        if(userSession.getUser() == null)
134
            return false;
135
        return checkCollectionManagerInTree(userSession.getUser());
136
    }
137
 
138
    private boolean checkCollectionManagerInTree(User u)
139
    {
140
        if(getCollectionManagers() != null)
141
        {
142
            for(User manager :getCollectionManagers())
143
            {
144
                if(manager.getId() == u.getId())
145
                    return true;
146
            }
147
            return getParent() != null && getParent().checkCollectionManagerInTree(u);
148
        }
149
        return false;
150
    }
1670 jmachado 151
}
152