Thursday, 16 May 2013

Dynamically changing the enum type ids

Have you ever felt, modelling a "STATUS" column as a enum type in any of your ticketing system?? Obviously, most of the people opt for it. I am here with one of example here...

eg:

public enum Status {
      
     OPEN("Open" , 1),
     IN_PROGRESS("In_Progress", 2),
     RESOLVED("Resolved", 3),
     CLOSE("Close", 4);

     private String name;
     private Integer id;

     private Status(String name, Integer id) {
           this.name = name;
           this.id = id;
     }

     public String getName() {
             return this.name;
     }

     public Integer getId() {
            return this.id;
     }
}

In the above mentioned enum class, whenever we change the id of the statuses due to environmental specific reasons, developers need to modify this enum class to keep the mapping between state names and the state ids.

How can we capture only state names in the enums, and ids need to be captured during the start of application dynamically??



public enum Status {

    OPEN ("Open"),

 IN_PROGRESS("In_Progress"),
     RESOLVED("Resolved"),

    CLOSE("Close");


    private String name;
    private Integer id;

    private CaseStatus(final String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String toString() {
        return this.name;
    }
}

Now, how do we map the ids with the states? Now, we will have a configuration class, which helps this. Of course, I have used the spring abilities here, you may use which is relevant for your scenarios.



import Status;
import models.Status;
import repositories.StatusRepository;
import org.springframework.stereotype.Service;
import play.Logger;
import sun.reflect.ReflectionFactory;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class StatusConfiguration {

    private static ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();

    @Resource
    private StatusRepository statusRepository;


    @PostConstruct
    private void initialize() {
        Map<String, Integer> statuses = this.fetchStatuses();

        if (null != statuses && !statuses.isEmpty()) {
            this.updateStatusEnum(statuses);
        }
    }

    public Map<String, Integer> fetchStatuses() {
        Map<String, Integer> statuses = null;
        try {
            List<Status> statusList = this.statusRepository.findAll();
            if (null != statusList && !statusList.isEmpty()) {
                statuses = new HashMap<String, Integer>(statusList.size());
                for (Status status : statusList) {
                    statuses.put(status.getName(), status.getId());
                }
            }
        } catch (Exception ex) {
            Logger.error("Error while fetching statuses information!", ex);
        }
        return statuses;
    }

    private void updateStatusEnum(Map<String, Integer> statuses) {
        Iterator<Map.Entry<String, Integer>> iterator = statuses.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> status = iterator.next();
            this.updateStatusEnumIds(statuses);
        }
    }

    private void updateStatusEnumIds(Map<String, Integer> statuses) {
        for (Status status : Status.values()) {
            Integer statusId = statuses.get(status.toString());
            if (null != statusId) {
                status.setId(statusId);
            }
        }
    }

}




No comments:

Post a Comment