Rev 1692 | Rev 1755 | 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; |
||
4 | import com.owlike.genson.TransformationException; |
||
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 { |
||
31 | Genson.Builder gensonBuilder = new Genson.Builder() |
||
1670 | jmachado | 32 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
33 | .include(RepositoryDocumentCollectionImpl.class) |
||
1732 | jmachado | 34 | .include("name", RepositoryDocumentCollection.class) |
35 | .include("description",RepositoryDocumentCollection.class) |
||
36 | .include("legacyUrl", RepositoryDocumentCollection.class) |
||
37 | .include("treeName", RepositoryDocumentCollection.class) |
||
38 | .include("pathName", RepositoryDocumentCollection.class) |
||
39 | .include("treeHiffens", RepositoryDocumentCollection.class) |
||
40 | .include("collectionManagers", RepositoryDocumentCollection.class) |
||
41 | .include("id",UserImpl.class) |
||
42 | .include("name",UserImpl.class) |
||
43 | .include("sigesCode",SigesUserImpl.class) |
||
1675 | jmachado | 44 | .exclude("parent", RepositoryDocumentCollection.class); |
1732 | jmachado | 45 | |
1675 | jmachado | 46 | genson = gensonBuilder.create(); |
1670 | jmachado | 47 | |
1732 | jmachado | 48 | Genson.Builder gensonBuilderSimpleFields = new Genson.Builder() |
49 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
||
50 | .include(RepositoryDocumentCollectionImpl.class) |
||
51 | .include("name", RepositoryDocumentCollection.class) |
||
52 | .include("description",RepositoryDocumentCollection.class) |
||
53 | .include("legacyUrl", RepositoryDocumentCollection.class) |
||
54 | .include("treeName", RepositoryDocumentCollection.class) |
||
55 | .include("pathName", RepositoryDocumentCollection.class) |
||
56 | .include("treeHiffens", RepositoryDocumentCollection.class) |
||
57 | .exclude("parent", RepositoryDocumentCollection.class); |
||
58 | |||
59 | gensonSimpleFields = gensonBuilderSimpleFields.create(); |
||
60 | |||
1675 | jmachado | 61 | } |
1670 | jmachado | 62 | public 1.5.0/docs/api/java/lang/String.html">String toJson() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException |
63 | { |
||
64 | return genson.serialize(this); |
||
65 | } |
||
66 | |||
67 | 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 |
||
68 | { |
||
69 | return genson.serialize(collections); |
||
70 | } |
||
1675 | jmachado | 71 | public static JSONArray toJsonArray(Set<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException { |
72 | ArrayList<RepositoryDocumentCollectionImpl> collectionsList = new ArrayList<RepositoryDocumentCollectionImpl>(collections); |
||
73 | return toJsonArray(collectionsList); |
||
74 | } |
||
1670 | jmachado | 75 | |
1675 | jmachado | 76 | public static JSONArray toJsonArray(List<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException { |
77 | return new JSONArray(genson.serialize(collections)); |
||
78 | } |
||
79 | |||
1732 | jmachado | 80 | public static JSONArray toJsonArraySimpleFields(List<RepositoryDocumentCollectionImpl> collections) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException { |
81 | return new JSONArray(gensonSimpleFields.serialize(collections)); |
||
82 | } |
||
83 | |||
1670 | jmachado | 84 | 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 { |
85 | return genson.deserialize(json, RepositoryDocumentCollectionImpl.class); |
||
86 | } |
||
87 | |||
88 | public JSONObject toJsonObject() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException { |
||
89 | return new JSONObject(toJson()); |
||
90 | } |
||
1675 | jmachado | 91 | |
92 | public 1.5.0/docs/api/java/lang/String.html">String getPathName() |
||
93 | { |
||
94 | if(getParent()==null) |
||
95 | return getName(); |
||
96 | else |
||
97 | return getParent().getPathName() + " - " + getName(); |
||
98 | } |
||
1692 | jmachado | 99 | |
100 | private 1.5.0/docs/api/java/lang/String.html">String getTreeHiffens() |
||
101 | { |
||
102 | if(getParent() == null) |
||
103 | return "-"; |
||
104 | else |
||
105 | return getParent().getTreeHiffens(); |
||
106 | } |
||
107 | |||
108 | public 1.5.0/docs/api/java/lang/String.html">String getTreeName() |
||
109 | { |
||
110 | if(getParent() == null) |
||
111 | return getName(); |
||
112 | else |
||
113 | return getParent().getTreeHiffens() + getName(); |
||
114 | } |
||
115 | public void setTreeName(1.5.0/docs/api/java/lang/String.html">String name) |
||
116 | {} |
||
1732 | jmachado | 117 | |
118 | |||
119 | public boolean isManager(UserSession userSession) |
||
120 | { |
||
121 | if(userSession.getUser() == null) |
||
122 | return false; |
||
123 | return checkCollectionManagerInTree(userSession.getUser()); |
||
124 | } |
||
125 | |||
126 | private boolean checkCollectionManagerInTree(User u) |
||
127 | { |
||
128 | if(getCollectionManagers() != null) |
||
129 | { |
||
130 | for(User manager :getCollectionManagers()) |
||
131 | { |
||
132 | if(manager.getId() == u.getId()) |
||
133 | return true; |
||
134 | } |
||
135 | return getParent() != null && getParent().checkCollectionManagerInTree(u); |
||
136 | } |
||
137 | return false; |
||
138 | } |
||
1670 | jmachado | 139 | } |
140 |