PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/specialpurpose/ebaystore/src/org/ofbiz/ebaystore/EbayStore.java

https://bitbucket.org/sudhirk76/ogb
Java | 2194 lines | 1991 code | 149 blank | 54 comment | 390 complexity | 2dbd3ec86132ca8f060411cfab90261c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.0, CPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.ofbiz.ebaystore;
  20. import java.io.BufferedReader;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.io.OutputStream;
  25. import java.math.BigDecimal;
  26. import java.net.HttpURLConnection;
  27. import java.net.URL;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.Iterator;
  31. import java.util.List;
  32. import java.util.Locale;
  33. import java.util.Map;
  34. import javolution.util.FastList;
  35. import javolution.util.FastMap;
  36. import org.ofbiz.base.util.Debug;
  37. import org.ofbiz.base.util.StringUtil;
  38. import org.ofbiz.base.util.UtilDateTime;
  39. import org.ofbiz.base.util.UtilMisc;
  40. import org.ofbiz.base.util.UtilProperties;
  41. import org.ofbiz.base.util.UtilValidate;
  42. import org.ofbiz.base.util.UtilXml;
  43. import org.ofbiz.common.DataModelConstants;
  44. import org.ofbiz.entity.Delegator;
  45. import org.ofbiz.entity.GenericEntityException;
  46. import org.ofbiz.entity.GenericValue;
  47. import org.ofbiz.entity.condition.EntityCondition;
  48. import org.ofbiz.entity.condition.EntityOperator;
  49. import org.ofbiz.entity.transaction.TransactionUtil;
  50. import org.ofbiz.entity.util.EntityUtil;
  51. import org.ofbiz.service.DispatchContext;
  52. import org.ofbiz.service.GenericServiceException;
  53. import org.ofbiz.service.LocalDispatcher;
  54. import org.ofbiz.service.ModelService;
  55. import org.ofbiz.service.ServiceUtil;
  56. import org.w3c.dom.Document;
  57. import org.w3c.dom.Element;
  58. import com.ebay.sdk.ApiContext;
  59. import com.ebay.sdk.ApiException;
  60. import com.ebay.sdk.SdkException;
  61. import com.ebay.sdk.SdkSoapException;
  62. import com.ebay.sdk.call.*;
  63. import com.ebay.sdk.util.eBayUtil;
  64. import com.ebay.soap.eBLBaseComponents.AmountType;
  65. import com.ebay.soap.eBLBaseComponents.CheckoutStatusCodeType;
  66. import com.ebay.soap.eBLBaseComponents.CurrencyCodeType;
  67. import com.ebay.soap.eBLBaseComponents.DeleteSellingManagerTemplateRequestType;
  68. import com.ebay.soap.eBLBaseComponents.DeleteSellingManagerTemplateResponseType;
  69. import com.ebay.soap.eBLBaseComponents.DisputeExplanationCodeType;
  70. import com.ebay.soap.eBLBaseComponents.DisputeReasonCodeType;
  71. import com.ebay.soap.eBLBaseComponents.GetAllBiddersModeCodeType;
  72. import com.ebay.soap.eBLBaseComponents.GetSellingManagerInventoryRequestType;
  73. import com.ebay.soap.eBLBaseComponents.GetSellingManagerInventoryResponseType;
  74. import com.ebay.soap.eBLBaseComponents.GetStoreOptionsRequestType;
  75. import com.ebay.soap.eBLBaseComponents.GetStoreOptionsResponseType;
  76. import com.ebay.soap.eBLBaseComponents.GetStoreRequestType;
  77. import com.ebay.soap.eBLBaseComponents.GetStoreResponseType;
  78. import com.ebay.soap.eBLBaseComponents.ItemSortTypeCodeType;
  79. import com.ebay.soap.eBLBaseComponents.ListingTypeCodeType;
  80. import com.ebay.soap.eBLBaseComponents.MerchDisplayCodeType;
  81. import com.ebay.soap.eBLBaseComponents.OfferType;
  82. import com.ebay.soap.eBLBaseComponents.OrderTransactionArrayType;
  83. import com.ebay.soap.eBLBaseComponents.OrderTransactionType;
  84. import com.ebay.soap.eBLBaseComponents.OrderType;
  85. import com.ebay.soap.eBLBaseComponents.PaginationType;
  86. import com.ebay.soap.eBLBaseComponents.SecondChanceOfferDurationCodeType;
  87. import com.ebay.soap.eBLBaseComponents.SellingManagerProductDetailsType;
  88. import com.ebay.soap.eBLBaseComponents.SellingManagerProductInventoryStatusType;
  89. import com.ebay.soap.eBLBaseComponents.SellingManagerProductType;
  90. import com.ebay.soap.eBLBaseComponents.SellingManagerSearchType;
  91. import com.ebay.soap.eBLBaseComponents.SellingManagerSearchTypeCodeType;
  92. import com.ebay.soap.eBLBaseComponents.SellingManagerSoldListingsPropertyTypeCodeType;
  93. import com.ebay.soap.eBLBaseComponents.SellingManagerTemplateDetailsArrayType;
  94. import com.ebay.soap.eBLBaseComponents.SellingManagerTemplateDetailsType;
  95. import com.ebay.soap.eBLBaseComponents.SellingStatusType;
  96. import com.ebay.soap.eBLBaseComponents.SetStoreCategoriesRequestType;
  97. import com.ebay.soap.eBLBaseComponents.SetStoreCategoriesResponseType;
  98. import com.ebay.soap.eBLBaseComponents.SetStoreRequestType;
  99. import com.ebay.soap.eBLBaseComponents.SetStoreResponseType;
  100. import com.ebay.soap.eBLBaseComponents.StoreCategoryUpdateActionCodeType;
  101. import com.ebay.soap.eBLBaseComponents.StoreColorSchemeType;
  102. import com.ebay.soap.eBLBaseComponents.StoreColorType;
  103. import com.ebay.soap.eBLBaseComponents.StoreCustomCategoryArrayType;
  104. import com.ebay.soap.eBLBaseComponents.StoreCustomCategoryType;
  105. import com.ebay.soap.eBLBaseComponents.StoreCustomHeaderLayoutCodeType;
  106. import com.ebay.soap.eBLBaseComponents.StoreCustomListingHeaderDisplayCodeType;
  107. import com.ebay.soap.eBLBaseComponents.StoreCustomListingHeaderType;
  108. import com.ebay.soap.eBLBaseComponents.StoreFontFaceCodeType;
  109. import com.ebay.soap.eBLBaseComponents.StoreFontSizeCodeType;
  110. import com.ebay.soap.eBLBaseComponents.StoreFontType;
  111. import com.ebay.soap.eBLBaseComponents.StoreHeaderStyleCodeType;
  112. import com.ebay.soap.eBLBaseComponents.StoreItemListLayoutCodeType;
  113. import com.ebay.soap.eBLBaseComponents.StoreItemListSortOrderCodeType;
  114. import com.ebay.soap.eBLBaseComponents.StoreLogoArrayType;
  115. import com.ebay.soap.eBLBaseComponents.StoreLogoType;
  116. import com.ebay.soap.eBLBaseComponents.StoreSubscriptionLevelCodeType;
  117. import com.ebay.soap.eBLBaseComponents.StoreThemeArrayType;
  118. import com.ebay.soap.eBLBaseComponents.StoreThemeType;
  119. import com.ebay.soap.eBLBaseComponents.StoreType;
  120. import com.ebay.soap.eBLBaseComponents.TaskStatusCodeType;
  121. import com.ebay.soap.eBLBaseComponents.TransactionArrayType;
  122. import com.ebay.soap.eBLBaseComponents.TransactionType;
  123. import com.ebay.soap.eBLBaseComponents.UserType;
  124. import com.ebay.soap.eBLBaseComponents.VerifyAddSecondChanceItemResponseType;
  125. import java.sql.Timestamp;
  126. import javax.swing.event.TableModelListener;
  127. import javax.swing.table.AbstractTableModel;
  128. import javax.swing.table.TableModel;
  129. import com.ebay.soap.eBLBaseComponents.DetailLevelCodeType;
  130. import com.ebay.soap.eBLBaseComponents.ItemArrayType;
  131. import com.ebay.soap.eBLBaseComponents.ItemListCustomizationType;
  132. import com.ebay.soap.eBLBaseComponents.ItemType;
  133. import com.ebay.soap.eBLBaseComponents.PaginatedItemArrayType;
  134. import com.ebay.soap.eBLBaseComponents.SellingManagerSoldOrderType;
  135. import com.ebay.soap.eBLBaseComponents.SellingManagerSoldTransactionType;
  136. import org.ofbiz.ebay.ProductsExportToEbay;
  137. import org.ofbiz.ebay.EbayHelper;
  138. public class EbayStore {
  139. private static final String resource = "EbayStoreUiLabels";
  140. private static final String module = ProductsExportToEbay.class.getName();
  141. public static ProductsExportToEbay productExportEbay = new ProductsExportToEbay();
  142. private static void appendRequesterCredentials(Element elem, Document doc, String token) {
  143. Element requesterCredentialsElem = UtilXml.addChildElement(elem, "RequesterCredentials", doc);
  144. UtilXml.addChildElementValue(requesterCredentialsElem, "eBayAuthToken", token, doc);
  145. }
  146. private static Map postItem(String postItemsUrl, StringBuffer dataItems, String devID, String appID, String certID,
  147. String callName, String compatibilityLevel, String siteID) throws IOException {
  148. if (Debug.verboseOn()) {
  149. Debug.logVerbose("Request of " + callName + " To eBay:\n" + dataItems.toString(), module);
  150. }
  151. HttpURLConnection connection = (HttpURLConnection)(new URL(postItemsUrl)).openConnection();
  152. connection.setDoInput(true);
  153. connection.setDoOutput(true);
  154. connection.setRequestMethod("POST");
  155. connection.setRequestProperty("X-EBAY-API-COMPATIBILITY-LEVEL", compatibilityLevel);
  156. connection.setRequestProperty("X-EBAY-API-DEV-NAME", devID);
  157. connection.setRequestProperty("X-EBAY-API-APP-NAME", appID);
  158. connection.setRequestProperty("X-EBAY-API-CERT-NAME", certID);
  159. connection.setRequestProperty("X-EBAY-API-CALL-NAME", callName);
  160. connection.setRequestProperty("X-EBAY-API-SITEID", siteID);
  161. connection.setRequestProperty("Content-Type", "text/xml");
  162. OutputStream outputStream = connection.getOutputStream();
  163. outputStream.write(dataItems.toString().getBytes());
  164. outputStream.close();
  165. int responseCode = connection.getResponseCode();
  166. InputStream inputStream;
  167. Map result = FastMap.newInstance();
  168. String response = null;
  169. if (responseCode == HttpURLConnection.HTTP_CREATED ||
  170. responseCode == HttpURLConnection.HTTP_OK) {
  171. inputStream = connection.getInputStream();
  172. response = toString(inputStream);
  173. result = ServiceUtil.returnSuccess(response);
  174. } else {
  175. inputStream = connection.getErrorStream();
  176. response = toString(inputStream);
  177. result = ServiceUtil.returnFailure(response);
  178. }
  179. if (Debug.verboseOn()) {
  180. Debug.logVerbose("Response of " + callName + " From eBay:\n" + response, module);
  181. }
  182. return result;
  183. }
  184. private static String toString(InputStream inputStream) throws IOException {
  185. String string;
  186. StringBuilder outputBuilder = new StringBuilder();
  187. if (inputStream != null) {
  188. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  189. while (null != (string = reader.readLine())) {
  190. outputBuilder.append(string).append('\n');
  191. }
  192. }
  193. return outputBuilder.toString();
  194. }
  195. /* add/update/delete categories and child into your ebay store category */
  196. public static Map<String,Object> exportCategoriesSelectedToEbayStore(DispatchContext dctx, Map<String,? extends Object> context) {
  197. Locale locale = (Locale) context.get("locale");
  198. Delegator delegator = dctx.getDelegator();
  199. Map<String, Object> result = FastMap.newInstance();
  200. SetStoreCategoriesRequestType req = null;
  201. StoreCustomCategoryArrayType categoryArrayType = null;
  202. List<GenericValue> catalogCategories = null;
  203. if (UtilValidate.isEmpty(context.get("prodCatalogId")) || UtilValidate.isEmpty(context.get("productStoreId")) || UtilValidate.isEmpty(context.get("partyId"))) {
  204. return ServiceUtil.returnError("Please set catalogId and productStoreId.");
  205. }
  206. if (!EbayStoreHelper.validatePartyAndRoleType(delegator,context.get("partyId").toString())) {
  207. return ServiceUtil.returnError("Party ".concat(context.get("partyId").toString()).concat(" no roleTypeId EBAY_ACCOUNT for export categories to ebay store."));
  208. }
  209. try {
  210. SetStoreCategoriesCall call = new SetStoreCategoriesCall(EbayStoreHelper.getApiContext((String)context.get("productStoreId"), locale, delegator));
  211. catalogCategories = delegator.findByAnd("ProdCatalogCategory", UtilMisc.toMap("prodCatalogId", context.get("prodCatalogId").toString(),"prodCatalogCategoryTypeId","PCCT_EBAY_ROOT"), UtilMisc.toList("sequenceNum ASC"));
  212. if (catalogCategories != null && catalogCategories.size() > 0) {
  213. List<StoreCustomCategoryType> listAdd = FastList.newInstance();
  214. List<StoreCustomCategoryType> listEdit = FastList.newInstance();
  215. //start at level 0 of categories
  216. for (GenericValue catalogCategory : catalogCategories) {
  217. GenericValue productCategory = catalogCategory.getRelatedOne("ProductCategory");
  218. if (productCategory != null) {
  219. String ebayCategoryId = EbayStoreHelper.retriveEbayCategoryIdByPartyId(delegator,productCategory.getString("productCategoryId"),context.get("partyId").toString());
  220. StoreCustomCategoryType categoryType = new StoreCustomCategoryType();
  221. if (ebayCategoryId == null) {
  222. categoryType.setName(productCategory.getString("categoryName"));
  223. listAdd.add(categoryType);
  224. } else {
  225. categoryType.setCategoryID(new Long(ebayCategoryId));
  226. categoryType.setName(productCategory.getString("categoryName"));
  227. listEdit.add(categoryType);
  228. }
  229. }
  230. }
  231. if (listAdd.size() > 0) {
  232. req = new SetStoreCategoriesRequestType();
  233. categoryArrayType = new StoreCustomCategoryArrayType();
  234. categoryArrayType.setCustomCategory(toStoreCustomCategoryTypeArray(listAdd));
  235. req.setStoreCategories(categoryArrayType);
  236. result = excuteExportCategoryToEbayStore(call, req, StoreCategoryUpdateActionCodeType.ADD, delegator,context.get("partyId").toString(), catalogCategories);
  237. }
  238. if (listEdit.size() > 0) {
  239. req = new SetStoreCategoriesRequestType();
  240. categoryArrayType = new StoreCustomCategoryArrayType();
  241. categoryArrayType.setCustomCategory(toStoreCustomCategoryTypeArray(listEdit));
  242. req.setStoreCategories(categoryArrayType);
  243. result = excuteExportCategoryToEbayStore(call, req, StoreCategoryUpdateActionCodeType.RENAME, delegator,context.get("partyId").toString(), catalogCategories);
  244. }
  245. //start at level 1 of categories
  246. listAdd = FastList.newInstance();
  247. listEdit = FastList.newInstance();
  248. for (GenericValue catalogCategory : catalogCategories) {
  249. GenericValue productCategory = catalogCategory.getRelatedOne("ProductCategory");
  250. if (productCategory != null) {
  251. String ebayParentCategoryId = EbayStoreHelper.retriveEbayCategoryIdByPartyId(delegator, productCategory.getString("productCategoryId"), context.get("partyId").toString());
  252. if (ebayParentCategoryId != null) {
  253. List<GenericValue> productCategoryRollupList = delegator.findByAnd("ProductCategoryRollup", UtilMisc.toMap("parentProductCategoryId", productCategory.getString("productCategoryId")), UtilMisc.toList("sequenceNum ASC"));
  254. for (GenericValue productCategoryRollup : productCategoryRollupList) {
  255. productCategory = delegator.findByPrimaryKey("ProductCategory", UtilMisc.toMap("productCategoryId", productCategoryRollup.getString("productCategoryId")));
  256. StoreCustomCategoryType childCategoryType = new StoreCustomCategoryType();
  257. String ebayChildCategoryId = EbayStoreHelper.retriveEbayCategoryIdByPartyId(delegator, productCategory.getString("productCategoryId"), context.get("partyId").toString());
  258. if (ebayChildCategoryId == null) {
  259. childCategoryType.setName(productCategory.getString("categoryName"));
  260. listAdd.add(childCategoryType);
  261. } else {
  262. childCategoryType.setCategoryID(new Long(ebayChildCategoryId));
  263. childCategoryType.setName(productCategory.getString("categoryName"));
  264. listEdit.add(childCategoryType);
  265. }
  266. }
  267. }
  268. if (listAdd.size() > 0) {
  269. req = new SetStoreCategoriesRequestType();
  270. categoryArrayType = new StoreCustomCategoryArrayType();
  271. categoryArrayType.setCustomCategory(toStoreCustomCategoryTypeArray(listAdd));
  272. req.setStoreCategories(categoryArrayType);
  273. req.setDestinationParentCategoryID(new Long(ebayParentCategoryId));
  274. result = excuteExportCategoryToEbayStore(call, req, StoreCategoryUpdateActionCodeType.ADD, delegator,context.get("partyId").toString(), catalogCategories);
  275. }
  276. if (listEdit.size() > 0) {
  277. req = new SetStoreCategoriesRequestType();
  278. categoryArrayType = new StoreCustomCategoryArrayType();
  279. categoryArrayType.setCustomCategory(toStoreCustomCategoryTypeArray(listEdit));
  280. req.setStoreCategories(categoryArrayType);
  281. req.setDestinationParentCategoryID(new Long(ebayParentCategoryId));
  282. result = excuteExportCategoryToEbayStore(call, req, StoreCategoryUpdateActionCodeType.RENAME, delegator,context.get("partyId").toString(), catalogCategories);
  283. }
  284. }
  285. }
  286. //start at level 2 of categories
  287. listAdd = FastList.newInstance();
  288. listEdit = FastList.newInstance();
  289. for (GenericValue catalogCategory : catalogCategories) {
  290. GenericValue productCategory = catalogCategory.getRelatedOne("ProductCategory");
  291. if (productCategory != null) {
  292. List<GenericValue> productParentCategoryRollupList = delegator.findByAnd("ProductCategoryRollup", UtilMisc.toMap("parentProductCategoryId",productCategory.getString("productCategoryId")),UtilMisc.toList("sequenceNum ASC"));
  293. for (GenericValue productParentCategoryRollup : productParentCategoryRollupList) {
  294. String ebayParentCategoryId = EbayStoreHelper.retriveEbayCategoryIdByPartyId(delegator,productParentCategoryRollup.getString("productCategoryId"),context.get("partyId").toString());
  295. if (ebayParentCategoryId != null) {
  296. List<GenericValue> productChildCategoryRollupList = delegator.findByAnd("ProductCategoryRollup", UtilMisc.toMap("parentProductCategoryId",productParentCategoryRollup.getString("productCategoryId")),UtilMisc.toList("sequenceNum ASC"));
  297. for (GenericValue productChildCategoryRollup : productChildCategoryRollupList) {
  298. productCategory = delegator.findByPrimaryKey("ProductCategory", UtilMisc.toMap("productCategoryId", productChildCategoryRollup.getString("productCategoryId")));
  299. StoreCustomCategoryType childCategoryType = new StoreCustomCategoryType();
  300. String ebayChildCategoryId = EbayStoreHelper.retriveEbayCategoryIdByPartyId(delegator,productCategory.getString("productCategoryId"),context.get("partyId").toString());
  301. if (ebayChildCategoryId == null) {
  302. childCategoryType.setName(productCategory.getString("categoryName"));
  303. listAdd.add(childCategoryType);
  304. } else {
  305. childCategoryType.setCategoryID(new Long(ebayChildCategoryId));
  306. childCategoryType.setName(productCategory.getString("categoryName"));
  307. listEdit.add(childCategoryType);
  308. }
  309. }
  310. if (listAdd.size() > 0) {
  311. req = new SetStoreCategoriesRequestType();
  312. categoryArrayType = new StoreCustomCategoryArrayType();
  313. categoryArrayType.setCustomCategory(toStoreCustomCategoryTypeArray(listAdd));
  314. req.setStoreCategories(categoryArrayType);
  315. req.setDestinationParentCategoryID(new Long(ebayParentCategoryId));
  316. result = excuteExportCategoryToEbayStore(call, req, StoreCategoryUpdateActionCodeType.ADD, delegator, context.get("partyId").toString(), catalogCategories);
  317. }
  318. if (listEdit.size() > 0) {
  319. req = new SetStoreCategoriesRequestType();
  320. categoryArrayType = new StoreCustomCategoryArrayType();
  321. categoryArrayType.setCustomCategory(toStoreCustomCategoryTypeArray(listEdit));
  322. req.setStoreCategories(categoryArrayType);
  323. req.setDestinationParentCategoryID(new Long(ebayParentCategoryId));
  324. result = excuteExportCategoryToEbayStore(call, req, StoreCategoryUpdateActionCodeType.RENAME, delegator, context.get("partyId").toString(), catalogCategories);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. } else {
  331. return ServiceUtil.returnError("Not found product Category type EBAY_ROOT in catalog "+context.get("prodCatalogId"));
  332. }
  333. } catch (GenericEntityException e) {
  334. result = ServiceUtil.returnFailure(e.getMessage());
  335. }
  336. if (result.get("responseMessage") != null && result.get("responseMessage").equals("fail")) result = ServiceUtil.returnError(result.get("errorMessage").toString());
  337. return result;
  338. }
  339. public static StoreCustomCategoryType[] toStoreCustomCategoryTypeArray(List<StoreCustomCategoryType> list) {
  340. StoreCustomCategoryType[] storeCustomCategoryTypeArry = null;
  341. try {
  342. if (list != null && list.size() > 0) {
  343. storeCustomCategoryTypeArry = new StoreCustomCategoryType[list.size()];
  344. int i=0;
  345. for (StoreCustomCategoryType val : list) {
  346. storeCustomCategoryTypeArry[i] = val;
  347. }
  348. }
  349. } catch (Exception e) {
  350. Debug.logError(e.getMessage(), module);
  351. }
  352. return storeCustomCategoryTypeArry;
  353. }
  354. public static Map<String, Object> excuteExportCategoryToEbayStore(SetStoreCategoriesCall call, SetStoreCategoriesRequestType req, StoreCategoryUpdateActionCodeType actionCode,Delegator delegator, String partyId,List<GenericValue> catalogCategories) {
  355. Map<String, Object> result = FastMap.newInstance();
  356. SetStoreCategoriesResponseType resp = null;
  357. try {
  358. if (req != null && actionCode != null) {
  359. req.setAction(actionCode);
  360. resp = (SetStoreCategoriesResponseType) call.execute(req);
  361. if (resp != null && "SUCCESS".equals(resp.getAck().toString())) {
  362. long returnTaskId = resp.getTaskID() == null? 0: resp.getTaskID().longValue();
  363. TaskStatusCodeType returnedStatus = resp.getStatus();
  364. StoreCustomCategoryArrayType returnedCustomCategory = resp.getCustomCategory();
  365. if (actionCode.equals(StoreCategoryUpdateActionCodeType.ADD) && returnedCustomCategory != null) {
  366. StoreCustomCategoryType[] returnCategoryTypeList = returnedCustomCategory.getCustomCategory();
  367. for (StoreCustomCategoryType returnCategoryType : returnCategoryTypeList) {
  368. List<GenericValue> productCategoryList = delegator.findByAnd("ProductCategory", UtilMisc.toMap("categoryName",returnCategoryType.getName(),"productCategoryTypeId","EBAY_CATEGORY"));
  369. for (GenericValue productCategory : productCategoryList) {
  370. if (EbayStoreHelper.veriflyCategoryInCatalog(delegator,catalogCategories,productCategory.getString("productCategoryId"))) {
  371. if (EbayStoreHelper.createEbayCategoryIdByPartyId(delegator, productCategory.getString("productCategoryId"), partyId, String.valueOf(returnCategoryType.getCategoryID()))) {
  372. Debug.logInfo("Create new ProductCategoryRollup with partyId "+partyId+" categoryId "+productCategory.getString("productCategoryId")+ " and ebayCategoryId "+String.valueOf(returnCategoryType.getCategoryID()), module);
  373. }
  374. break;
  375. }
  376. }
  377. }
  378. }
  379. result = ServiceUtil.returnSuccess("Export categories to ebay store".concat(" success."));
  380. } else {
  381. result = ServiceUtil.returnError("Fail to export categories to an ebay store ".concat(resp.getMessage()));
  382. }
  383. }
  384. } catch (ApiException e) {
  385. result = ServiceUtil.returnFailure(e.getMessage());
  386. } catch (SdkSoapException e) {
  387. result = ServiceUtil.returnFailure(e.getMessage());
  388. } catch (SdkException e) {
  389. result = ServiceUtil.returnFailure(e.getMessage());
  390. } catch (GenericEntityException e) {
  391. result = ServiceUtil.returnFailure(e.getMessage());
  392. }
  393. return result;
  394. }
  395. public static Map buildSetStoreXml(DispatchContext dctx, Map context, StringBuffer dataStoreXml, String token, String siteID) {
  396. Locale locale = (Locale)context.get("locale");
  397. try {
  398. Delegator delegator = dctx.getDelegator();
  399. String webSiteUrl = (String)context.get("webSiteUrl");
  400. List selectResult = (List)context.get("selectResult");
  401. StringUtil.SimpleEncoder encoder = StringUtil.getEncoder("xml");
  402. // Get the list of products to be exported to eBay
  403. try {
  404. Document storeDocument = UtilXml.makeEmptyXmlDocument("SetStoreRequest");
  405. Element storeRequestElem = storeDocument.getDocumentElement();
  406. storeRequestElem.setAttribute("xmlns", "urn:ebay:apis:eBLBaseComponents");
  407. appendRequesterCredentials(storeRequestElem, storeDocument, token);
  408. /*UtilXml.addChildElementValue(storeRequestElem, "SiteId", siteID, storeDocument);
  409. UtilXml.addChildElementValue(storeRequestElem, "DetailLevel", "ReturnAll", storeDocument);
  410. UtilXml.addChildElementValue(storeRequestElem, "LevelLimit", "1", storeDocument);*/
  411. // Prepare data for set to XML
  412. GenericValue productStore = null;
  413. if (UtilValidate.isNotEmpty(context.get("productStoreId").toString())) {
  414. productStore = delegator.findByPrimaryKey("ProductStore",UtilMisc.toMap("productStoreId", context.get("productStoreId").toString()));
  415. }
  416. Element itemElem = UtilXml.addChildElement(storeRequestElem, "Store", storeDocument);
  417. UtilXml.addChildElementValue(itemElem, "Name", (String) productStore.getString("storeName"), storeDocument);
  418. UtilXml.addChildElementValue(itemElem, "SubscriptionLevel", "Basic", storeDocument);
  419. UtilXml.addChildElementValue(itemElem, "Description", (String) productStore.getString("title"), storeDocument);
  420. dataStoreXml.append(UtilXml.writeXmlDocument(storeDocument));
  421. } catch (Exception e) {
  422. Debug.logError("Exception during building data items to eBay: " + e.getMessage(), module);
  423. return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsExportToEbay.exceptionDuringBuildingDataItemsToEbay", locale));
  424. }
  425. } catch (Exception e) {
  426. Debug.logError("Exception during building data items to eBay: " + e.getMessage(), module);
  427. return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsExportToEbay.exceptionDuringBuildingDataItemsToEbay", locale));
  428. }
  429. return ServiceUtil.returnSuccess();
  430. }
  431. public static String readEbayResponse(String msg, String productStoreId) {
  432. String result ="success";
  433. try {
  434. Document docResponse = UtilXml.readXmlDocument(msg, true);
  435. Element elemResponse = docResponse.getDocumentElement();
  436. String ack = UtilXml.childElementValue(elemResponse, "Ack", "Failure");
  437. if (ack != null && "Failure".equals(ack)) {
  438. String errorMessage = "";
  439. List errorList = UtilXml.childElementList(elemResponse, "Errors");
  440. Iterator errorElemIter = errorList.iterator();
  441. while (errorElemIter.hasNext()) {
  442. Element errorElement = (Element) errorElemIter.next();
  443. errorMessage = UtilXml.childElementValue(errorElement, "LongMessage");
  444. }
  445. result = errorMessage;
  446. } else {
  447. String productSuccessfullyExportedMsg = "Successfully exported with ID (" + productStoreId + ").";
  448. result = "success";
  449. }
  450. } catch (Exception e) {
  451. Debug.logError("Error in processing xml string" + e.getMessage(), module);
  452. result = "Failure";
  453. }
  454. return result;
  455. }
  456. public static Map buildGetStoreXml(Map context, StringBuffer dataStoreXml, String token, String siteID) {
  457. Locale locale = (Locale)context.get("locale");
  458. try {
  459. StringUtil.SimpleEncoder encoder = StringUtil.getEncoder("xml");
  460. // Get the list of products to be exported to eBay
  461. try {
  462. Document storeDocument = UtilXml.makeEmptyXmlDocument("GetStoreRequest");
  463. Element storeRequestElem = storeDocument.getDocumentElement();
  464. storeRequestElem.setAttribute("xmlns", "urn:ebay:apis:eBLBaseComponents");
  465. appendRequesterCredentials(storeRequestElem, storeDocument, token);
  466. //UtilXml.addChildElementValue(storeRequestElem, "CategorySiteID", siteID, storeDocument);
  467. UtilXml.addChildElementValue(storeRequestElem, "DetailLevel", "ReturnAll", storeDocument);
  468. UtilXml.addChildElementValue(storeRequestElem, "LevelLimit", "1", storeDocument);
  469. dataStoreXml.append(UtilXml.writeXmlDocument(storeDocument));
  470. } catch (Exception e) {
  471. Debug.logError("Exception during building data to eBay: " + e.getMessage(), module);
  472. return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsExportToEbay.exceptionDuringBuildingDataItemsToEbay", locale));
  473. }
  474. } catch (Exception e) {
  475. Debug.logError("Exception during building data to eBay: " + e.getMessage(), module);
  476. return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsExportToEbay.exceptionDuringBuildingDataItemsToEbay", locale));
  477. }
  478. return ServiceUtil.returnSuccess();
  479. }
  480. public static Map buildSetStoreCategoriesXml(DispatchContext dctx, Map context, StringBuffer dataStoreXml, String token, String siteID, String productCategoryId) {
  481. Delegator delegator = dctx.getDelegator();
  482. Locale locale = (Locale)context.get("locale");
  483. try {
  484. StringUtil.SimpleEncoder encoder = StringUtil.getEncoder("xml");
  485. // Get the list of products to be exported to eBay
  486. try {
  487. Document storeDocument = UtilXml.makeEmptyXmlDocument("SetStoreCategoriesRequest");
  488. Element storeRequestElem = storeDocument.getDocumentElement();
  489. storeRequestElem.setAttribute("xmlns", "urn:ebay:apis:eBLBaseComponents");
  490. appendRequesterCredentials(storeRequestElem, storeDocument, token);
  491. UtilXml.addChildElementValue(storeRequestElem, "DetailLevel", "ReturnAll", storeDocument);
  492. UtilXml.addChildElementValue(storeRequestElem, "Version", "643", storeDocument);
  493. UtilXml.addChildElementValue(storeRequestElem, "Action", "Add", storeDocument);
  494. Element StoreCategoriesElem = UtilXml.addChildElement(storeRequestElem, "StoreCategories", storeDocument);
  495. //UtilXml.addChildElementValue(StoreCategoriesElem, "Country", (String)context.get("country"), storeDocument);
  496. GenericValue category = null;
  497. if (UtilValidate.isNotEmpty(context.get("prodCatalogId"))) {
  498. category = delegator.findByPrimaryKeyCache("ProductCategory", UtilMisc.toMap("productCategoryId", productCategoryId));
  499. }
  500. String categoryName = category.getString("productCategoryId").toString();
  501. if (category.getString("categoryName").toString() != null) {
  502. categoryName = category.getString("categoryName").toString();
  503. }
  504. Element customCategoryElem = UtilXml.addChildElement(StoreCategoriesElem, "CustomCategory", storeDocument);
  505. //UtilXml.addChildElementValue(customCategoryElem, "CategoryID", "", storeDocument);
  506. UtilXml.addChildElementValue(customCategoryElem, "Name", categoryName, storeDocument);
  507. dataStoreXml.append(UtilXml.writeXmlDocument(storeDocument));
  508. } catch (Exception e) {
  509. Debug.logError("Exception during building data to eBay: " + e.getMessage(), module);
  510. return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsExportToEbay.exceptionDuringBuildingDataItemsToEbay", locale));
  511. }
  512. } catch (Exception e) {
  513. Debug.logError("Exception during building data to eBay: " + e.getMessage(), module);
  514. return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsExportToEbay.exceptionDuringBuildingDataItemsToEbay", locale));
  515. }
  516. return ServiceUtil.returnSuccess();
  517. }
  518. public static Map readEbayGetStoreCategoriesResponse(String msg, Locale locale) {
  519. Map results = null;
  520. List categories = FastList.newInstance();
  521. try {
  522. Document docResponse = UtilXml.readXmlDocument(msg, true);
  523. Element elemResponse = docResponse.getDocumentElement();
  524. String ack = UtilXml.childElementValue(elemResponse, "Ack", "Failure");
  525. if (ack != null && "Failure".equals(ack)) {
  526. String errorMessage = "";
  527. List errorList = UtilXml.childElementList(elemResponse, "Errors");
  528. Iterator errorElemIter = errorList.iterator();
  529. while (errorElemIter.hasNext()) {
  530. Element errorElement = (Element) errorElemIter.next();
  531. errorMessage = UtilXml.childElementValue(errorElement, "ShortMessage", "");
  532. }
  533. return ServiceUtil.returnFailure(errorMessage);
  534. } else {
  535. // retrieve Store
  536. List Store = UtilXml.childElementList(elemResponse, "Store");
  537. Iterator StoreElemIter = Store.iterator();
  538. while (StoreElemIter.hasNext()) {
  539. Element StoreElemIterElemIterElement = (Element) StoreElemIter.next();
  540. // retrieve Custom Category Array
  541. List customCategories = UtilXml.childElementList(StoreElemIterElemIterElement, "CustomCategories");
  542. Iterator customCategoriesElemIter = customCategories.iterator();
  543. while (customCategoriesElemIter.hasNext()) {
  544. Element customCategoriesElemIterElement = (Element)customCategoriesElemIter.next();
  545. // retrieve CustomCategory
  546. List customCategory = UtilXml.childElementList(customCategoriesElemIterElement, "CustomCategory");
  547. Iterator customCategoryElemIter = customCategory.iterator();
  548. while (customCategoryElemIter.hasNext()) {
  549. Map categ = FastMap.newInstance();
  550. Element categoryElement = (Element)customCategoryElemIter.next();
  551. categ.put("CategoryID", UtilXml.childElementValue(categoryElement, "CategoryID"));
  552. categ.put("CategoryName", UtilXml.childElementValue(categoryElement, "Name"));
  553. categ.put("CategorySeq", UtilXml.childElementValue(categoryElement, "Order"));
  554. categories.add(categ);
  555. }
  556. }
  557. }
  558. categories = UtilMisc.sortMaps(categories, UtilMisc.toList("CategoryName"));
  559. results = UtilMisc.toMap("categories", categories);
  560. }
  561. } catch (Exception e) {
  562. return ServiceUtil.returnFailure();
  563. }
  564. return results;
  565. }
  566. public static Map<String, Object> getEbayStoreUser(DispatchContext dctx, Map<String, ? extends Object> context) {
  567. Map<String, Object> result = FastMap.newInstance();
  568. String errorMsg = null;
  569. LocalDispatcher dispatcher = dctx.getDispatcher();
  570. GenericValue userLogin = (GenericValue) context.get("userLogin");
  571. Delegator delegator = dctx.getDelegator();
  572. Locale locale = (Locale) context.get("locale");
  573. String productStoreId = (String) context.get("productStoreId");
  574. List itemsResult = FastList.newInstance();
  575. try {
  576. List productStores = delegator.findByAnd("ProductStoreRole", UtilMisc.toMap("productStoreId", productStoreId, "roleTypeId", "EBAY_ACCOUNT"));
  577. if (productStores.size() != 0) {
  578. String partyId = ((GenericValue) productStores.get(0)).getString("partyId");
  579. List userLoginStore = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId));
  580. if (userLoginStore.size() != 0) {
  581. String userLoginId = ((GenericValue) userLoginStore.get(0)).getString("userLoginId");
  582. result.put("userLoginId", userLoginId);
  583. }
  584. }
  585. } catch (Exception e) {
  586. }
  587. return result;
  588. }
  589. /*Editing the Store Settings */
  590. /* Get store output */
  591. public static Map<String,Object> getEbayStoreOutput(DispatchContext dctx, Map<String,Object> context) {
  592. Locale locale = (Locale) context.get("locale");
  593. GenericValue userLogin = (GenericValue) context.get("userLogin");
  594. Delegator delegator = dctx.getDelegator();
  595. Map<String,Object> result = FastMap.newInstance();
  596. StoreType returnedStoreType = null;
  597. GetStoreRequestType req = new GetStoreRequestType();
  598. GetStoreResponseType resp = null;
  599. String userLoginId = null;
  600. String password = null;
  601. if (context.get("productStoreId") != null) {
  602. String partyId = null;
  603. try {
  604. List<GenericValue> productStoreRoles = delegator.findByAnd("ProductStoreRole", UtilMisc.toMap("productStoreId", context.get("productStoreId").toString(),"roleTypeId","EBAY_ACCOUNT"));
  605. if (productStoreRoles.size() != 0) {
  606. partyId= (String)productStoreRoles.get(0).get("partyId");
  607. List<GenericValue> userLogins = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId));
  608. if (userLogins.size() != 0) {
  609. userLoginId = (String)userLogins.get(0).get("userLoginId");
  610. password = (String)userLogins.get(0).get("currentPassword");
  611. }
  612. }
  613. } catch (GenericEntityException e1) {
  614. e1.printStackTrace();
  615. }
  616. Debug.logInfo("userLoginId is "+userLoginId+" and productStoreId is "+ context.get("productStoreId"), module);
  617. GetStoreCall call = new GetStoreCall(EbayStoreHelper.getApiContext((String)context.get("productStoreId"), locale, delegator));
  618. //call.setSite(EbayHelper.getSiteCodeType((String)context.get("productStoreId"), locale, delegator));
  619. call.setCategoryStructureOnly(false);
  620. call.setUserID(userLoginId);
  621. try {
  622. resp = (GetStoreResponseType)call.execute(req);
  623. if (resp != null && "SUCCESS".equals(resp.getAck().toString())) {
  624. returnedStoreType = resp.getStore();
  625. result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
  626. //result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "EbayStoreLoadSuccess", locale));
  627. // update product store in ofbiz
  628. updateProductStore(dctx, context, returnedStoreType,(String) context.get("productStoreId"));
  629. Map<String,Object> ebayResp = FastMap.newInstance();
  630. ebayResp.put("storeName", returnedStoreType.getName());
  631. ebayResp.put("storeUrl", returnedStoreType.getURL());
  632. ebayResp.put("storeUrlPath", returnedStoreType.getURLPath());
  633. String desc = returnedStoreType.getDescription();
  634. if (desc != null) desc = desc.trim();
  635. ebayResp.put("storeDesc", desc);
  636. StoreLogoType logoType = returnedStoreType.getLogo();
  637. ebayResp.put("storeLogoId", logoType.getLogoID());
  638. ebayResp.put("storeLogoName", logoType.getName());
  639. ebayResp.put("storeLogoURL", logoType.getURL());
  640. StoreThemeType themeType = returnedStoreType.getTheme();
  641. ebayResp.put("storeThemeId", themeType.getThemeID());
  642. ebayResp.put("storeThemeName", themeType.getName());
  643. StoreColorSchemeType colorSchemeType = themeType.getColorScheme();
  644. ebayResp.put("storeColorSchemeId", colorSchemeType.getColorSchemeID());
  645. StoreColorType colorType = colorSchemeType.getColor();
  646. ebayResp.put("storeColorPrimary", colorType.getPrimary());
  647. ebayResp.put("storeColorAccent", colorType.getAccent());
  648. ebayResp.put("storeColorSecondary", colorType.getSecondary());
  649. StoreFontType fontType = colorSchemeType.getFont();
  650. ebayResp.put("storeDescColor", fontType.getDescColor());
  651. ebayResp.put("storeNameColor", fontType.getNameColor());
  652. ebayResp.put("storeTitleColor", fontType.getTitleColor());
  653. if (fontType != null) {// basic & advance theme
  654. String themeId = themeType.getThemeID().toString().concat("-").concat(colorSchemeType.getColorSchemeID().toString());
  655. context.put("themeId", themeId);
  656. Map<String,Object> results = retrieveThemeColorSchemeByThemeId(dctx, context);
  657. if (results != null) {
  658. Map<String,Object> storeFontScheme = (Map<String,Object>) results.get("storeFontScheme");
  659. if (storeFontScheme != null) {
  660. ebayResp.put("storeDescFontFace", storeFontScheme.get("storeFontTypeFontDescValue"));
  661. ebayResp.put("storeDescSizeCode", storeFontScheme.get("storeDescSizeValue"));
  662. ebayResp.put("storeNameFontFace", storeFontScheme.get("storeFontTypeFontFaceValue"));
  663. ebayResp.put("storeNameFontFaceSize", storeFontScheme.get("storeFontTypeSizeFaceValue"));
  664. ebayResp.put("storeTitleFontFace", storeFontScheme.get("storeFontTypeFontTitleValue"));
  665. ebayResp.put("storeTitleFontFaceSize", storeFontScheme.get("storeFontSizeTitleValue"));
  666. }
  667. }
  668. }
  669. StoreHeaderStyleCodeType storeHeaderStyleCodeType = returnedStoreType.getHeaderStyle();
  670. ebayResp.put("storeHeaderStyle", storeHeaderStyleCodeType.value());
  671. StoreHeaderStyleCodeType[] storeHeaderStyleCodeList = storeHeaderStyleCodeType.values();
  672. if (storeHeaderStyleCodeList != null) {
  673. List<Map<String,Object>> storeHeaderStyleList = FastList.newInstance();
  674. int i=0;
  675. while (i<storeHeaderStyleCodeList.length) {
  676. Map<String,Object> storeHeaderStyleMap = FastMap.newInstance();
  677. StoreHeaderStyleCodeType storeHeaderStyleCode = storeHeaderStyleCodeList[i];
  678. storeHeaderStyleMap.put("storeHeaderStyleName", storeHeaderStyleCode.name());
  679. storeHeaderStyleMap.put("storeHeaderStyleValue", storeHeaderStyleCode.value());
  680. storeHeaderStyleList.add(storeHeaderStyleMap);
  681. i++;
  682. }
  683. ebayResp.put("storeHeaderStyleList", storeHeaderStyleList);
  684. }
  685. ebayResp.put("storeHomePage", returnedStoreType.getHomePage().toString());
  686. StoreItemListLayoutCodeType storeItemListLayoutCodeType = returnedStoreType.getItemListLayout();
  687. ebayResp.put("storeItemLayoutSelected", storeItemListLayoutCodeType.value());
  688. StoreItemListLayoutCodeType[] storeItemListLayoutCodeTypeList = storeItemListLayoutCodeType.values();
  689. if (storeItemListLayoutCodeTypeList != null) {
  690. List<Map<String,Object>> storeItemListLayoutCodeList = FastList.newInstance();
  691. int i = 0;
  692. while (i < storeItemListLayoutCodeTypeList.length) {
  693. Map<String,Object> storeItemListLayoutCodeMap = FastMap.newInstance();
  694. StoreItemListLayoutCodeType storeItemListLayoutCode = storeItemListLayoutCodeTypeList[i];
  695. storeItemListLayoutCodeMap.put("storeItemLayoutName", storeItemListLayoutCode.name());
  696. storeItemListLayoutCodeMap.put("storeItemLayoutValue", storeItemListLayoutCode.value());
  697. storeItemListLayoutCodeList.add(storeItemListLayoutCodeMap);
  698. i++;
  699. }
  700. ebayResp.put("storeItemLayoutList", storeItemListLayoutCodeList);
  701. }
  702. StoreItemListSortOrderCodeType storeItemListSortOrderCodeType = returnedStoreType.getItemListSortOrder();
  703. ebayResp.put("storeItemSortOrderSelected", storeItemListSortOrderCodeType.value());
  704. StoreItemListSortOrderCodeType[] storeItemListSortOrderCodeTypeList = storeItemListSortOrderCodeType.values();
  705. if (storeItemListSortOrderCodeTypeList != null) {
  706. List<Map<String,Object>> storeItemSortOrderCodeList = FastList.newInstance();
  707. int i = 0;
  708. while (i < storeItemListSortOrderCodeTypeList.length) {
  709. Map<String,Object> storeItemSortOrderCodeMap = FastMap.newInstance();
  710. StoreItemListSortOrderCodeType storeItemListLayoutCode = storeItemListSortOrderCodeTypeList[i];
  711. storeItemSortOrderCodeMap.put("storeItemSortLayoutName", storeItemListLayoutCode.name());
  712. storeItemSortOrderCodeMap.put("storeItemSortLayoutValue", storeItemListLayoutCode.value());
  713. storeItemSortOrderCodeList.add(storeItemSortOrderCodeMap);
  714. i++;
  715. }
  716. ebayResp.put("storeItemSortOrderList", storeItemSortOrderCodeList);
  717. }
  718. ebayResp.put("storeCustomHeader", returnedStoreType.getCustomHeader());
  719. StoreCustomHeaderLayoutCodeType storeCustomHeaderLayoutCodeType = returnedStoreType.getCustomHeaderLayout();
  720. ebayResp.put("storeCustomHeaderLayout", storeCustomHeaderLayoutCodeType.value());
  721. StoreCustomHeaderLayoutCodeType[] storeCustomHeaderLayoutCodeTypeList = storeCustomHeaderLayoutCodeType.values();
  722. if (storeCustomHeaderLayoutCodeTypeList != null) {
  723. List<Map<String,Object>> storeCustomHeaderLayoutList = FastList.newInstance();
  724. int i = 0;
  725. while (i < storeCustomHeaderLayoutCodeTypeList.length) {
  726. Map<String,Object> storeCustomHeaderLayoutMap = FastMap.newInstance();
  727. StoreCustomHeaderLayoutCodeType StoreCustomHeaderLayoutCode = storeCustomHeaderLayoutCodeTypeList[i];
  728. storeCustomHeaderLayoutMap.put("storeCustomHeaderLayoutName", StoreCustomHeaderLayoutCode.name());
  729. storeCustomHeaderLayoutMap.put("storeCustomHeaderLayoutValue", StoreCustomHeaderLayoutCod

Large files files are truncated, but you can click here to view the full file