use of org.eclipse.kuksa.appstore.model.User in project Eclipse-Kuksa-Automotive-Edge-Extension by max-grzanna.
the class AppService method purchaseApp.
public Result<?> purchaseApp(Long userId, Long appId) throws NotFoundException, BadRequestException {
List<User> ownerUserList;
App currentApp = appRepository.findById(appId);
if (currentApp == null) {
throw new NotFoundException("App not found. appId: " + appId);
}
User currentUser = userRepository.findById(userId);
if (currentUser == null) {
throw new NotFoundException("User not found. userId: " + userId);
}
if (currentApp.getOwnerusers().contains(currentUser)) {
throw new BadRequestException("This User already purchased this app!");
}
ownerUserList = currentApp.getOwnerusers();
ownerUserList.add(currentUser);
currentApp.setOwnerusers(ownerUserList);
appRepository.save(currentApp);
return Result.success(HttpStatus.OK, currentApp);
}
use of org.eclipse.kuksa.appstore.model.User in project Eclipse-Kuksa-Automotive-Edge-Extension by max-grzanna.
the class AppService method InstallApp.
public Result<?> InstallApp(String targetDeviceName, Long userId, Long appId) throws NotFoundException, BadRequestException, AlreadyExistException {
App currentApp = appRepository.findById(appId);
if (currentApp == null) {
throw new NotFoundException("App not found. appId: " + appId);
}
User currentUser = userRepository.findById(userId);
if (currentUser == null) {
throw new NotFoundException("User not found. userId: " + userId);
}
List<String> listOfTargets = getListOfTargets(userId);
boolean isOwner = userService.isUsersAppOwner(currentUser.getId().toString(), currentApp.getId(), getListOfOem(listOfTargets));
if (!isOwner) {
throw new BadRequestException("This User is not owner of this app!");
}
if (targetDeviceName == null) {
throw new BadRequestException("targetDeviceName should not be empty!");
}
SoftwareModuleResult currentSoftwareModuleResult = hawkbitFeignClient.getSoftwaremoduleByName(Utils.createFIQLEqual("name", currentApp.getName()) + ";" + Utils.createFIQLEqual("version", currentApp.getVersion()));
DistributionResult lastDistributionResult = hawkbitFeignClient.getDistributionByName(Utils.createFIQLEqual("name", targetDeviceName), 1, "id:DESC");
String version = null;
if (lastDistributionResult.getSize() > 0) {
version = lastDistributionResult.getContent().get(0).getVersion();
} else {
version = "0";
}
int newVersion = Integer.parseInt(version) + 1;
boolean isAlreadyAssigned = false;
Rule ruleNew = new Rule();
ruleNew.setForcetime("1530893371603");
ruleNew.setType("timeforced");
RuleMain rulemain = new RuleMain();
rulemain.setDuration("00:10:00");
rulemain.setSchedule("0 37 8 22 6 ? 2019");
rulemain.setTimezone("+00:00");
ruleNew.setMaintenanceWindow(rulemain);
List<SoftwareModule> softwareModules = new ArrayList<SoftwareModule>();
if (newVersion > 1) {
softwareModules = lastDistributionResult.getContent().get(lastDistributionResult.getSize() - 1).getModules();
if (softwareModules.size() == 1 && softwareModules.get(0).getName().equals(Utils.UNINSTALLED_ALL)) {
softwareModules.remove(0);
}
if (!Utils.isAppAlreadyInstalled(currentSoftwareModuleResult.getContent().get(0), softwareModules)) {
softwareModules.addAll(currentSoftwareModuleResult.getContent());
} else {
isAlreadyAssigned = true;
}
} else {
softwareModules.addAll(currentSoftwareModuleResult.getContent());
}
if (isAlreadyAssigned == false) {
Distribution newDistribution = new Distribution(targetDeviceName, Integer.toString(newVersion));
newDistribution.setName(targetDeviceName);
newDistribution.setDescription(currentApp.getDescription());
newDistribution.setType("app");
newDistribution.setModules(softwareModules);
Response responseCreateDistribution = hawkbitFeignClient.createDistributionSets(Arrays.asList(newDistribution));
if (responseCreateDistribution.status() != HttpStatus.CREATED.value()) {
throw new BadRequestException("Fail Creating Distribution");
}
DistributionResult newDistributionResult = hawkbitFeignClient.getDistributionByName(Utils.createFIQLEqual("name", targetDeviceName), 1, "id:DESC");
if (newDistributionResult.getSize() > 0) {
ruleNew.setId(newDistributionResult.getContent().get(0).getId());
} else {
throw new BadRequestException("This app not found on Hawkbit!");
}
} else {
ruleNew.setId(lastDistributionResult.getContent().get(lastDistributionResult.getSize() - 1).getId());
}
AssignedResult response = hawkbitFeignClient.sendApptoDevice(targetDeviceName, ruleNew);
if (response.getAssigned() > 0) {
List<User> list = currentApp.getInstalledusers();
if (!Utils.isUserAlreadyOwner(currentUser, list)) {
currentApp = incrementAppDownloadCount(currentApp);
list.add(currentUser);
currentApp.setInstalledusers(list);
updateApp(currentApp.getId().toString(), currentApp);
}
return Result.success(HttpStatus.OK);
} else if (response.getAlreadyAssigned() > 0) {
throw new BadRequestException("The updating action is already assigned for selected device.");
} else {
throw new BadRequestException("The updating action hasnt been sent to Hawkbit for selected device.");
}
}
use of org.eclipse.kuksa.appstore.model.User in project Eclipse-Kuksa-Automotive-Edge-Extension by max-grzanna.
the class AppService method UninstallMultiApp.
public Result<?> UninstallMultiApp(String targetDeviceName, Long userId, List<Long> appIds) throws NotFoundException, BadRequestException, AlreadyExistException {
User currentUser = userRepository.findById(userId);
if (currentUser == null) {
throw new NotFoundException("User not found. userId: " + userId);
}
if (targetDeviceName == null) {
throw new BadRequestException("targetDeviceName should not be empty!");
}
Rule ruleNew = new Rule();
ruleNew.setForcetime("1530893371603");
ruleNew.setType("timeforced");
RuleMain rulemain = new RuleMain();
rulemain.setDuration("00:10:00");
rulemain.setSchedule("0 37 8 22 6 ? 2019");
rulemain.setTimezone("+00:00");
ruleNew.setMaintenanceWindow(rulemain);
int newVersion = 0;
List<SoftwareModule> softwareModules = new ArrayList<SoftwareModule>();
DistributionResult lastDistributionResult = hawkbitFeignClient.getDistributionByName(Utils.createFIQLEqual("name", targetDeviceName), 1, "id:DESC");
if (lastDistributionResult.getSize() == 0) {
throw new BadRequestException("This application is already not installed to the selected device!");
}
softwareModules = lastDistributionResult.getContent().get(lastDistributionResult.getSize() - 1).getModules();
String version = null;
if (lastDistributionResult.getSize() > 0) {
version = lastDistributionResult.getContent().get(0).getVersion();
} else {
throw new BadRequestException("Distribution not found for the TargetDevice!");
}
newVersion = Integer.parseInt(version) + 1;
for (Long appId : appIds) {
App currentApp = appRepository.findById(appId);
if (currentApp == null) {
throw new NotFoundException("App not found. appId: " + appId);
}
SoftwareModuleResult currentSoftwareModuleResult = hawkbitFeignClient.getSoftwaremoduleByName(Utils.createFIQLEqual("name", currentApp.getName()) + ";" + Utils.createFIQLEqual("version", currentApp.getVersion()));
boolean isAlreadyAssigned = false;
isAlreadyAssigned = Utils.isAppAlreadyInstalled(currentSoftwareModuleResult.getContent().get(0), softwareModules);
if (isAlreadyAssigned == true) {
softwareModules = Utils.UninstallApp(currentSoftwareModuleResult.getContent().get(0), softwareModules);
} else {
throw new BadRequestException("This App is not installed right now on the device!");
}
}
if (softwareModules.size() == 0) {
// Check UNINSTALLED_ALL MODULE
SoftwareModuleResult softwareModuleResult;
try {
softwareModuleResult = hawkbitFeignClient.getSoftwaremoduleByName(Utils.createFIQLEqual("name", Utils.UNINSTALLED_ALL));
} catch (Exception e) {
throw new BadRequestException("Hawkbit connection error. Check your Hawkbit's IP in the propreties file!");
}
if (softwareModuleResult.getSize() == 0) {
List<SoftwareModule> responsesoftwareModule;
try {
// Create UNINSTALLED_ALL MODULE
List<SoftwareModule> softwareModuleList = new ArrayList<>();
softwareModuleList.add(new SoftwareModule(Utils.UNINSTALLED_ALL, "This software module is a Dummy software module. It was created to assign empty distribution to the device. This situation is for uninstalling device's all apps.", "0", "application", "KUKSA_APPSTORE"));
responsesoftwareModule = hawkbitFeignClient.createSoftwaremodules(softwareModuleList);
// Create UNINSTALLED_ALL MODULE
} catch (Exception e) {
throw new BadRequestException("Hawkbit connection error. Check your Hawkbit's IP in the propreties file!");
}
if (responsesoftwareModule.size() == 0) {
throw new BadRequestException(Utils.UNINSTALLED_ALL + " Dummy App could not be saved to Hawkbit and Appstore!");
} else {
softwareModules.add(responsesoftwareModule.get(0));
}
} else {
softwareModules.add(softwareModuleResult.getContent().get(0));
}
// Check UNINSTALLED_ALL MODULE
}
Distribution newDistribution = new Distribution(targetDeviceName, Integer.toString(newVersion));
newDistribution.setName(targetDeviceName);
newDistribution.setDescription("This distribution is created by Appstore for" + targetDeviceName);
newDistribution.setType("app");
newDistribution.setModules(softwareModules);
Response responseCreateDistribution = hawkbitFeignClient.createDistributionSets(Arrays.asList(newDistribution));
if (responseCreateDistribution.status() != HttpStatus.CREATED.value()) {
throw new BadRequestException("Fail Creating Distribution");
}
DistributionResult newDistributionResult = hawkbitFeignClient.getDistributionByName(Utils.createFIQLEqual("name", targetDeviceName), 1, "id:DESC");
if (newDistributionResult.getSize() > 0) {
ruleNew.setId(newDistributionResult.getContent().get(0).getId());
} else {
throw new BadRequestException("This app not found on Hawkbit!");
}
AssignedResult response = hawkbitFeignClient.sendApptoDevice(targetDeviceName, ruleNew);
if (response.getAssigned() > 0) {
for (Long appId : appIds) {
App currentApp = appRepository.findById(appId);
List<User> list = currentApp.getInstalledusers();
if (Utils.isUserAlreadyOwner(currentUser, list)) {
currentApp.setInstalledusers(Utils.removeOwnerUser(currentUser, list));
updateApp(currentApp.getId().toString(), currentApp);
}
}
return Result.success(HttpStatus.OK);
} else if (response.getAlreadyAssigned() > 0) {
throw new BadRequestException("The updating action is already assigned for selected device.");
} else {
throw new BadRequestException("The updating action hasnt been sent to Hawkbit for selected device.");
}
}
use of org.eclipse.kuksa.appstore.model.User in project Eclipse-Kuksa-Automotive-Edge-Extension by max-grzanna.
the class UserService method updateUser.
public Result<?> updateUser(String userId, User userObject) throws NotFoundException, BadRequestException, AlreadyExistException {
User currentUser = userRepository.findById(Long.parseLong(userId));
if (currentUser == null) {
throw new NotFoundException("User not found. userId: " + userId);
} else if (userObject.getId() != null && !userObject.getId().toString().equals(userId)) {
throw new BadRequestException("The userId parameter and id of userObject should be same!");
} else if (userObject.getUsername() == null || userObject.getUsername().equals("")) {
throw new BadRequestException("Username is mandatory field!");
} else if (!currentUser.getUsername().equals(userObject.getUsername())) {
if (userRepository.findByUsername(userObject.getUsername()) != null) {
throw new AlreadyExistException("New User name already exist. New username: " + userObject.getUsername());
}
}
userObject.setId(currentUser.getId());
userRepository.save(userObject);
return Result.success(HttpStatus.OK, userObject);
}
use of org.eclipse.kuksa.appstore.model.User in project Eclipse-Kuksa-Automotive-Edge-Extension by max-grzanna.
the class UserEditView method addEditColumn.
private void addEditColumn(String caption) {
ImageRenderer<User> renderer = new ImageRenderer<>();
renderer.addClickListener(e -> iconClicked(e.getItem()));
Grid.Column<User, ThemeResource> iconColumn = grid.addColumn(i -> new ThemeResource("img/edit.png"), renderer);
iconColumn.setCaption(caption);
iconColumn.setMaximumWidth(70);
grid.addItemClickListener(e -> {
if (e.getColumn().equals(iconColumn)) {
iconClicked(e.getItem());
}
});
}
Aggregations