in ebaystore/src/main/java/org/apache/ofbiz/ebaystore/EbayStoreAutoPreferences.java [400:526]
public static Map<String, Object> automaticEbayRelistSoldItems(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
String jobId = (String) context.get("jobId");
try {
GenericValue userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", "system").queryOne();
Map<String, Object> serviceMap = new HashMap<>();
serviceMap.put("userLogin", userLogin);
//ProductStore
List<GenericValue> productStores = EntityQuery.use(delegator).from("EbayProductStorePref").where("autoPrefJobId", jobId).queryList();
if (!productStores.isEmpty()) {
// get auto preference setting
String productStoreId = productStores.get(0).getString("productStoreId");
String condition1 = productStores.get(0).getString("condition1");
String condition2 = productStores.get(0).getString("condition2");
// convert preference setting
Timestamp fromDate = UtilDateTime.toTimestamp(condition1);
Timestamp thruDate = UtilDateTime.toTimestamp(condition2);
Timestamp nowTime = UtilDateTime.nowTimestamp();
if (nowTime.after(fromDate) && nowTime.before(thruDate)) {
serviceMap.put("productStoreId", productStoreId);
Map<String, Object> eBayUserLogin = dispatcher.runSync("getEbayStoreUser", serviceMap);
if (ServiceUtil.isError(eBayUserLogin)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(eBayUserLogin));
}
String eBayUserLoginId = (String) eBayUserLogin.get("userLoginId");
GenericValue party = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", eBayUserLoginId).queryOne();
String partyId = party.getString("partyId");
//save sold items to OFbBiz product entity
Map<String, Object> serviceResult = dispatcher.runSync("getEbaySoldItems", serviceMap);
if (ServiceUtil.isError(resultService)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(resultService));
}
List<Map<String, Object>> soldItems = UtilGenerics.checkList(serviceResult.get("soldItems"));
if (!soldItems.isEmpty()) {
for (Map<String, Object> soldItemMap : soldItems) {
if (UtilValidate.isNotEmpty(soldItemMap.get("itemId"))) {
GenericValue productCheck = EntityQuery.use(delegator).from("Product").where("productId", soldItemMap.get("itemId")).queryOne();
if (productCheck == null) {
Map<String, Object> inMap = new HashMap<>();
inMap.put("productId", soldItemMap.get("itemId"));
inMap.put("productTypeId", "EBAY_ITEM");
inMap.put("internalName", "eBay Item " + soldItemMap.get("title"));
inMap.put("userLogin", userLogin);
Map<String, Object> result = dispatcher.runSync("createProduct", inMap);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
// ProductRole (VENDOR)
List<GenericValue> productRole = EntityQuery.use(delegator).from("ProductRole").where("partyId", partyId, "productId", soldItemMap.get("itemId"), "roleTypeId", "VENDOR").queryList();
if (productRole.isEmpty()) {
Map<String, Object> addRole = new HashMap<>();
addRole.put("productId", soldItemMap.get("itemId"));
addRole.put("roleTypeId", "VENDOR");
addRole.put("partyId", partyId);
addRole.put("fromDate", UtilDateTime.nowTimestamp());
addRole.put("userLogin", userLogin);
Map<String, Object> result = dispatcher.runSync("addPartyToProduct", addRole);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
}
}
}
}
//check active items
serviceMap = new HashMap<>();
serviceMap.put("userLogin", userLogin);
serviceMap.put("productStoreId", productStoreId);
serviceResult = dispatcher.runSync("getEbayActiveItems", serviceMap);
if (ServiceUtil.isError(resultService)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(resultService));
}
List<Map<String, Object>> activeItems = UtilGenerics.checkList(serviceResult.get("activeItems"));
List<String> activeItemMaps = new LinkedList<>();
if (!activeItems.isEmpty()) {
for (Map<String, Object> activeItem : activeItems) {
Map<String, Object> activeItemMap = UtilGenerics.checkMap(activeItem);
if (UtilValidate.isNotEmpty(activeItemMap.get("itemId"))) {
activeItemMaps.add((String) activeItemMap.get("itemId"));
}
}
}
//check product role
List<GenericValue> productRoles = EntityQuery.use(delegator).from("ProductRole").where("partyId", partyId, "roleTypeId", "VENDOR").queryList();
List<String> productRoleIds = new LinkedList<>();
if (!productRoles.isEmpty()) {
for (GenericValue productRole : productRoles) {
String productId = productRole.getString("productId");
productRoleIds.add(productId);
}
}
List<EntityCondition> andExpr = new LinkedList<>();
EntityCondition activeItemCond = EntityCondition.makeCondition("productId", EntityOperator.NOT_IN, activeItemMaps);
andExpr.add(activeItemCond);
EntityCondition productTypeCond = EntityCondition.makeCondition("productTypeId", EntityOperator.EQUALS, "EBAY_ITEM");
andExpr.add(productTypeCond);
EntityCondition isVirtualCond = EntityCondition.makeCondition("isVirtual", EntityOperator.NOT_EQUAL, "Y");
andExpr.add(isVirtualCond);
EntityCondition productRole = EntityCondition.makeCondition("productId", EntityOperator.IN, productRoleIds);
andExpr.add(productRole);
List<GenericValue> itemsToRelist = EntityQuery.use(delegator).from("Product").where(andExpr).queryList();
if (!itemsToRelist.isEmpty()) {
//re-list sold items and not active
ApiContext apiContext = EbayStoreHelper.getApiContext(productStoreId, locale, delegator);
for (GenericValue genericValue : itemsToRelist) {
RelistItemCall relistItemCall = new RelistItemCall(apiContext);
ItemType itemToBeRelisted = new ItemType();
GenericValue product = genericValue;
itemToBeRelisted.setItemID(product.getString("productId"));
relistItemCall.setItemToBeRelisted(itemToBeRelisted);
relistItemCall.relistItem();
GenericValue productStore = EntityQuery.use(delegator).from("Product").where("productId", product.getString("productId")).queryOne();
productStore.set("isVirtual", "Y");
productStore.store();
Debug.logInfo("Relisted Item - " + product.getString("productId"), MODULE);
}
}
}
}
} catch (GenericEntityException | GenericServiceException ge) {
return ServiceUtil.returnError(ge.getMessage());
}
return ServiceUtil.returnSuccess();
}