attribute2dbField.put("itemCodeItm01ITM04","ITEM_CODE_ITM01");
attribute2dbField.put("progressiveITM04","PROGRESSIVE");
// check if there already exist the specified components in some group (in some PROGRESSIVE...)
String items = "";
AltComponentVO vo = null;
for(int i=0;i<list.size();i++) {
vo = (AltComponentVO) list.get(i);
items += "'"+vo.getItemCodeItm01ITM04()+"',";
}
if (items.length()>0)
items = items.substring(0,items.length()-1);
pstmt = conn.prepareStatement(
"select PROGRESSIVE,ITEM_CODE_ITM01 from ITM04_ALTERNATIVE_ITEMS where COMPANY_CODE_SYS01=? and ITEM_CODE_ITM01 in ("+items+")"
);
pstmt.setString(1,vo.getCompanyCodeSys01ITM04());
rset = pstmt.executeQuery();
ArrayList progressives = new ArrayList();
HashSet itms = new HashSet();
while(rset.next()) {
progressives.add(rset.getBigDecimal(1));
itms.add(rset.getString(2));
}
rset.close();
pstmt.close();
// if progressives size = 0 then add all components to the current item group
// if progressives size = 1 then add all components to the fetched group (fetched PROGRESSIVE)
// if progressives size > 1 then join all groups found and add all components to the unique group just created
BigDecimal progressiveITM04 = null;
Response res = null;
if (progressives.size()==0) {
vo = (AltComponentVO) list.get(0);
if (vo.getProgressiveITM04()==null) {
// maybe there exists ONE record for the current item...
pstmt = conn.prepareStatement(
"select PROGRESSIVE from ITM04_ALTERNATIVE_ITEMS where COMPANY_CODE_SYS01=? and ITEM_CODE_ITM01=?"
);
pstmt.setString(1,vo.getCompanyCodeSys01ITM04());
pstmt.setString(2,vo.getCurrentItemCodeItm01ITM04());
rset = pstmt.executeQuery();
if (rset.next())
progressiveITM04 = rset.getBigDecimal(1);
rset.close();
pstmt.close();
if (progressiveITM04==null) {
// this is the first alternative component defined for this item: create a new progressive...
progressiveITM04 = CompanyProgressiveUtils.getInternalProgressive(vo.getCompanyCodeSys01ITM04(),"ITM04_ALTERNATIVE_ITEMS", "PROGRESSIVE", conn);
// insert current item into ITM04...
AltComponentVO currVO = new AltComponentVO();
currVO.setCompanyCodeSys01ITM04(vo.getCompanyCodeSys01ITM04());
currVO.setCurrentItemCodeItm01ITM04(vo.getCurrentItemCodeItm01ITM04());
currVO.setItemCodeItm01ITM04(vo.getCurrentItemCodeItm01ITM04());
currVO.setProgressiveITM04(progressiveITM04);
res = org.jallinone.commons.server.QueryUtilExtension.insertTable(
conn,
new UserSessionParameters(username),
currVO,
"ITM04_ALTERNATIVE_ITEMS",
attribute2dbField,
"Y",
"N",
null,
true
);
if (res.isError()) {
throw new Exception(res.getErrorMessage());
}
}
}
else
progressiveITM04 = vo.getProgressiveITM04();
}
else if (progressives.size()==1) {
// add all components to the fetched group (fetched PROGRESSIVE)
progressiveITM04 = (BigDecimal)progressives.get(0);
// insert current item into ITM04...
AltComponentVO currVO = new AltComponentVO();
currVO.setCompanyCodeSys01ITM04(vo.getCompanyCodeSys01ITM04());
currVO.setCurrentItemCodeItm01ITM04(vo.getCurrentItemCodeItm01ITM04());
currVO.setItemCodeItm01ITM04(vo.getCurrentItemCodeItm01ITM04());
currVO.setProgressiveITM04(progressiveITM04);
res = org.jallinone.commons.server.QueryUtilExtension.insertTable(
conn,
new UserSessionParameters(username),
currVO,
"ITM04_ALTERNATIVE_ITEMS",
attribute2dbField,
"Y",
"N",
null,
true
);
if (res.isError()) {
throw new Exception(res.getErrorMessage());
}
}
else if (progressives.size()>1) {
// join all groups found and add all components to the unique group just created
String progrs = "";
for(int i=0;i<progressives.size();i++)
progrs += progressives.get(i)+",";
progrs = progrs.substring(0,progrs.length()-1);
pstmt = conn.prepareStatement(
"select ITEM_CODE_ITM01 from ITM04_ALTERNATIVE_ITEMS where COMPANY_CODE_SYS01=? and PROGRESSIVE in ("+progrs+")"
);
pstmt.setString(1,vo.getCompanyCodeSys01ITM04());
rset = pstmt.executeQuery();
itms = new HashSet();
while(rset.next())
if (!rset.getString(1).equals(vo.getCurrentItemCodeItm01ITM04()))
itms.add(rset.getString(1));
rset.close();
pstmt.close();
// delete all items of all retrieved groups...
pstmt = conn.prepareStatement(
"delete from ITM04_ALTERNATIVE_ITEMS where COMPANY_CODE_SYS01=? and PROGRESSIVE in ("+progrs+")"
);
pstmt.execute();
// insert all items in a single new group...
progressiveITM04 = CompanyProgressiveUtils.getInternalProgressive(vo.getCompanyCodeSys01ITM04(),"ITM04_ALTERNATIVE_ITEMS", "PROGRESSIVE", conn);
// insert current item into ITM04...
AltComponentVO currVO = new AltComponentVO();
currVO.setCompanyCodeSys01ITM04(vo.getCompanyCodeSys01ITM04());
currVO.setCurrentItemCodeItm01ITM04(vo.getCurrentItemCodeItm01ITM04());
currVO.setItemCodeItm01ITM04(vo.getCurrentItemCodeItm01ITM04());
currVO.setProgressiveITM04(progressiveITM04);
res = org.jallinone.commons.server.QueryUtilExtension.insertTable(
conn,
new UserSessionParameters(username),
currVO,
"ITM04_ALTERNATIVE_ITEMS",
attribute2dbField,
"Y",
"N",
null,
true
);
if (res.isError()) {
throw new Exception(res.getErrorMessage());
}
Iterator it = itms.iterator();
while(it.hasNext()) {
currVO = new AltComponentVO();
currVO.setCompanyCodeSys01ITM04(vo.getCompanyCodeSys01ITM04());
currVO.setCurrentItemCodeItm01ITM04(vo.getCurrentItemCodeItm01ITM04());
currVO.setItemCodeItm01ITM04(it.next().toString());
currVO.setProgressiveITM04(progressiveITM04);
res = org.jallinone.commons.server.QueryUtilExtension.insertTable(
conn,
new UserSessionParameters(username),
currVO,
"ITM04_ALTERNATIVE_ITEMS",