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