/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2018 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.csv.script;

import com.axelor.auth.db.Group;
import com.axelor.auth.db.Permission;
import com.axelor.auth.db.Role;
import com.axelor.auth.db.repo.GroupRepository;
import com.axelor.auth.db.repo.PermissionRepository;
import com.axelor.auth.db.repo.RoleRepository;
import com.axelor.inject.Beans;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ImportPermission {

  @Inject PermissionRepository permissionRepo;

  @Transactional
  public Object importPermission(Object bean, Map<String, Object> values) {
    assert bean instanceof Permission;
    try {

      GroupRepository groupRepository = Beans.get(GroupRepository.class);

      Permission permission = (Permission) bean;
      String groups = (String) values.get("group");
      if (permission.getId() != null) {
        if (groups != null && !groups.isEmpty()) {
          for (Group group :
              groupRepository
                  .all()
                  .filter("code in ?1", Arrays.asList(groups.split("\\|")))
                  .fetch()) {
            Set<Permission> permissions = group.getPermissions();
            if (permissions == null) permissions = new HashSet<Permission>();
            permissions.add(permissionRepo.find(permission.getId()));
            group.setPermissions(permissions);
            groupRepository.save(group);
          }
        }
      }
      return permission;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return bean;
  }

  @Transactional
  public Object importPermissionToRole(Object bean, Map<String, Object> values) {
    
      assert bean instanceof Permission;
    
      Permission permission = (Permission) bean;
      String roleName = values.get("roleName").toString();
      if ( Strings.isNullOrEmpty(roleName) ){ return bean; }
      
      RoleRepository roleRepository = Beans.get(RoleRepository.class);
      Role role = roleRepository.findByName(roleName);
      
      if ( role == null ){ return bean; }
      
      role.addPermission(permission);
      return bean;
      
  }
}

@@ -2443,23 +2443,17 @@
 lues) %7B%0A
-    %0A  
+%0A
     asse
@@ -2483,23 +2483,17 @@
 ission;%0A
-    %0A  
+%0A
     Perm
@@ -2523,34 +2523,32 @@
 rmission) bean;%0A
-  
     String roleN
@@ -2592,23 +2592,20 @@
 ();%0A    
-  
 if (
- 
 Strings.
@@ -2627,19 +2627,25 @@
 oleName)
+)
  
-)
 %7B
+%0A     
  return 
@@ -2645,36 +2645,32 @@
 return bean;
- %7D
 %0A    
-  %0A  
+%7D%0A%0A
     RoleRepo
@@ -2722,26 +2722,24 @@
 ory.class);%0A
-  
     Role rol
@@ -2783,25 +2783,17 @@
 e);%0A
-      %0A  
+%0A
     if (
  rol
@@ -2788,17 +2788,16 @@
     if (
- 
 role == 
@@ -2804,11 +2804,17 @@
 null
- )%7B
+) %7B%0A     
  ret
@@ -2826,20 +2826,16 @@
 ean;
- %7D%0A  
+%0A
     
-%0A  
+%7D%0A%0A
     
@@ -2866,26 +2866,24 @@
 ssion);%0A    
-  
 return bean;
@@ -2887,15 +2887,8 @@
 an;%0A
-      %0A
   %7D%0A

Format the code