- @Override
- public void renamePolicy(String oldPath, String newPath,String username){
-/* String[] oldPolicy = getScopeAndNameAndType(oldPath);
- String[] newPolicy = getScopeAndNameAndType(newPath);
- if(oldPolicy == null || newPolicy == null){
- PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW+"Could not parse one or more of the path names: "
- +oldPath+", "+newPath);
- throw new IllegalArgumentException("Could not parse one or more of the path names");
- }
- synchronized (emLock) {
- checkBeforeOperationRun();
-
- PolicyEntity existingPolicy;
- boolean existingPolicyDeleted = false;
- List<?> groups = null;
- try{
- existingPolicy = getPolicy(newPolicy[1],newPolicy[0]);
- } catch(Exception e){
- existingPolicy = null;
- }
- if(existingPolicy != null && !existingPolicy.isDeleted()){
- logger.error("The policy named "+existingPolicy.getPolicyName()+" already exists, cannot rename policy: "+newPolicy);
- throw new IllegalArgumentException("The policy named "+existingPolicy.getPolicyName()+" already exists, cannot rename policy: "+newPolicy);
- } else if(existingPolicy != null && existingPolicy.isDeleted()){
- try{
- Query getGroups = em.createQuery("SELECT g FROM GroupEntity g JOIN g.policies p WHERE p.policyId=:pid");
-
- getGroups.setParameter("pid", existingPolicy.getPolicyId());
- groups = getGroups.getResultList();
- }catch(Exception e){
- groups = new LinkedList<>();
- }
- for(Object o : groups){
- GroupEntity group = (GroupEntity)o;
- group.removePolicyFromGroup(existingPolicy);
- }
- try{
- em.flush();
- }catch(Exception e){
- logger.error("Error while removing the policy from groups: "+existingPolicy.getPolicyName());
- }
- try{
- em.remove(existingPolicy);
- em.flush();
- }catch(Exception e){
- logger.error("Could not remove the existing deleted policy: "+existingPolicy.getPolicyName());
- }
- existingPolicyDeleted = true;
- //create the new policy
- //for each of the groups, add the new policy
- }
-
- PolicyEntity policyToRename;
- try{
- policyToRename = getPolicy(oldPolicy[1],oldPolicy[0]);
- } catch(Exception e){
- PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "PolicyDBDao", "Could not get policy record to rename: "
- +oldPolicy[1]);
- throw new PersistenceException("Could not get policy record to rename");
- }
- String policyDataString = null;
- InputStream fileContentStream = null;
- String policyFilePath = Paths.get(oldPath).toAbsolutePath().toString();
- //I want to try the old path first, then if it doesn't work, try the new path
- for(int i=0;i<2;i++){
- try {
- fileContentStream = new FileInputStream(policyFilePath);
- policyDataString = IOUtils.toString(fileContentStream);
- } catch (FileNotFoundException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught FileNotFoundException on new FileInputStream("+policyFilePath+")");
- //if we can't find the oldPath, we'll try the new path
- if(i == 0){
- policyFilePath = Paths.get(newPath).toAbsolutePath().toString();
- continue;
- }
- throw new IllegalArgumentException("The file path does not exist");
- } catch(IOException e2){
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Caught IOException on newIOUtils.toString("+fileContentStream+")");
- throw new IllegalArgumentException("The file path cannot be read");
- } finally {
- IOUtils.closeQuietly(fileContentStream);
- }
- if(policyDataString == null){
- throw new IllegalArgumentException("The file path cannot be read");
- }
- //escape the loop
- i=2;
- }
- policyToRename.setPolicyName(newPolicy[1]);
- policyToRename.setPolicyData(policyDataString);
- policyToRename.setScope(newPolicy[0]);
- policyToRename.setModifiedBy(username);
- if(policyToRename.getConfigurationData() != null){
- String configType = policyToRename.getConfigurationData().getConfigType();
- policyToRename.getConfigurationData().setConfigurationName(getConfigFile(newPolicy[1], configType));
- policyToRename.getConfigurationData().setModifiedBy(username);
- }
- if(policyToRename.getActionBodyEntity() != null){
- String newActionName = newPolicy[0]+"."+removeFileExtension(newPolicy[1])+".json";
- policyToRename.getActionBodyEntity().setActionBodyName(newActionName);
- policyToRename.getActionBodyEntity().setModifiedBy(username);
- }
- if(existingPolicyDeleted){
- for(Object o : groups){
-
- GroupEntity group = (GroupEntity)o;
- group.addPolicyToGroup(policyToRename);
- }
- }
- em.flush();
- this.policyId = policyToRename.getPolicyId();
- this.newGroupId = oldPath;
- }*/
- }
-