How to apply restrictions at all business points in spring / hibernate enterprise application without change codes?

Suppose this class diagram:

enter image description here

I have a class with a name Organizationthat many objects are associated with. there are also many objects in addition to StoreHouseand Personnel, but for a simple class diagram, I did not put these classes in the diagram (it is assumed that more than 1000 classes depend on the class Organization).

Now I want to add a field enabledto the class Organization. it is very simple and no problem. but after that I want all business points and services not to use organizations disabled.

for example, suppose this service is below:

@Service
public class PersonnelService {
    @Autowired
    private PersonnelRepository repository;

    public long save(Personnel entity) {
        return repository.save(entity);
    }
}

If I had the code above in the application, after adding enabledto the field Organization, I should change the method above:

@Service
public class PersonnelService {
    @Autowired
    private PersonnelRepository repository;

    public long save(Personnel entity) {

        if(!entity.getOrganization().getEnabled()) {
            throw Exception();
        }

        return repository.save(entity);
    }
}

- , 1000 . , -, , Aspect - , , , Organization t24 > ?

+4
1

, spring jpa spring . , :

UpdateIfTrue

@Documented
@Target(ElementType.TYPE)
@Retention(RUNTIME)
public @interface UpdateIfTrue {
    String value();
}

@Service("updateIfTrueService")
public class UpdateIfTrueService {

    public boolean canUpdate(Object object){

        Class klass = object.getClass();
        UpdateIfTrue updateIfTrue = (UpdateIfTrue) klass.getAnnotation(UpdateIfTrue.class);
        if (updateIfTrue != null){
            String propertyTree[] = updateIfTrue.value().split(".");

            /*Traverse heirarchy now to get the value of the last one*/
            int i=0;
            try{
                while(i<propertyTree.length){
                    for (Field field : klass.getDeclaredFields()){
                        if (field.getName().equalsIgnoreCase(propertyTree[i])){
                            if (i < (propertyTree.length - 1)){
                                i++;
                                klass = field.getClass();
                                object = field.get(object);
                                break;
                            }
                            else if (i == (propertyTree.length - 1)){
                                i++;
                                klass= field.getClass();
                                object = field.get(object);
                                return (Boolean)object;
                            }
                        }
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }else{
            return true;
        }
        return true;
    }
}

, . ,

@UpdateIfTrue("personnel.organization.enabled")

@Override
@PreAuthorize("@updateIfTrueService.canUpdate(#user)")
User save(@Param("user")User user);
0

Source: https://habr.com/ru/post/1684588/


All Articles