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