jeudi 20 février 2020

How can I to refactor this code and decouple method?

I have set of entities(templates)

enter image description here

I need to clone full this set and create same set with new Ids.

I have 3 methods like this(Entity One is User):

private Map<String, String> createUsers() {
            Map<String, String> userIds = new HashMap<>();
            Iterator<User> users = userService.findAllUsers();
            while (users.hasNext()) {
                User user = users.next();
                String oldId = user.getId();
                user.setId(generateNewId());
                user.setName(generateNewName());
                userService.saveUser(user);
                userIds.put(oldId, user.getId());
            }
            return userIds;
        }

And two similar methods(Entity two is Person and Entity Three is Book). In each method, I create new entity and store old and new IDS. After that, I clone Orders and relink old id and new id. I do it like this:

        if (userIds.containsKey(order.getUserId())) {
            order.setUserId(userIds.get(order.getUserId()));
        }

        if (personIds.containsKey(order.getPersonId())) {
            order.setPersonId(personIds.get(order.getPersonId()));
        }

This structure is ugly and I want to refactor it. But I have not ideas.

Aucun commentaire:

Enregistrer un commentaire