1 25 package org.ofbiz.manufacturing.techdata; 26 27 import java.sql.Time ; 28 import java.sql.Timestamp ; 29 import java.util.Calendar ; 30 import java.util.Date ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 import java.util.Map ; 36 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.UtilDateTime; 39 import org.ofbiz.base.util.UtilMisc; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericEntityException; 42 import org.ofbiz.entity.GenericValue; 43 import org.ofbiz.entity.condition.EntityExpr; 44 import org.ofbiz.entity.condition.EntityOperator; 45 import org.ofbiz.entity.util.EntityUtil; 46 import org.ofbiz.service.DispatchContext; 47 import org.ofbiz.service.ServiceUtil; 48 49 54 public class TechDataServices { 55 56 public static final String module = TechDataServices.class.getName(); 57 58 66 public static Map lookupRoutingTask(DispatchContext ctx, Map context) { 67 GenericDelegator delegator = ctx.getDelegator(); 68 GenericValue userLogin = (GenericValue) context.get("userLogin"); 69 70 Map result = new HashMap (); 71 72 String workEffortName = (String ) context.get("workEffortName"); 73 String fixedAssetId = (String ) context.get("fixedAssetId"); 74 75 List listRoutingTask = null; 76 List constraints = new LinkedList (); 77 78 if (workEffortName != null && workEffortName.length()>0) 79 constraints.add(new EntityExpr("workEffortName", EntityOperator.GREATER_THAN_EQUAL_TO, workEffortName)); 80 if (fixedAssetId != null && fixedAssetId.length()>0 && ! "ANY".equals(fixedAssetId)) 81 constraints.add(new EntityExpr("fixedAssetId", EntityOperator.EQUALS, fixedAssetId)); 82 83 constraints.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "ROU_ACTIVE")); 84 constraints.add(new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "ROU_TASK")); 85 86 try { 87 listRoutingTask = delegator.findByAnd("WorkEffort", constraints, UtilMisc.toList("workEffortName")); 88 } catch (GenericEntityException e) { 89 Debug.logWarning(e, module); 90 return ServiceUtil.returnError("Error finding desired WorkEffort records: " + e.toString()); 91 } 92 if (listRoutingTask == null) listRoutingTask = new LinkedList (); 93 if (listRoutingTask.size() == 0) listRoutingTask.add(UtilMisc.toMap("label","no Match","value","NO_MATCH")); 94 result.put("lookupResult", listRoutingTask); 95 return result; 96 } 97 105 public static Map checkRoutingTaskAssoc(DispatchContext ctx, Map context) { 106 GenericDelegator delegator = ctx.getDelegator(); 107 Map result = new HashMap (); 108 String sequenceNumNotOk = "N"; 109 110 String workEffortIdFrom = (String ) context.get("workEffortIdFrom"); 111 String workEffortIdTo = (String ) context.get("workEffortIdTo"); 112 String workEffortAssocTypeId = (String ) context.get("workEffortAssocTypeId"); 113 Long sequenceNum = (Long ) context.get("sequenceNum"); 114 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 115 Timestamp thruDate = (Timestamp ) context.get("thruDate"); 116 String create = (String ) context.get("create"); 117 118 boolean createProcess = (create !=null && create.equals("Y")) ? true : false; 119 List listRoutingTaskAssoc = null; 120 121 try { 122 listRoutingTaskAssoc = delegator.findByAnd("WorkEffortAssoc",UtilMisc.toMap("workEffortIdFrom", workEffortIdFrom,"sequenceNum",sequenceNum), UtilMisc.toList("fromDate")); 123 } catch (GenericEntityException e) { 124 Debug.logWarning(e, module); 125 return ServiceUtil.returnError("Error finding desired WorkEffortAssoc records: " + e.toString()); 126 } 127 128 if (listRoutingTaskAssoc != null) { 129 Iterator i = listRoutingTaskAssoc.iterator(); 130 while (i.hasNext()) { 131 GenericValue routingTaskAssoc = (GenericValue) i.next(); 132 if ( ! workEffortIdFrom.equals(routingTaskAssoc.getString("workEffortIdFrom")) || 133 ! workEffortIdTo.equals(routingTaskAssoc.getString("workEffortIdTo")) || 134 ! workEffortAssocTypeId.equals(routingTaskAssoc.getString("workEffortAssocTypeId")) || 135 ! sequenceNum.equals(routingTaskAssoc.getLong("sequenceNum")) 136 ) { 137 if (routingTaskAssoc.getTimestamp("thruDate") == null && routingTaskAssoc.getTimestamp("fromDate") == null) sequenceNumNotOk = "Y"; 138 else if (routingTaskAssoc.getTimestamp("thruDate") == null) { 139 if (thruDate == null) sequenceNumNotOk = "Y"; 140 else if (thruDate.after(routingTaskAssoc.getTimestamp("fromDate"))) sequenceNumNotOk = "Y"; 141 } 142 else if (routingTaskAssoc.getTimestamp("fromDate") == null) { 143 if (fromDate == null) sequenceNumNotOk = "Y"; 144 else if (fromDate.before(routingTaskAssoc.getTimestamp("thruDate"))) sequenceNumNotOk = "Y"; 145 } 146 else if ( fromDate == null && thruDate == null) sequenceNumNotOk = "Y"; 147 else if (thruDate == null) { 148 if (fromDate.before(routingTaskAssoc.getTimestamp("thruDate"))) sequenceNumNotOk = "Y"; 149 } 150 else if (fromDate == null) { 151 if (thruDate.after(routingTaskAssoc.getTimestamp("fromDate"))) sequenceNumNotOk = "Y"; 152 } 153 else if ( routingTaskAssoc.getTimestamp("fromDate").before(thruDate) && fromDate.before(routingTaskAssoc.getTimestamp("thruDate")) ) sequenceNumNotOk = "Y"; 154 } else if (createProcess) sequenceNumNotOk = "Y"; 155 } 156 } 157 result.put("sequenceNumNotOk", sequenceNumNotOk); 158 return result; 159 } 160 167 public static boolean routingTaskAssocIsValid(GenericValue routingTaskAssoc, Timestamp testDate) { 168 if (routingTaskAssoc.getTimestamp("fromDate") != null) 169 if (routingTaskAssoc.getTimestamp("thruDate") != null) 170 if (testDate.after(routingTaskAssoc.getTimestamp("fromDate")) && testDate.before(routingTaskAssoc.getTimestamp("thruDate"))) return true; 171 else return false; 172 else 173 if (testDate.after(routingTaskAssoc.getTimestamp("fromDate"))) return true; 174 else return false; 175 else 176 if (routingTaskAssoc.getTimestamp("thruDate") != null) 177 if (testDate.before(routingTaskAssoc.getTimestamp("thruDate"))) return true; 178 else return false; 179 else return true; 180 } 181 188 public static boolean productBomIsValid(GenericValue productBom, Timestamp testDate) { 189 if (productBom.getTimestamp("fromDate") != null) 190 if (productBom.getTimestamp("thruDate") != null) 191 if (testDate.after(productBom.getTimestamp("fromDate")) && testDate.before(productBom.getTimestamp("thruDate"))) return true; 192 else return false; 193 else 194 if (testDate.after(productBom.getTimestamp("fromDate"))) return true; 195 else return false; 196 else 197 if (productBom.getTimestamp("thruDate") != null) 198 if (testDate.before(productBom.getTimestamp("thruDate"))) return true; 199 else return false; 200 else return true; 201 } 202 209 public static GenericValue getTechDataCalendar(GenericValue routingTask) { 210 GenericValue machineGroup = null, techDataCalendar = null; 211 try{ 212 machineGroup = routingTask.getRelatedOneCache("FixedAsset"); 213 } catch (GenericEntityException e) { 214 Debug.logError("Pb reading FixedAsset associated with routingTask"+e.getMessage(), module); 215 } 216 if (machineGroup != null) { 217 if (machineGroup.getString("calendarId") != null){ 218 try{ 219 techDataCalendar = machineGroup.getRelatedOneCache("TechDataCalendar"); 220 } catch (GenericEntityException e) { 221 Debug.logError("Pb reading TechDataCalendar associated with machineGroup"+e.getMessage(), module); 222 } 223 }else { 224 try{ 225 List machines = machineGroup.getRelatedCache("ChildFixedAsset"); 226 if (machines != null && machines.size()>0) { 227 GenericValue machine = EntityUtil.getFirst(machines); 228 techDataCalendar = machine.getRelatedOneCache("TechDataCalendar"); 229 } 230 } catch (GenericEntityException e) { 231 Debug.logError("Pb reading machine child from machineGroup"+e.getMessage(), module); 232 } 233 } 234 } 235 if (techDataCalendar == null) { 236 try { 237 GenericDelegator delegator = routingTask.getDelegator(); 238 techDataCalendar = delegator.findByPrimaryKey("TechDataCalendar",UtilMisc.toMap("calendarId","DEFAULT")); 239 } catch (GenericEntityException e) { 240 Debug.logError("Pb reading TechDataCalendar DEFAULT"+e.getMessage(), module); 241 } 242 } 243 return techDataCalendar; 244 } 245 246 252 public static Map dayStartCapacityAvailable(GenericValue techDataCalendarWeek, int dayStart) { 253 Map result = new HashMap (); 254 int moveDay = 0; 255 Double capacity = null; 256 Time startTime = null; 257 while (capacity == null || capacity.doubleValue()==0) { 258 switch( dayStart){ 259 case Calendar.MONDAY: 260 capacity = techDataCalendarWeek.getDouble("mondayCapacity"); 261 startTime = techDataCalendarWeek.getTime("mondayStartTime"); 262 break; 263 case Calendar.TUESDAY: 264 capacity = techDataCalendarWeek.getDouble("tuesdayCapacity"); 265 startTime = techDataCalendarWeek.getTime("tuesdayStartTime"); 266 break; 267 case Calendar.WEDNESDAY: 268 capacity = techDataCalendarWeek.getDouble("wednesdayCapacity"); 269 startTime = techDataCalendarWeek.getTime("wednesdayStartTime"); 270 break; 271 case Calendar.THURSDAY: 272 capacity = techDataCalendarWeek.getDouble("thursdayCapacity"); 273 startTime = techDataCalendarWeek.getTime("thursdayStartTime"); 274 break; 275 case Calendar.FRIDAY: 276 capacity = techDataCalendarWeek.getDouble("fridayCapacity"); 277 startTime = techDataCalendarWeek.getTime("fridayStartTime"); 278 break; 279 case Calendar.SATURDAY: 280 capacity = techDataCalendarWeek.getDouble("saturdayCapacity"); 281 startTime = techDataCalendarWeek.getTime("saturdayStartTime"); 282 break; 283 case Calendar.SUNDAY: 284 capacity = techDataCalendarWeek.getDouble("sundayCapacity"); 285 startTime = techDataCalendarWeek.getTime("sundayStartTime"); 286 break; 287 } 288 if (capacity == null || capacity.doubleValue() == 0) { 289 moveDay +=1; 290 dayStart = (dayStart==7) ? 1 : dayStart +1; 291 } 292 } 294 result.put("capacity",capacity); 295 result.put("startTime",startTime); 296 result.put("moveDay",new Integer (moveDay)); 297 return result; 298 } 299 306 public static long capacityRemaining(GenericValue techDataCalendar, Timestamp dateFrom) { 307 GenericValue techDataCalendarWeek = null; 308 try{ 310 techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek"); 311 } catch (GenericEntityException e) { 312 Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module); 313 return 0; 315 } 316 Calendar cDateTrav = Calendar.getInstance(); 318 cDateTrav.setTime((Date ) dateFrom); 319 Map position = dayStartCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); 320 int moveDay = ((Integer ) position.get("moveDay")).intValue(); 321 if (moveDay != 0) return 0; 322 Time startTime = (Time ) position.get("startTime"); 323 Double capacity = (Double ) position.get("capacity"); 324 Timestamp startAvailablePeriod = new Timestamp (UtilDateTime.getDayStart(dateFrom).getTime() + startTime.getTime() + 3600000); 326 if (dateFrom.before(startAvailablePeriod) ) return 0; 327 Timestamp endAvailablePeriod = new Timestamp (startAvailablePeriod.getTime()+capacity.longValue()); 328 if (dateFrom.after(endAvailablePeriod)) return 0; 329 return endAvailablePeriod.getTime() - dateFrom.getTime(); 330 } 331 338 public static Map startNextDay(GenericValue techDataCalendar, Timestamp dateFrom) { 339 Map result = new HashMap (); 340 Timestamp dateTo = null; 341 GenericValue techDataCalendarWeek = null; 342 try{ 344 techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek"); 345 } catch (GenericEntityException e) { 346 Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module); 347 return ServiceUtil.returnError("Pb reading Calendar Week associated with calendar"); 349 } 350 Calendar cDateTrav = Calendar.getInstance(); 352 cDateTrav.setTime((Date ) dateFrom); 353 Map position = dayStartCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); 354 Time startTime = (Time ) position.get("startTime"); 355 int moveDay = ((Integer ) position.get("moveDay")).intValue(); 356 dateTo = (moveDay == 0) ? dateFrom : UtilDateTime.getDayStart(dateFrom,moveDay); 357 Timestamp startAvailablePeriod = new Timestamp (UtilDateTime.getDayStart(dateTo).getTime() + startTime.getTime() + 3600000); 359 if (dateTo.before(startAvailablePeriod) ) { 360 dateTo = startAvailablePeriod; 361 } 362 else { 363 dateTo = UtilDateTime.getNextDayStart(dateTo); 364 cDateTrav.setTime((Date ) dateTo); 365 position = dayStartCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); 366 startTime = (Time ) position.get("startTime"); 367 moveDay = ((Integer ) position.get("moveDay")).intValue(); 368 if (moveDay != 0) dateTo = UtilDateTime.getDayStart(dateTo,moveDay); 369 dateTo.setTime(dateTo.getTime() + startTime.getTime() + 3600000); 370 } 371 result.put("dateTo",dateTo); 372 result.put("nextCapacity",position.get("capacity")); 373 return result; 374 } 375 383 public static Timestamp addForward(GenericValue techDataCalendar, Timestamp dateFrom, long amount) { 384 Timestamp dateTo = (Timestamp ) dateFrom.clone(); 385 long nextCapacity = capacityRemaining(techDataCalendar, dateFrom); 386 if (amount <= nextCapacity){ 387 dateTo.setTime(dateTo.getTime()+amount); 388 amount = 0; 389 }else amount -= nextCapacity; 390 391 Map result = new HashMap (); 392 while (amount > 0) { 393 result = startNextDay(techDataCalendar, dateTo); 394 dateTo = (Timestamp ) result.get("dateTo"); 395 nextCapacity = ((Double ) result.get("nextCapacity")).longValue(); 396 if (amount <= nextCapacity){ 397 dateTo.setTime(dateTo.getTime()+amount); 398 amount = 0; 399 }else amount -= nextCapacity; 400 } 401 return dateTo; 402 } 403 404 411 public static Map dayEndCapacityAvailable(GenericValue techDataCalendarWeek, int dayEnd) { 412 Map result = new HashMap (); 413 int moveDay = 0; 414 Double capacity = null; 415 Time startTime = null; 416 while (capacity == null || capacity.doubleValue() == 0) { 417 switch( dayEnd){ 418 case Calendar.MONDAY: 419 capacity = techDataCalendarWeek.getDouble("mondayCapacity"); 420 startTime = techDataCalendarWeek.getTime("mondayStartTime"); 421 break; 422 case Calendar.TUESDAY: 423 capacity = techDataCalendarWeek.getDouble("tuesdayCapacity"); 424 startTime = techDataCalendarWeek.getTime("tuesdayStartTime"); 425 break; 426 case Calendar.WEDNESDAY: 427 capacity = techDataCalendarWeek.getDouble("wednesdayCapacity"); 428 startTime = techDataCalendarWeek.getTime("wednesdayStartTime"); 429 break; 430 case Calendar.THURSDAY: 431 capacity = techDataCalendarWeek.getDouble("thursdayCapacity"); 432 startTime = techDataCalendarWeek.getTime("thursdayStartTime"); 433 break; 434 case Calendar.FRIDAY: 435 capacity = techDataCalendarWeek.getDouble("fridayCapacity"); 436 startTime = techDataCalendarWeek.getTime("fridayStartTime"); 437 break; 438 case Calendar.SATURDAY: 439 capacity = techDataCalendarWeek.getDouble("saturdayCapacity"); 440 startTime = techDataCalendarWeek.getTime("saturdayStartTime"); 441 break; 442 case Calendar.SUNDAY: 443 capacity = techDataCalendarWeek.getDouble("sundayCapacity"); 444 startTime = techDataCalendarWeek.getTime("sundayStartTime"); 445 break; 446 } 447 if (capacity == null || capacity.doubleValue() == 0) { 448 moveDay -=1; 449 dayEnd = (dayEnd==1) ? 7 : dayEnd - 1; 450 } 451 } 453 result.put("capacity",capacity); 454 result.put("startTime",startTime); 455 result.put("moveDay",new Integer (moveDay)); 456 return result; 457 } 458 465 public static long capacityRemainingBackward(GenericValue techDataCalendar, Timestamp dateFrom) { 466 GenericValue techDataCalendarWeek = null; 467 try{ 469 techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek"); 470 } catch (GenericEntityException e) { 471 Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module); 472 return 0; 474 } 475 Calendar cDateTrav = Calendar.getInstance(); 477 cDateTrav.setTime((Date ) dateFrom); 478 Map position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); 479 int moveDay = ((Integer ) position.get("moveDay")).intValue(); 480 if (moveDay != 0) return 0; 481 Time startTime = (Time ) position.get("startTime"); 482 Double capacity = (Double ) position.get("capacity"); 483 Timestamp startAvailablePeriod = new Timestamp (UtilDateTime.getDayStart(dateFrom).getTime() + startTime.getTime() + 3600000); 485 if (dateFrom.before(startAvailablePeriod) ) return 0; 486 Timestamp endAvailablePeriod = new Timestamp (startAvailablePeriod.getTime()+capacity.longValue()); 487 if (dateFrom.after(endAvailablePeriod)) return 0; 488 return dateFrom.getTime() - startAvailablePeriod.getTime(); 489 } 490 497 public static Map endPreviousDay(GenericValue techDataCalendar, Timestamp dateFrom) { 498 Map result = new HashMap (); 499 Timestamp dateTo = null; 500 GenericValue techDataCalendarWeek = null; 501 try{ 503 techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek"); 504 } catch (GenericEntityException e) { 505 Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module); 506 return ServiceUtil.returnError("Pb reading Calendar Week associated with calendar"); 508 } 509 Calendar cDateTrav = Calendar.getInstance(); 511 cDateTrav.setTime((Date ) dateFrom); 512 Map position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); 513 Time startTime = (Time ) position.get("startTime"); 514 int moveDay = ((Integer ) position.get("moveDay")).intValue(); 515 Double capacity = (Double ) position.get("capacity"); 516 dateTo = (moveDay == 0) ? dateFrom : UtilDateTime.getDayEnd(dateFrom,moveDay); 517 Timestamp endAvailablePeriod = new Timestamp (UtilDateTime.getDayStart(dateTo).getTime() + startTime.getTime() + 3600000 + capacity.longValue()); 519 if (dateTo.after(endAvailablePeriod) ) { 520 dateTo = endAvailablePeriod; 521 } 522 else { 523 dateTo = UtilDateTime.getDayStart(dateTo, -1); 524 cDateTrav.setTime((Date ) dateTo); 525 position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); 526 startTime = (Time ) position.get("startTime"); 527 moveDay = ((Integer ) position.get("moveDay")).intValue(); 528 capacity = (Double ) position.get("capacity"); 529 if (moveDay != 0) dateTo = UtilDateTime.getDayStart(dateTo,moveDay); 530 dateTo.setTime(dateTo.getTime() + startTime.getTime() + 3600000 + capacity.longValue()); 531 } 532 result.put("dateTo",dateTo); 533 result.put("previousCapacity",position.get("capacity")); 534 return result; 535 } 536 544 public static Timestamp addBackward(GenericValue techDataCalendar, Timestamp dateFrom, long amount) { 545 Timestamp dateTo = (Timestamp ) dateFrom.clone(); 546 long previousCapacity = capacityRemainingBackward(techDataCalendar, dateFrom); 547 if (amount <= previousCapacity){ 548 dateTo.setTime(dateTo.getTime()-amount); 549 amount = 0; 550 }else amount -= previousCapacity; 551 552 Map result = new HashMap (); 553 while (amount > 0) { 554 result = endPreviousDay(techDataCalendar, dateTo); 555 dateTo = (Timestamp ) result.get("dateTo"); 556 previousCapacity = ((Double ) result.get("previousCapacity")).longValue(); 557 if (amount <= previousCapacity){ 558 dateTo.setTime(dateTo.getTime()-amount); 559 amount = 0; 560 }else amount -= previousCapacity; 561 } 562 return dateTo; 563 } 564 } 565 | Popular Tags |