private Role getRole(Package apiPackage, String sigenrId) { for (Role role : apiPackage.getRoles()) { for (Signer signer : role.getSigners()) { if (signer.getId().equals(sigenrId)) { return role; } } } return new Role(); }
/** * Create a new package based on an existing template. * * @param packageId * @param aPackage * @return PackageId */ public PackageId createPackageFromTemplate(PackageId packageId, Package aPackage) { String path = template .urlFor(UrlTemplate.TEMPLATE_PATH) .replace("{packageId}", packageId.getId()) .build(); List<Role> roles = aPackage.getRoles(); aPackage.setRoles(Collections.<Role>emptyList()); String packageJson = Serialization.toJson(aPackage); PackageId newPackageId = null; try { String response = client.post(path, packageJson); newPackageId = Serialization.fromJson(response, PackageId.class); } catch (RequestException e) { throw new EslServerException("Could not create a new package", e); } catch (Exception e) { throw new EslException("Could not create a new package", e); } Package createdPackage = getApiPackage(newPackageId.getId()); for (Role role : roles) { String roleUid = findRoleUidByName(createdPackage.getRoles(), role.getName()); if (roleUid == null) { continue; } role.setId(roleUid); updateRole(newPackageId, role); } return newPackageId; }
/** * Updates the package's fields and roles. * * @param packageId * @param sdkPackage * @throws EslException */ public void updatePackage(PackageId packageId, DocumentPackage sdkPackage) throws EslException { String path = template .urlFor(UrlTemplate.PACKAGE_ID_PATH) .replace("{packageId}", packageId.getId()) .build(); Package aPackage = new DocumentPackageConverter(sdkPackage).toAPIPackage(); String packageJson = Serialization.toJson(aPackage); try { client.put(path, packageJson); } catch (RequestException e) { throw new EslServerException("Could not update the package.", e); } catch (Exception e) { throw new EslException("Could not update the package.", e); } // Update roles List<Role> roleList = aPackage.getRoles(); for (Role role : roleList) { updateRole(packageId, role); } }