83
83
*/
84
84
@ Path ("/subscriptions" )
85
85
public class SubscriptionResource {
86
-
87
- /**
88
- * Extract a List of subscriptions for the user
89
- * @param oUser Target User
90
- * @param bValid True to get only valid subscriptions
91
- * @return
92
- */
93
- protected List <SubscriptionListViewModel > getUsersSubscriptionsList (User oUser , boolean bValid ) {
94
86
95
- List <SubscriptionListViewModel > aoSubscriptionLVM = new ArrayList <>();
96
-
97
- // Domain Check
98
- if (oUser == null ) {
99
- WasdiLog .warnLog ("SubscriptionResource.getUsersSubscriptionsList: invalid session" );
100
- return aoSubscriptionLVM ;
101
- }
102
-
103
- try {
104
- // 1. subscriptions directly owned by the user
105
- // 2. subscriptions belonging to organizations owned by the user
106
- // 3. subscriptions belonging to organizations shared with the user
107
- // 4. subscriptions shared with the user on individual basis
108
-
109
-
110
- // Get the list of Subscriptions owned by the user
111
- SubscriptionRepository oSubscriptionRepository = new SubscriptionRepository ();
112
- List <Subscription > aoOwnedSubscriptions = oSubscriptionRepository .getSubscriptionsByUser (oUser .getUserId ());
113
-
114
- Set <String > asOwnedSubscriptionIds = aoOwnedSubscriptions .stream ().map (Subscription ::getSubscriptionId ).collect (Collectors .toSet ());
115
- Set <String > asOrganizationIdsOfOwnedSubscriptions = aoOwnedSubscriptions .stream ().map (Subscription ::getOrganizationId ).filter (Objects ::nonNull ).collect (Collectors .toSet ());
116
- Map <String , String > aoOrganizationNamesOfOwnedSubscriptions = getOrganizationNamesById (asOrganizationIdsOfOwnedSubscriptions );
117
-
118
- // For each
119
- for (Subscription oSubscription : aoOwnedSubscriptions ) {
120
-
121
- if (bValid ) {
122
- if (!oSubscription .isValid ()) continue ;
123
- }
124
-
125
- // Create View Model
126
- SubscriptionListViewModel oSubscriptionViewModel = convertSubscriptionToViewModel (oSubscription , oUser .getUserId (), aoOrganizationNamesOfOwnedSubscriptions .get (oSubscription .getOrganizationId ()), "owner" );
127
-
128
- if (oSubscriptionViewModel != null ) {
129
- oSubscriptionViewModel .setOrganizationId (oSubscription .getOrganizationId ());
130
- oSubscriptionViewModel .setReadOnly (false );
131
- aoSubscriptionLVM .add (oSubscriptionViewModel );
132
- }
133
- else {
134
- WasdiLog .warnLog ("SubscriptionResource.getUsersSubscriptionsList: Error converting a Owned Subscription, jumping" );
135
- }
136
- }
137
-
138
- UserResourcePermissionRepository oUserResourcePermissionRepository = new UserResourcePermissionRepository ();
139
-
140
- // Get the list of Subscriptions shared by organizations
141
- Set <String > asOrganizationIdsOfOwnedByOrSharedWithUser = new OrganizationResource ().getIdsOfOrganizationsOwnedByOrSharedWithUser (oUser .getUserId ());
142
- Map <String , String > aoOrganizationNamesOfOwnedByOrSharedWithUser = getOrganizationNamesById (asOrganizationIdsOfOwnedByOrSharedWithUser );
143
-
144
- List <Subscription > aoOrganizationalSubscriptions = getSubscriptionsSharedByOrganizations (asOrganizationIdsOfOwnedByOrSharedWithUser );
145
-
146
- for (Subscription oSubscription : aoOrganizationalSubscriptions ) {
147
- if (!asOwnedSubscriptionIds .contains (oSubscription .getSubscriptionId ())) {
148
-
149
- boolean bToAdd =true ;
150
-
151
- if (bValid ) {
152
- bToAdd = oSubscription .isValid ();
153
- }
154
-
155
-
156
- if (bToAdd ) {
157
- SubscriptionListViewModel oSubscriptionViewModel = convertSubscriptionToViewModel (oSubscription , oUser .getUserId (), aoOrganizationNamesOfOwnedByOrSharedWithUser .get (oSubscription .getOrganizationId ()), "shared by " + aoOrganizationNamesOfOwnedByOrSharedWithUser .get (oSubscription .getOrganizationId ()));
158
-
159
- if (oSubscriptionViewModel != null ) {
160
-
161
- oSubscriptionViewModel .setReadOnly (!PermissionsUtils .canUserWriteSubscription (oUser .getUserId (), oSubscriptionViewModel .getSubscriptionId ()));
162
- aoSubscriptionLVM .add (oSubscriptionViewModel );
163
- }
164
- else {
165
- WasdiLog .warnLog ("SubscriptionResource.getUsersSubscriptionsList: Error converting an Organization Subscription, jumping" );
166
- }
167
-
168
- }
169
- }
170
- }
171
-
172
-
173
- // Get the list of Subscriptions shared with this user
174
- List <Subscription > aoSharedSubscriptions = getSubscriptionsSharedWithUser (oUser .getUserId ());
175
- List <UserResourcePermission > aoSubscriptionSharings = oUserResourcePermissionRepository .getSubscriptionSharingsByUserId (oUser .getUserId ());
176
-
177
- Map <String , String > aoSubscriptionUser = aoSubscriptionSharings .stream ().collect (Collectors .toMap (UserResourcePermission ::getResourceId , UserResourcePermission ::getUserId ));
178
-
179
-
180
- List <String > asOrganizationIdsOfDirectSubscriptionSharings = aoSharedSubscriptions .stream ().map (Subscription ::getOrganizationId ).collect (Collectors .toList ());
181
-
182
- Map <String , String > asNamesOfOrganizationsOfDirectSubscriptionSharings = getOrganizationNamesById (asOrganizationIdsOfDirectSubscriptionSharings );
183
-
184
- for (Subscription oSubscription : aoSharedSubscriptions ) {
185
-
186
- boolean bToAdd =true ;
187
-
188
- if (bValid ) {
189
- bToAdd = oSubscription .isValid ();
190
- }
191
-
192
- if (bToAdd ) {
193
- SubscriptionListViewModel oSubscriptionViewModel = convertSubscriptionToViewModel (oSubscription , oUser .getUserId (),
194
- asNamesOfOrganizationsOfDirectSubscriptionSharings .get (oSubscription .getOrganizationId ()),
195
- "shared by " + aoSubscriptionUser .get (oSubscription .getSubscriptionId ()));
196
-
197
- if (oSubscriptionViewModel !=null ) {
198
- oSubscriptionViewModel .setReadOnly (!PermissionsUtils .canUserWriteSubscription (oUser .getUserId (), oSubscriptionViewModel .getSubscriptionId ()));
199
- aoSubscriptionLVM .add (oSubscriptionViewModel );
200
- }
201
- else {
202
- WasdiLog .warnLog ("SubscriptionResource.getUsersSubscriptionsList: Error converting a Shared Subscription, jumping" );
203
- }
204
- }
205
- }
206
-
207
- return aoSubscriptionLVM ;
208
- }
209
- catch (Exception oEx ) {
210
- WasdiLog .errorLog ("SubscriptionResource.getUsersSubscriptionsList error: " + oEx );
211
- return aoSubscriptionLVM ;
212
- }
213
- }
214
-
215
-
216
87
@ GET
217
88
@ Path ("/active" )
218
89
@ Produces ({ "application/xml" , "application/json" , "text/xml" })
@@ -235,7 +106,8 @@ public Response getActiveByUser(@HeaderParam("x-session-token") String sSessionI
235
106
236
107
WasdiLog .debugLog ("SubscriptionResource.getActiveByUser: subscriptions for " + oUser .getUserId ());
237
108
238
- List <SubscriptionListViewModel > aoSubscriptions = getUsersSubscriptionsList (oUser , true );
109
+ //List<SubscriptionListViewModel> aoSubscriptions = getUsersSubscriptionsList(oUser, true);
110
+ List <SubscriptionListViewModel > aoSubscriptions = PermissionsUtils .getUsersSubscriptionsList (oUser , true );
239
111
240
112
if (aoSubscriptions == null ) {
241
113
return Response .status (Status .NOT_FOUND ).build ();
@@ -294,7 +166,7 @@ public Response getListByUser(@HeaderParam("x-session-token") String sSessionId,
294
166
295
167
WasdiLog .debugLog ("SubscriptionResource.getListByUser: subscriptions for " + oUser .getUserId () + " Valid = " + bValid );
296
168
297
- aoSubscriptionLVM = getUsersSubscriptionsList (oUser , bValid );
169
+ aoSubscriptionLVM = PermissionsUtils . getUsersSubscriptionsList (oUser , bValid );
298
170
299
171
return Response .ok (aoSubscriptionLVM ).build ();
300
172
}
@@ -1171,7 +1043,7 @@ public Response getSortedList(@HeaderParam("x-session-token") String sSessionId,
1171
1043
Subscription oSubscription = aoSubscriptions .get (iSubscriptions );
1172
1044
1173
1045
// Create View Model
1174
- SubscriptionListViewModel oSubscriptionViewModel = convertSubscriptionToViewModel (oSubscription , oUser .getUserId (), aoOrganizationNames .get (oSubscription .getOrganizationId ()), "owner" );
1046
+ SubscriptionListViewModel oSubscriptionViewModel = Subscription . convertSubscriptionToViewModel (oSubscription , oUser .getUserId (), aoOrganizationNames .get (oSubscription .getOrganizationId ()), "owner" );
1175
1047
1176
1048
if (oSubscriptionViewModel != null ) {
1177
1049
oSubscriptionViewModel .setOrganizationId (oSubscription .getOrganizationId ());
@@ -1243,48 +1115,7 @@ private static SubscriptionViewModel convert(Subscription oSubscription, String
1243
1115
return oSubscriptionViewModel ;
1244
1116
}
1245
1117
1246
- /**
1247
- * Convert a Subscription entity to a view model
1248
- * @param oSubscription
1249
- * @param sCurrentUserId
1250
- * @param sOrganizationName
1251
- * @param sReason
1252
- * @return
1253
- */
1254
- private static SubscriptionListViewModel convertSubscriptionToViewModel (Subscription oSubscription , String sCurrentUserId , String sOrganizationName , String sReason ) {
1255
- try {
1256
- SubscriptionListViewModel oSubscriptionListViewModel = new SubscriptionListViewModel ();
1257
- oSubscriptionListViewModel .setSubscriptionId (oSubscription .getSubscriptionId ());
1258
- oSubscriptionListViewModel .setName (oSubscription .getName ());
1259
-
1260
- if (oSubscription .getType ()!=null ) {
1261
- oSubscriptionListViewModel .setTypeId (oSubscription .getType ());
1262
- oSubscriptionListViewModel .setTypeName (SubscriptionType .get (oSubscription .getType ()).getTypeName ());
1263
- }
1264
- else {
1265
- oSubscriptionListViewModel .setTypeId (SubscriptionType .Free .getTypeId ());
1266
- oSubscriptionListViewModel .setTypeName (SubscriptionType .Free .getTypeName ());
1267
- }
1268
- oSubscriptionListViewModel .setOrganizationName (sOrganizationName );
1269
- oSubscriptionListViewModel .setReason (sReason );
1270
- oSubscriptionListViewModel .setStartDate (TimeEpochUtils .fromEpochToDateString (oSubscription .getStartDate ()));
1271
- oSubscriptionListViewModel .setEndDate (TimeEpochUtils .fromEpochToDateString (oSubscription .getEndDate ()));
1272
- oSubscriptionListViewModel .setBuySuccess (oSubscription .isBuySuccess ());
1273
- oSubscriptionListViewModel .setOwnerUserId (oSubscription .getUserId ());
1274
-
1275
- return oSubscriptionListViewModel ;
1276
- }
1277
- catch (Exception oEx ) {
1278
- WasdiLog .errorLog ("SubscriptionResource.convert exception: " , oEx );
1279
- if (oSubscription !=null ) {
1280
- String sSubId = oSubscription .getSubscriptionId ();
1281
- if (Utils .isNullOrEmpty (sSubId )) sSubId = "Subscritption Id == NULL" ;
1282
-
1283
- WasdiLog .errorLog ("SubscriptionResource.convert Subscription Id: " + sSubId );
1284
- }
1285
- return null ;
1286
- }
1287
- }
1118
+
1288
1119
1289
1120
/**
1290
1121
* Converts a Subscription View Model to an entity
@@ -1326,8 +1157,7 @@ private Map<String, String> getOrganizationNamesById(Collection<String> asOrgani
1326
1157
OrganizationRepository oOrganizationRepository = new OrganizationRepository ();
1327
1158
List <Organization > aoOrganizations = oOrganizationRepository .getOrganizations (asOrganizationIds );
1328
1159
1329
- return aoOrganizations .stream ()
1330
- .collect (Collectors .toMap (Organization ::getOrganizationId , Organization ::getName ));
1160
+ return aoOrganizations .stream ().collect (Collectors .toMap (Organization ::getOrganizationId , Organization ::getName ));
1331
1161
}
1332
1162
1333
1163
private Set <String > getIdsOfSubscriptionsOwnedByUser (String sUserId ) {
@@ -1374,6 +1204,137 @@ private static List<SubscriptionTypeViewModel> convert(List<SubscriptionType> ao
1374
1204
private static SubscriptionTypeViewModel convert (SubscriptionType oSubscriptionType ) {
1375
1205
return new SubscriptionTypeViewModel (oSubscriptionType .name (), oSubscriptionType .getTypeName (), oSubscriptionType .getTypeDescription ());
1376
1206
}
1207
+
1208
+
1209
+
1210
+ // 2025-07-14: Moved to PermissionUtils: kept here some time
1211
+ // /**
1212
+ // * Extract a List of subscriptions for the user
1213
+ // * @param oUser Target User
1214
+ // * @param bValid True to get only valid subscriptions
1215
+ // * @return
1216
+ // */
1217
+ // protected List<SubscriptionListViewModel> getUsersSubscriptionsList(User oUser, boolean bValid) {
1218
+ //
1219
+ // List<SubscriptionListViewModel> aoSubscriptionLVM = new ArrayList<>();
1220
+ //
1221
+ // // Domain Check
1222
+ // if (oUser == null) {
1223
+ // WasdiLog.warnLog("SubscriptionResource.getUsersSubscriptionsList: invalid session");
1224
+ // return aoSubscriptionLVM;
1225
+ // }
1226
+ //
1227
+ // try {
1228
+ // // 1. subscriptions directly owned by the user
1229
+ // // 2. subscriptions belonging to organizations owned by the user
1230
+ // // 3. subscriptions belonging to organizations shared with the user
1231
+ // // 4. subscriptions shared with the user on individual basis
1232
+ //
1233
+ //
1234
+ // // Get the list of Subscriptions owned by the user
1235
+ // SubscriptionRepository oSubscriptionRepository = new SubscriptionRepository();
1236
+ // List<Subscription> aoOwnedSubscriptions = oSubscriptionRepository.getSubscriptionsByUser(oUser.getUserId());
1237
+ //
1238
+ // Set<String> asOwnedSubscriptionIds = aoOwnedSubscriptions.stream().map(Subscription::getSubscriptionId).collect(Collectors.toSet());
1239
+ // Set<String> asOrganizationIdsOfOwnedSubscriptions = aoOwnedSubscriptions.stream().map(Subscription::getOrganizationId).filter(Objects::nonNull).collect(Collectors.toSet());
1240
+ // Map<String, String> aoOrganizationNamesOfOwnedSubscriptions = getOrganizationNamesById(asOrganizationIdsOfOwnedSubscriptions);
1241
+ //
1242
+ // // For each
1243
+ // for (Subscription oSubscription : aoOwnedSubscriptions) {
1244
+ //
1245
+ // if (bValid) {
1246
+ // if (!oSubscription.isValid()) continue;
1247
+ // }
1248
+ //
1249
+ // // Create View Model
1250
+ // SubscriptionListViewModel oSubscriptionViewModel = Subscription.convertSubscriptionToViewModel(oSubscription, oUser.getUserId(), aoOrganizationNamesOfOwnedSubscriptions.get(oSubscription.getOrganizationId()), "owner");
1251
+ //
1252
+ // if (oSubscriptionViewModel != null) {
1253
+ // oSubscriptionViewModel.setOrganizationId(oSubscription.getOrganizationId());
1254
+ // oSubscriptionViewModel.setReadOnly(false);
1255
+ // aoSubscriptionLVM.add(oSubscriptionViewModel);
1256
+ // }
1257
+ // else {
1258
+ // WasdiLog.warnLog("SubscriptionResource.getUsersSubscriptionsList: Error converting a Owned Subscription, jumping");
1259
+ // }
1260
+ // }
1261
+ //
1262
+ // UserResourcePermissionRepository oUserResourcePermissionRepository = new UserResourcePermissionRepository();
1263
+ //
1264
+ // // Get the list of Subscriptions shared by organizations
1265
+ // Set<String> asOrganizationIdsOfOwnedByOrSharedWithUser = new OrganizationResource().getIdsOfOrganizationsOwnedByOrSharedWithUser(oUser.getUserId());
1266
+ // Map<String, String> aoOrganizationNamesOfOwnedByOrSharedWithUser = getOrganizationNamesById(asOrganizationIdsOfOwnedByOrSharedWithUser);
1267
+ //
1268
+ // List<Subscription> aoOrganizationalSubscriptions = getSubscriptionsSharedByOrganizations(asOrganizationIdsOfOwnedByOrSharedWithUser);
1269
+ //
1270
+ // for (Subscription oSubscription : aoOrganizationalSubscriptions) {
1271
+ // if (!asOwnedSubscriptionIds.contains(oSubscription.getSubscriptionId())) {
1272
+ //
1273
+ // boolean bToAdd=true;
1274
+ //
1275
+ // if (bValid) {
1276
+ // bToAdd = oSubscription.isValid();
1277
+ // }
1278
+ //
1279
+ //
1280
+ // if (bToAdd) {
1281
+ // SubscriptionListViewModel oSubscriptionViewModel = Subscription.convertSubscriptionToViewModel(oSubscription, oUser.getUserId(), aoOrganizationNamesOfOwnedByOrSharedWithUser.get(oSubscription.getOrganizationId()), "shared by " + aoOrganizationNamesOfOwnedByOrSharedWithUser.get(oSubscription.getOrganizationId()));
1282
+ //
1283
+ // if (oSubscriptionViewModel != null) {
1284
+ //
1285
+ // oSubscriptionViewModel.setReadOnly(!PermissionsUtils.canUserWriteSubscription(oUser.getUserId(), oSubscriptionViewModel.getSubscriptionId()));
1286
+ // aoSubscriptionLVM.add(oSubscriptionViewModel);
1287
+ // }
1288
+ // else {
1289
+ // WasdiLog.warnLog("SubscriptionResource.getUsersSubscriptionsList: Error converting an Organization Subscription, jumping");
1290
+ // }
1291
+ // }
1292
+ // }
1293
+ // }
1294
+ //
1295
+ //
1296
+ // // Get the list of Subscriptions shared with this user
1297
+ // List<Subscription> aoSharedSubscriptions = getSubscriptionsSharedWithUser(oUser.getUserId());
1298
+ // List<UserResourcePermission> aoSubscriptionSharings = oUserResourcePermissionRepository.getSubscriptionSharingsByUserId(oUser.getUserId());
1299
+ //
1300
+ // Map<String, String> aoSubscriptionUser = aoSubscriptionSharings.stream().collect(Collectors.toMap(UserResourcePermission::getResourceId, UserResourcePermission::getUserId));
1301
+ //
1302
+ //
1303
+ // List<String> asOrganizationIdsOfDirectSubscriptionSharings = aoSharedSubscriptions.stream().map(Subscription::getOrganizationId).collect(Collectors.toList());
1304
+ //
1305
+ // Map<String, String> asNamesOfOrganizationsOfDirectSubscriptionSharings = getOrganizationNamesById(asOrganizationIdsOfDirectSubscriptionSharings);
1306
+ //
1307
+ // for (Subscription oSubscription : aoSharedSubscriptions) {
1308
+ //
1309
+ // boolean bToAdd=true;
1310
+ //
1311
+ // if (bValid) {
1312
+ // bToAdd = oSubscription.isValid();
1313
+ // }
1314
+ //
1315
+ // if (bToAdd) {
1316
+ // SubscriptionListViewModel oSubscriptionViewModel = Subscription.convertSubscriptionToViewModel(oSubscription, oUser.getUserId(),
1317
+ // asNamesOfOrganizationsOfDirectSubscriptionSharings.get(oSubscription.getOrganizationId()),
1318
+ // "shared by " + aoSubscriptionUser.get(oSubscription.getSubscriptionId()));
1319
+ //
1320
+ // if (oSubscriptionViewModel!=null) {
1321
+ // oSubscriptionViewModel.setReadOnly(!PermissionsUtils.canUserWriteSubscription(oUser.getUserId(), oSubscriptionViewModel.getSubscriptionId()));
1322
+ // aoSubscriptionLVM.add(oSubscriptionViewModel);
1323
+ // }
1324
+ // else {
1325
+ // WasdiLog.warnLog("SubscriptionResource.getUsersSubscriptionsList: Error converting a Shared Subscription, jumping");
1326
+ // }
1327
+ // }
1328
+ // }
1329
+ //
1330
+ // return aoSubscriptionLVM;
1331
+ // }
1332
+ // catch (Exception oEx) {
1333
+ // WasdiLog.errorLog("SubscriptionResource.getUsersSubscriptionsList error: " + oEx);
1334
+ // return aoSubscriptionLVM;
1335
+ // }
1336
+ // }
1337
+ //
1377
1338
1378
1339
1379
1340
}
0 commit comments