BOM Expansion Example of JAVA Spring MVC Bill of Material

Posted by paulb on Thu, 22 Aug 2019 13:56:22 +0200

Related concepts: BOM expansion, LIST object, JSON string, JSON object
Recently, a management software is being developed, which involves the BOM management of bill of materials (also known as product structure), one of which is the development of BOM. BOM expansion generally has the form of vertical priority expansion, hierarchical priority expansion and aggregate expansion, and there are many expansion algorithms. Spring MVC framework is used in the development to complete the BOM program according to hierarchical priority. It also involves the related knowledge in JAVA development, such as LIST object, LIST object to JSON string, JSON string returns to the front end and restores to JSON object, and outputs the result content through JSON object in the front end.

BOM Entity Class
public class Bom {
Private Integer id; // ID number
private Integer parentid; //parent id
private String parentcode; // parent code
private String parentname; // parent name
private Integer subitemid; // subitem id
Private String subitem code; // subitem code
private String subitemname; // subitem name
private String unit; //Unit of Measurement
private Integer qty; //Quantity
private Integer level; // level number
private Date createDate; // Establishment Date
private String username; //Establish users
private String subitemflag; // Subitem identification

get and set methods
}

The parameter parentcode passed into the program is the item code of the BOM to be expanded
Three LIST object arrays are used in the program.
List listBom stores the final results of BOM expansion
List ListBomt01 Temporary LIST Object Array Used in BOM Expansion
List Bomt02 Temporary LIST Object Array Used in BOM Expansion

Since BOM is a tree-like loop expansion, at least two LIST object arrays are used, one for the current level node and the other for the next level node. Alternate loop unfolding, and the results of each level unfolding are sent to listBom. After the expansion is completed, the listBom is transferred to JSON string and returned to the front-end data display page.

The JAVA code is as follows

//Depending on the material number, BOM and back handler are developed layer by layer.
@ResponseBody
@RequestMapping(value="/ajaxbomexplosionlevel", method=RequestMethod.POST,produces = "text/html;charset=UTF-8")
public String ajaxbomexplosionbylevel(@RequestParam(value="parentcode",required=false) String parentcode,Map<String, Object> map) throws         JsonProcessingException {
    	List<Bom> listBom = BomService.BomExplosionbyLevel(parentcode);	             
       
        // LIST object to JSON string
        ObjectMapper mapper = new ObjectMapper();
        String jsonlist = mapper.writeValueAsString(listBom);
    	    	    	    
    	return  jsonlist;
      }

//BOM Expansion Function in Service
//Depending on the material number, BOM is developed layer by layer and Service is implemented.
@Transactional(readOnly=true)
public List<Bom> BomExplosionbyLevel(String parentcode){
	
	//listBom stores the results of expanding BOM layer by layer, and gets the first layer initially.		
	List<Bom> listBom =BomRepository.listBomByparentcode(parentcode);	 //Using the query function in Repository					 				
	
	//Level assignment at the first level
	Integer level = 1;
	for (int i = 0; i < listBom.size(); i++) {
		Bom bom = listBom.get(i);
		bom.setLevel(level);
	}
	
	//listBomt01 and listBomt02 alternately expand the temporary two list objects of BOM layer by layer and assign initial values.
	List<Bom> listBomt01 =BomRepository.listBomByparentcode(parentcode); //Using the query function in Repository
	List<Bom> listBomt02 =new ArrayList<Bom>();
	      
//Begin to expand BOM layer by layer alternately, each BOM node to be expanded is put into the result listBom, while the temporary object listBomt01 or listBomt02 is placed, waiting for the continuation of the loop to expand down, and the temporary object is initialized before it is put in.
	
	while ( listBomt01.size()>0     ) {
		
	level = level + 1;listBomt02.clear();
	//When the current layer is listBomt01, the results are output to listBom and listBomt02
	for (int i = 0; i < listBomt01.size(); i++) {
 	        Bom bom = listBomt01.get(i);
 	        String subcode = bom.getSubitemcode();
 	        Integer itemqty = bom.getQty();
 	        List<Bom> listBomt01sub =BomRepository.listBomByparentcode(subcode);
 	        
      for (int i1 = 0; i1 < listBomt01sub.size(); i1++) {  
 		Bom bom1 =  listBomt01sub.get(i1);
 		bom1.setLevel(level);
 		Integer qty = bom1.getQty();
 		bom1.setQty(qty * itemqty);
 		listBom.add( bom1);
     	listBomt02.add( bom1);
    	 }     	
 	         }		
    	
    	level = level + 1;listBomt01.clear();
   	//When the current layer is listBomt02, the results are output to listBom and listBomt01.
   	for (int i = 0; i < listBomt02.size(); i++) {
   	Bom bom = listBomt02.get(i);
    	String subcode = bom.getSubitemcode();
    	Integer itemqty = bom.getQty();
        List<Bom> listBomt02sub =BomRepository.listBomByparentcode(subcode);
      	
      for (int i2 = 0; i2 < listBomt02sub.size(); i2++) {  
      	Bom bom2 =  listBomt02sub.get(i2);
      	bom2.setLevel(level);
      	Integer qty = bom2.getQty();
      	bom2.setQty(qty * itemqty);
      	listBom.add( bom2);
        listBomt01.add( bom2);
         }
      	 }	
    	
           }  //When Ends
	//Returns the expanded result listBom
	return listBom;
}

JSP page output results

Topics: JSON Java Spring JSP