Maze
Maze

Reputation: 398

How to remove part of a hashmap value?

So I'm building a group of dropdowns that rely upon each other and built a query to get the code and description for a Product Type, Family, and Model object. I used nested hashmaps to story all of the data and objects. This was fine because I can just call all of the information that I need from the hashmaps. However, when it comes to the REST API's, it's going to display all of the nested information for each of the hashmaps when I call them. For each map I have it's key, and then the value consists of a Code, Desc, and the hashmap of the next object.

So, it would be like:

Main hashmap
 - Key
 - value
    -> code
    -> desc
    -> product family hashmap
       -- key
       -- value
          --> code
          --> desc
          --> product model hashmap
              --- key
              --- value
                  ---> code
                  ---> desc  

My main question is how can I either strip these additional hashmaps from being displayed in the json format when viewing the REST API via web browser? Or can/do I need to just completely strip the additional information altogether?

@Service
public class ProductDAOImpl implements ProductDAO {
    @PersistenceContext
    private EntityManager em;

    @Override
    public Map<String, ProductType> getProductTypeStructure() {
        HashMap<String, ProductType> prodTypes = new HashMap<>();
        Query q = em.createNativeQuery("<query>");


        List<Object[]> prodTypeEntities = q.getResultList();
        final String badData = "XX-BAD-XX";
        ProductType prodType = new ProductType(badData, "");
        ProductFamily prodFamily = new ProductFamily(badData, "");


        for(Object[] prodTypeEntity : prodTypeEntities) {


            if (prodTypeEntity[1] == null || prodTypeEntity[3] == null || prodTypeEntity[5] == null) {
                continue;
            }

            String prodTypeCd = prodTypeEntity[0].toString().toUpperCase();
            String prodTypeDesc = StringUtils.trimTrailingWhitespace(prodTypeEntity[1].toString()).toUpperCase(); 

            String prodFamilyCd = prodTypeEntity[2].toString().toUpperCase();
            String prodFamilyDesc = StringUtils.trimTrailingWhitespace(prodTypeEntity[3].toString()).toUpperCase(); 

            String prodModelCd = prodTypeEntity[4].toString().toUpperCase();
            String prodModelDesc = StringUtils.trimTrailingWhitespace(prodTypeEntity[5].toString()).toUpperCase(); 

            if(!prodType.getCode().equalsIgnoreCase(prodTypeCd)) {   
                prodType = new ProductType(prodTypeCd, prodTypeDesc); 
                prodType.setProdFamilies(new HashMap<String, ProductFamily>());
                prodTypes.put(prodType.getCode(), prodType);

                prodFamily.setCode(badData);
            }

            if(!prodFamily.getCode().equalsIgnoreCase(prodFamilyCd)) {
                prodFamily = new ProductFamily(prodFamilyCd, prodFamilyDesc);
                prodFamily.setProdModels(new HashMap<String, ProductModel>());
                prodType.getProdFamilies().put(prodFamily.getCode(), prodFamily);
            }
            prodFamily.getProdModels().put(prodModelCd, new ProductModel(prodModelCd, prodModelDesc));

        }    
        return prodTypes;
    }

}

Upvotes: 0

Views: 58

Answers (1)

tdranv
tdranv

Reputation: 1340

If I understood your question correctly, I think a DTO object might be the answer here. You add to it only the values that the dropdown might need and return it from the REST API.

Here's more on DTOs.

Upvotes: 1

Related Questions