Skip to content

Commit b53e08c

Browse files
committed
no message
1 parent 4f77d06 commit b53e08c

File tree

4 files changed

+392
-177
lines changed

4 files changed

+392
-177
lines changed

wasdiwebserver/src/main/java/it/fadeout/rest/resources/ProjectResource.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,18 @@ public Response getListByUser(@HeaderParam("x-session-token") String sSessionId,
115115
}
116116

117117
boolean bFoundActiveProject = false;
118+
Map<String, String> asProjectIdSubscriptionId = new HashMap<>();
118119

119120
// For each
120121
for (Project oProject : aoProjects) {
121122
// Create View Model
122123
ProjectListViewModel oProjectViewModel = convert(oProject, aoSubscriptionNames.get(oProject.getSubscriptionId()), oUser.getActiveProjectId());
123124
aoProjectList.add(oProjectViewModel);
124125

126+
if (asProjectIdSubscriptionId.containsKey(oProject.getProjectId())==false) {
127+
asProjectIdSubscriptionId.put(oProject.getProjectId(), oProject.getSubscriptionId());
128+
}
129+
125130
if (oProjectViewModel.isActiveProject()) {
126131
bFoundActiveProject = true;
127132
}
@@ -130,6 +135,14 @@ public Response getListByUser(@HeaderParam("x-session-token") String sSessionId,
130135
// Force a default project if available and none is selected
131136
if (!bFoundActiveProject && aoProjectList.size()>0) {
132137
aoProjectList.get(0).setActiveProject(true);
138+
UserRepository oUserRepository = new UserRepository();
139+
oUser.setActiveProjectId(aoProjectList.get(0).getProjectId());
140+
141+
if (asProjectIdSubscriptionId.containsKey(oUser.getActiveProjectId())) {
142+
oUser.setActiveSubscriptionId(asProjectIdSubscriptionId.get(oUser.getActiveProjectId()));
143+
}
144+
145+
oUserRepository.updateUser(oUser);
133146
}
134147

135148
return Response.ok(aoProjectList).build();

wasdiwebserver/src/main/java/it/fadeout/rest/resources/SubscriptionResource.java

Lines changed: 137 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -83,136 +83,7 @@
8383
*/
8484
@Path("/subscriptions")
8585
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) {
9486

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-
21687
@GET
21788
@Path("/active")
21889
@Produces({ "application/xml", "application/json", "text/xml" })
@@ -235,7 +106,8 @@ public Response getActiveByUser(@HeaderParam("x-session-token") String sSessionI
235106

236107
WasdiLog.debugLog("SubscriptionResource.getActiveByUser: subscriptions for " + oUser.getUserId());
237108

238-
List<SubscriptionListViewModel> aoSubscriptions = getUsersSubscriptionsList(oUser, true);
109+
//List<SubscriptionListViewModel> aoSubscriptions = getUsersSubscriptionsList(oUser, true);
110+
List<SubscriptionListViewModel> aoSubscriptions = PermissionsUtils.getUsersSubscriptionsList(oUser, true);
239111

240112
if (aoSubscriptions == null) {
241113
return Response.status(Status.NOT_FOUND).build();
@@ -294,7 +166,7 @@ public Response getListByUser(@HeaderParam("x-session-token") String sSessionId,
294166

295167
WasdiLog.debugLog("SubscriptionResource.getListByUser: subscriptions for " + oUser.getUserId() + " Valid = " + bValid);
296168

297-
aoSubscriptionLVM = getUsersSubscriptionsList(oUser, bValid);
169+
aoSubscriptionLVM = PermissionsUtils.getUsersSubscriptionsList(oUser, bValid);
298170

299171
return Response.ok(aoSubscriptionLVM).build();
300172
}
@@ -1171,7 +1043,7 @@ public Response getSortedList(@HeaderParam("x-session-token") String sSessionId,
11711043
Subscription oSubscription = aoSubscriptions.get(iSubscriptions);
11721044

11731045
// 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");
11751047

11761048
if (oSubscriptionViewModel != null) {
11771049
oSubscriptionViewModel.setOrganizationId(oSubscription.getOrganizationId());
@@ -1243,48 +1115,7 @@ private static SubscriptionViewModel convert(Subscription oSubscription, String
12431115
return oSubscriptionViewModel;
12441116
}
12451117

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+
12881119

12891120
/**
12901121
* Converts a Subscription View Model to an entity
@@ -1326,8 +1157,7 @@ private Map<String, String> getOrganizationNamesById(Collection<String> asOrgani
13261157
OrganizationRepository oOrganizationRepository = new OrganizationRepository();
13271158
List<Organization> aoOrganizations = oOrganizationRepository.getOrganizations(asOrganizationIds);
13281159

1329-
return aoOrganizations.stream()
1330-
.collect(Collectors.toMap(Organization::getOrganizationId, Organization::getName));
1160+
return aoOrganizations.stream().collect(Collectors.toMap(Organization::getOrganizationId, Organization::getName));
13311161
}
13321162

13331163
private Set<String> getIdsOfSubscriptionsOwnedByUser(String sUserId) {
@@ -1374,6 +1204,137 @@ private static List<SubscriptionTypeViewModel> convert(List<SubscriptionType> ao
13741204
private static SubscriptionTypeViewModel convert(SubscriptionType oSubscriptionType) {
13751205
return new SubscriptionTypeViewModel(oSubscriptionType.name(), oSubscriptionType.getTypeName(), oSubscriptionType.getTypeDescription());
13761206
}
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+
//
13771338

13781339

13791340
}

0 commit comments

Comments
 (0)