Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.13 KB | None | 0 0
  1. import com.jcatalog.businesspartner.GormCustomer
  2. import com.jcatalog.businesspartner.GormCustomer2Address
  3. import com.jcatalog.businesspartner.GormCustomerGroup
  4. import com.jcatalog.businesspartner.GormSupplier
  5. import com.jcatalog.businesspartner.GormSupplier2Address
  6. import com.jcatalog.businesspartner.GormSupplierGroup
  7. import com.jcatalog.businesspartner.GormSupplierProvisioning
  8. import com.jcatalog.businesspartner.classification.GormClassificationUsage
  9. import com.jcatalog.businesspartner.classification.GormSupplier2ClassificationGroup
  10. import com.jcatalog.classification.GormClassification
  11. import com.jcatalog.classification.GormClassificationGroup
  12. import com.jcatalog.core.DateRange
  13. import com.jcatalog.core.GormCurrency
  14. import com.jcatalog.core.i18n.GormLanguage
  15. import com.jcatalog.core.status.GormDefaultStatus
  16. import com.jcatalog.pim.masterdata.dataimport.xml.FlatMLImportUtils
  17. import com.jcatalog.termsofbusiness.GormMethodOfPayment
  18. import com.jcatalog.termsofbusiness.GormTermsOfDelivery
  19. import com.jcatalog.termsofbusiness.GormTermsOfPayment
  20. import groovy.util.slurpersupport.GPathResult
  21.  
  22. import com.jcatalog.product.GormProductCatalog
  23. import com.jcatalog.product.GormCatalogCategory
  24. import com.jcatalog.product.GormCatalogUsage
  25. import com.jcatalog.product.GormCatalogProvisioning
  26.  
  27.  
  28. import javax.xml.stream.XMLStreamReader
  29. import java.text.DateFormat
  30. import java.text.DecimalFormat
  31. import java.text.NumberFormat
  32. import java.text.SimpleDateFormat
  33.  
  34. Closure getNumberFormat = {
  35. return new DecimalFormat("#.#")
  36. }
  37.  
  38. Closure getDateFormat = {
  39. return new SimpleDateFormat("MM/dd/yyyy")
  40. }
  41.  
  42. def delete = {clazz, query, fields ->
  43. return { GPathResult pathResult ->
  44. clazz.withTransaction {
  45. def params = [:]
  46. fields.each { field -> params[field] = pathResult."${field}"?.text() }
  47. if (params.find { !it }) return "failed"
  48. def result = clazz.executeQuery(query, params)
  49. if (!result) return "failed"
  50. result[0].delete(flush: true)
  51. return "delete"
  52. }
  53. }
  54. }
  55.  
  56. Closure insertOrUpdateCatalog = {GPathResult pathResult ->
  57. GormProductCatalog.withTransaction {
  58. NumberFormat numberFormat = getNumberFormat.call()
  59. DateFormat dateFormat = getDateFormat.call()
  60. String actionType = "undefined"
  61. def productCatalogId = pathResult.ProductCatalogId.text()
  62. def statusId = pathResult.Status.text()
  63. def catalogCategoryId = pathResult.CatalogCategoryId.text()
  64.  
  65. if (!productCatalogId) {
  66. return "failed"
  67. }
  68.  
  69. GormProductCatalog catalog = null
  70. if (updateType == "insert-update") {
  71. catalog = GormProductCatalog.findByProductCatalogId(productCatalogId)
  72. if (!catalog) {
  73. catalog = new GormProductCatalog(productCatalogId: productCatalogId)
  74. catalog.save()
  75. actionType = "insert"
  76. } else {
  77. actionType = "update"
  78. }
  79. } else if (updateType == "insert") {
  80. catalog = new GormProductCatalog(productCatalogId: productCatalogId)
  81. catalog = catalog.save()
  82. actionType = "insert"
  83. } else if (updateType == "update") {
  84. catalog = GormProductCatalog.findByProductCatalogId(productCatalogId)
  85. if (!catalog) {
  86. return "failed"
  87. }
  88. actionType = "update"
  89. }
  90.  
  91. if(statusId){
  92. def status = GormDefaultStatus.findByStatusId()
  93. if(status) catalog.status = status
  94. }
  95.  
  96. def validRange = new DateRange()
  97. FlatMLImportUtils.setDatePropertyFromPathResult(validRange, pathResult, dateFormat, "validFrom", "from")
  98. FlatMLImportUtils.setDatePropertyFromPathResult(validRange, pathResult, dateFormat, "validTo", "to")
  99. catalog.validRange = validRange
  100. FlatMLImportUtils.setNumberPropertyFromPathResult(catalog, pathResult, numberFormat, "version")
  101. FlatMLImportUtils.setStringPropertyFromPathResult(catalog, pathResult, "description")
  102. FlatMLImportUtils.setStringPropertyFromPathResult(catalog, pathResult, "type")
  103. FlatMLImportUtils.setBooleanPropertyFromPathResult(catalog, pathResult, "isPunchoutOnly")
  104. FlatMLImportUtils.setBooleanPropertyFromPathResult(catalog, pathResult, "requireSafetyCheck")
  105. FlatMLImportUtils.setBooleanPropertyFromPathResult(catalog, pathResult, "requireChemicalCheck")
  106.  
  107. if(catalogCategoryId){
  108. def catalogCategory = GormCatalogCategory.get(catalogCategoryId)
  109. if(catalogCategory) catalog.catalogCategory = catalogCategory
  110. }
  111.  
  112. catalog.save(flush:true)
  113. return actionType
  114. }
  115. }
  116.  
  117. Closure insertOrUpdateCatalogUsage = {GPathResult pathResult ->
  118. GormCatalogUsage.withTransaction {
  119. String actionType = "undefined"
  120. def productCatalogId = pathResult.ProductCatalogID.text()
  121. def customerId = pathResult.CustomerID.text()
  122. if (!productCatalogId || !customerId) {
  123. return "failed"
  124. }
  125.  
  126. def catalogUsage = null
  127. if (updateType == "insert-update") {
  128. catalogUsage = GormCatalogUsage.find("from GormCatalogUsage as cu where cu.catalog.productCatalogId='${productCatalogId}' and cu.customer.customerId='${customerId}' ")
  129. if (!catalogUsage) {
  130. catalogUsage = new GormCatalogUsage()
  131. catalogUsage.save()
  132. actionType = "insert"
  133. } else {
  134. actionType = "update"
  135. }
  136. } else if (updateType == "insert") {
  137. catalogUsage = new GormCatalogUsage()
  138. actionType = "insert"
  139. } else if (updateType == "update") {
  140. catalogUsage = GormCatalogUsage.find("from GormCatalogUsage as cu where cu.catalog.productCatalogId='${productCatalogId}' and cu.customer.customerId='${customerId}' ")
  141. if (!catalogUsage) {
  142. return "failed"
  143. }
  144. actionType = "update"
  145. }
  146.  
  147. def catalog = GormProductCatalog.findByProductCatalogId(productCatalogId)
  148. if(catalog) catalogUsage.catalog = catalog
  149. def customer = GormCustomer.findByCustomerId(customerId)
  150. if(customer) catalogUsage.customer = customer
  151.  
  152. catalogUsage.save(flush:true)
  153. return actionType
  154. }
  155. }
  156.  
  157. Closure insertOrUpdateCatalogProvisioning = {GPathResult pathResult ->
  158. GormCatalogUsage.withTransaction {
  159. String actionType = "undefined"
  160. def productCatalogId = pathResult.ProductCatalogID.text()
  161. def supplierId = pathResult.SupplierID.text()
  162. if (!productCatalogId || !supplierId) {
  163. return "failed"
  164. }
  165.  
  166. def catalogProvisioning = null
  167. if (updateType == "insert-update") {
  168. catalogProvisioning = GormCatalogProvisioning.find("from GormCatalogProvisioning as cu where cu.catalog.productCatalogId='${productCatalogId}' and cu.supplier.supplierId='${supplierId}' ")
  169. if (!catalogProvisioning) {
  170. catalogProvisioning = new GormCatalogProvisioning()
  171. catalogProvisioning.save()
  172. actionType = "insert"
  173. } else {
  174. actionType = "update"
  175. }
  176. } else if (updateType == "insert") {
  177. catalogProvisioning = new GormCatalogProvisioning()
  178. actionType = "insert"
  179. } else if (updateType == "update") {
  180. catalogProvisioning = GormCatalogProvisioning.find("from GormCatalogUsage as cu where cu.catalog.productCatalogId='${productCatalogId}' and cu.supplier.supplierId='${supplierId}' ")
  181. if (!catalogProvisioning) {
  182. return "failed"
  183. }
  184. actionType = "update"
  185. }
  186.  
  187. def catalog = GormProductCatalog.findByProductCatalogId(productCatalogId)
  188. if(catalog) catalogProvisioning.catalog = catalog
  189. def supplier = GormCustomer.findBySupplierId(supplierId)
  190. if(supplier) catalogProvisioning.supplier = supplier
  191.  
  192. catalogProvisioning.save(flush:true)
  193. return actionType
  194. }
  195. }
  196.  
  197. Closure insertOrUpdateCustomerGroup = {GPathResult pathResult ->
  198. GormCustomerGroup.withTransaction {
  199. if (!pathResult.CustomerGroupId.text()) {
  200. return "failed"
  201. }
  202. String actionType = "undefined"
  203. GormCustomerGroup customerGroup = null
  204. if (updateType == "insert-update") {
  205. customerGroup = GormCustomerGroup.findByCustomerGroupId(pathResult.CustomerGroupId.text())
  206. if (!customerGroup) {
  207. customerGroup = new GormCustomerGroup(customerGroupId: pathResult.CustomerGroupId.text())
  208. customerGroup = customerGroup.save()
  209. actionType = "insert"
  210. } else {
  211. actionType = "update"
  212. }
  213. } else if (updateType == "insert") {
  214. customerGroup = new GormCustomerGroup(customerGroupId: pathResult.CustomerGroupId.text())
  215. customerGroup = customerGroup.save()
  216. actionType = "insert"
  217. } else if (updateType == "update") {
  218. customerGroup = GormCustomerGroup.findByCustomerGroupId(pathResult.CustomerGroupId.text())
  219. if (!customerGroup) {
  220. return "failed"
  221. }
  222. actionType = "update"
  223. }
  224. FlatMLImportUtils.setStringPropertyFromPathResult(customerGroup, pathResult, "name")
  225. customerGroup.save(flush:true)
  226. return actionType
  227. }
  228. }
  229.  
  230.  
  231. Closure insertOrUpdateCustomer = {GPathResult pathResult ->
  232.  
  233. GormCustomer.withTransaction {
  234. NumberFormat numberFormat = getNumberFormat.call()
  235. String actionType = "undefined"
  236. if (!pathResult.CustomerId.text()) {
  237. return "failed"
  238. }
  239.  
  240. GormCustomer customer = null
  241. if (updateType == "insert-update") {
  242. customer = GormCustomer.findByCustomerId(pathResult.CustomerId.text())
  243. if (!customer) {
  244. customer = new GormCustomer(customerId: pathResult.CustomerId.text())
  245. customer = customer.save()
  246. actionType = "insert"
  247. } else {
  248. actionType = "update"
  249. }
  250. } else if (updateType == "insert") {
  251. customer = new GormCustomer(customerId: pathResult.CustomerId.text())
  252. customer = customer.save()
  253. actionType = "insert"
  254. } else if (updateType == "update") {
  255. customer = GormCustomer.findByCustomerId(pathResult.CustomerId.text())
  256. if (!customer) {
  257. return "failed"
  258. }
  259. actionType = "update"
  260. }
  261.  
  262. if (pathResult.CustomerGroupId.text()) {
  263. GormCustomerGroup customerGroup = GormCustomerGroup.findByCustomerGroupId(pathResult.CustomerGroupId.text())
  264. if (customerGroup) {
  265. customer.customerGroup = customerGroup
  266. }
  267. }
  268.  
  269. if (pathResult.ParentCustomerId.text()) {
  270. GormCustomer parentCustomer = GormCustomer.findByCustomerId(pathResult.ParentCustomerId.text())
  271. if (parentCustomer) {
  272. customer.parentCustomer = parentCustomer
  273. }
  274. }
  275.  
  276. if (pathResult.Tax.text()) {
  277. GormTax tax = GormTax.findByTaxId(pathResult.Tax.text())
  278. if (tax) {
  279. customer.tax = tax
  280. }
  281. }
  282.  
  283. if (pathResult.LanguageID.text()) {
  284. GormLanguage language = GormLanguage.get(pathResult.LanguageID.text())
  285. if (language) {
  286. customer.language = language
  287. }
  288. }
  289.  
  290. if (pathResult.CurrencyID.text()) {
  291. GormCurrency currency = GormCurrency.get(pathResult.CurrencyID.text())
  292. if (currency) {
  293. customer.currency = currency
  294. }
  295. }
  296.  
  297. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "customerName")
  298. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "dunsNo")
  299. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "information", "CustomerInfo")
  300. FlatMLImportUtils.setNumberPropertyFromPathResult(customer, pathResult, numberFormat, "invTolerancePercent")
  301. FlatMLImportUtils.setNumberPropertyFromPathResult(customer, pathResult, numberFormat, "invToleranceAbs")
  302. FlatMLImportUtils.setBooleanPropertyFromPathResult(customer, pathResult, "isLocked")
  303. FlatMLImportUtils.setBooleanPropertyFromPathResult(customer, pathResult, "isCustomerGrpMaster")
  304. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "extAccountCode","ExtAcctCode")
  305. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "extShortName")
  306. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "extName")
  307. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "extGlobalId")
  308. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "logo")
  309. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "headerText")
  310. FlatMLImportUtils.setStringPropertyFromPathResult(customer, pathResult, "footerText")
  311. customer.save(flush:true)
  312. return actionType
  313. }
  314. }
  315.  
  316. Closure insertOrUpdateCustomer2Address = {GPathResult pathResult ->
  317. GormCustomer2Address.withTransaction {
  318. String actionType = "undefined"
  319. if (!pathResult.CustomerID.text() || !pathResult.AddressID.text()) {
  320. return "failed"
  321. }
  322.  
  323. GormCustomer customer = GormCustomer.findByCustomerId(pathResult.CustomerID.text())
  324. GormAddress address = GormAddress.findByAddressId(pathResult.AddressID.text())
  325. if (!customer || !address) {
  326. return "failed"
  327. }
  328. GormCustomer2Address customer2Address = null
  329. if (updateType == "insert-update") {
  330. customer2Address = GormCustomer2Address.findByCustomerAndAddress(customer, address)
  331. if (!customer2Address) {
  332. customer2Address = new GormCustomer2Address(customer: customer, address: address)
  333. customer2Address = customer2Address.save()
  334. actionType = "insert"
  335. } else {
  336. actionType = "update"
  337. }
  338. } else if (updateType == "insert") {
  339. customer2Address = new GormCustomer2Address(customer: customer, address: address)
  340. customer2Address = customer2Address.save()
  341. actionType = "insert"
  342. } else if (updateType == "update") {
  343. customer2Address = GormCustomer2Address.findByCustomerAndAddress(customer, address)
  344. if (!customer2Address) {
  345. return "failed"
  346. }
  347. actionType = "update"
  348. }
  349.  
  350. FlatMLImportUtils.setBooleanPropertyFromPathResult(customer2Address, pathResult, "isDefault")
  351. FlatMLImportUtils.setStringPropertyFromPathResult(customer2Address, pathResult, "type")
  352. customer2Address.save(flush:true)
  353. return actionType
  354. }
  355. }
  356.  
  357. Closure insertOrUpdateClassificationUsage = {GPathResult pathResult ->
  358. GormClassificationUsage.withTransaction {
  359. String actionType = "undefined"
  360. if (!pathResult.ClassificationID.text() || !pathResult.CustomerID.text()) {
  361. return "failed"
  362. }
  363.  
  364. GormCustomer customer = GormCustomer.findByCustomerId(pathResult.CustomerID.text())
  365. GormClassification classification = GormClassification.findByClassificationId(pathResult.ClassificationID.text())
  366. if (!customer || !classification) {
  367. return "failed"
  368. }
  369. GormClassificationUsage classificationUsage = null
  370. if (updateType == "insert-update") {
  371. classificationUsage = GormClassificationUsage.findByCustomerAndClassification(customer, classification)
  372. if (!classificationUsage) {
  373. classificationUsage = new GormClassificationUsage(customer: customer, classification: classification)
  374. classificationUsage = classificationUsage.save()
  375. actionType = "insert"
  376. } else {
  377. actionType = "update"
  378. }
  379. } else if (updateType == "insert") {
  380. classificationUsage = new GormClassificationUsage(customer: customer, address: address)
  381. classificationUsage = classificationUsage.save()
  382. actionType = "insert"
  383. } else if (updateType == "update") {
  384. classificationUsage = GormClassificationUsage.findByCustomerAndClassification(customer, classification)
  385. if (!classificationUsage) {
  386. return "failed"
  387. }
  388. actionType = "update"
  389. }
  390.  
  391. FlatMLImportUtils.setBooleanPropertyFromPathResult(classificationUsage, pathResult, "isMaster")
  392. classificationUsage.save(flush:true)
  393. return actionType
  394. }
  395. }
  396.  
  397. Closure insertOrUpdateSupplierGroup = {GPathResult pathResult ->
  398. GormSupplierGroup.withTransaction {
  399. String actionType = "undefined"
  400. if (!pathResult.SupplierGroupID.text()) {
  401. return "failed"
  402. }
  403.  
  404. GormSupplierGroup supplierGroup = null
  405. if (updateType == "insert-update") {
  406. supplierGroup = GormSupplierGroup.findBySupplierGroupId(pathResult.SupplierGroupID.text())
  407. if (!supplierGroup) {
  408. supplierGroup = new GormSupplierGroup(supplierGroupId: pathResult.SupplierGroupID.text())
  409. supplierGroup = supplierGroup.save()
  410. actionType = "insert"
  411. } else {
  412. actionType = "update"
  413. }
  414. } else if (updateType == "insert") {
  415. supplierGroup = new GormSupplierGroup(supplierGroupId: pathResult.SupplierGroupID.text())
  416. supplierGroup = supplierGroup.save()
  417. actionType = "insert"
  418. } else if (updateType == "update") {
  419. supplierGroup = GormSupplierGroup.findBySupplierGroupId(pathResult.SupplierGroupID.text())
  420. if (!supplierGroup) {
  421. return "failed"
  422. }
  423. actionType = "update"
  424. }
  425. FlatMLImportUtils.setStringPropertyFromPathResult(supplierGroup, pathResult, "name")
  426. supplierGroup.save(flush:true)
  427. return actionType
  428. }
  429. }
  430.  
  431. Closure insertOrUpdateSupplier = {GPathResult pathResult ->
  432. GormSupplier.withTransaction {
  433. String actionType = "undefined"
  434. if (!pathResult.SupplierId.text()) {
  435. return "failed"
  436. }
  437.  
  438. GormSupplier supplier = null
  439. if (updateType == "insert-update") {
  440. supplier = GormSupplier.findBySupplierId(pathResult.SupplierId.text())
  441. if (!supplier) {
  442. supplier = new GormSupplier(supplierId: pathResult.SupplierId.text())
  443. supplier = supplier.save()
  444. actionType = "insert"
  445. } else {
  446. actionType = "update"
  447. }
  448. } else if (updateType == "insert") {
  449. supplier = new GormSupplier(supplierId: pathResult.SupplierId.text())
  450. supplier = supplier.save()
  451. actionType = "insert"
  452. } else if (updateType == "update") {
  453. supplier = GormSupplier.findBySupplierId(pathResult.SupplierId.text())
  454. if (!supplier) {
  455. return "failed"
  456. }
  457. actionType = "update"
  458. }
  459.  
  460. if (pathResult.SupplierGroupId.text()) {
  461. GormSupplierGroup supplierGroup = GormSupplierGroup.findBySupplierGroupId(pathResult.SupplierGroupId.text())
  462. if (supplierGroup) {
  463. supplier.supplierGroup = supplierGroup
  464. }
  465. }
  466.  
  467. if (pathResult.Language.text()) {
  468. GormLanguage language = GormLanguage.get(pathResult.Language.text())
  469. if (language) {
  470. supplier.language = language
  471. }
  472. }
  473.  
  474. if (pathResult.CurrencyID.text()) {
  475. GormCurrency currency = GormCurrency.get(pathResult.CurrencyID.text())
  476. if (currency) {
  477. supplier.currency = currency
  478. }
  479. }
  480.  
  481. if (pathResult.Status.text()) {
  482. GormDefaultStatus status = GormDefaultStatus.findByStatusIdAndObjectClass(pathResult.Status.text(),"SUPPLIER")
  483. if (status) {
  484. supplier.status = status
  485. }
  486. }
  487.  
  488. if (pathResult.TermsOfPaymentID.text()) {
  489. GormTermsOfPayment termsOfPayment = GormTermsOfPayment.get(pathResult.TermsOfPaymentID.text())
  490. if (termsOfPayment) {
  491. supplier.termsOfPayment = termsOfPayment
  492. }
  493. }
  494.  
  495. if (pathResult.TermsOfDeliveryID.text()) {
  496. GormTermsOfDelivery termsOfDelivery = GormTermsOfDelivery.get(pathResult.TermsOfDeliveryID.text())
  497. if (termsOfDelivery) {
  498. supplier.termsOfDelivery = termsOfDelivery
  499. }
  500. }
  501.  
  502. if (pathResult.MethodOfPaymentID.text()) {
  503. GormMethodOfPayment methodOfPayment = GormMethodOfPayment.get(pathResult.MethodOfPaymentID.text())
  504. if (methodOfPayment) {
  505. supplier.methodOfPayment = methodOfPayment
  506. }
  507. }
  508.  
  509. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "supplierName")
  510. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "dunsNo")
  511. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "extAccountCode","ExtAcctCode")
  512. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "extName")
  513. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "extShortName")
  514. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "extGlobalId")
  515. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "extSupplierID")
  516. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "accountNumber")
  517. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "bankIdentificationCode")
  518. FlatMLImportUtils.setStringPropertyFromPathResult(supplier, pathResult, "logo")
  519. supplier.save(flush:true)
  520. return actionType
  521.  
  522. }
  523. }
  524.  
  525. Closure insertOrUpdateSupplier2Address = {GPathResult pathResult ->
  526. GormSupplier2Address.withTransaction {
  527. String actionType = "undefined"
  528. if (!pathResult.SupplierID.text() || !pathResult.AddressID.text()) {
  529. return "failed"
  530. }
  531.  
  532. GormSupplier supplier = GormSupplier.findBySupplierId(pathResult.SupplierID.text())
  533. GormAddress address = GormAddress.findByAddressId(pathResult.AddressID.text())
  534. if (!supplier || !address) {
  535. return "failed"
  536. }
  537. GormSupplier2Address supplier2Address = null
  538. if (updateType == "insert-update") {
  539. supplier2Address = GormSupplier2Address.findBySupplierAndAddress(supplier, address)
  540. if (!supplier2Address) {
  541. supplier2Address = new GormSupplier2Address(supplier: supplier, address: address)
  542. supplier2Address = supplier2Address.save()
  543. actionType = "insert"
  544. } else {
  545. actionType = "update"
  546. }
  547. } else if (updateType == "insert") {
  548. supplier2Address = new GormSupplier2Address(supplier: supplier, address: address)
  549. supplier2Address = supplier2Address.save()
  550. actionType = "insert"
  551. } else if (updateType == "update") {
  552. supplier2Address = GormSupplier2Address.findBySupplierAndAddress(supplier, address)
  553. if (!supplier2Address) {
  554. return "failed"
  555. }
  556. actionType = "update"
  557. }
  558.  
  559. FlatMLImportUtils.setBooleanPropertyFromPathResult(supplier2Address, pathResult, "isDefault")
  560. FlatMLImportUtils.setStringPropertyFromPathResult(supplier2Address, pathResult, "type")
  561. supplier2Address.save(flush:true)
  562. return actionType
  563. }
  564. }
  565.  
  566. Closure insertOrUpdateSupplier2ClassificationGroup = { GPathResult pathResult ->
  567. GormSupplier2ClassificationGroup.withTransaction {
  568. if(!pathResult.SupplierID.text() || !pathResult.ClassificationID.text() || !pathResult.ClassificationGroupID.text() ||
  569. !pathResult.CustomerID.text()) {
  570. return "failed"
  571. }
  572. GormClassification classification = GormClassification.findByClassificationId(pathResult.ClassificationID.text())
  573. if(!classification) {
  574. return "failed"
  575. }
  576.  
  577. GormSupplier supplier = GormSupplier.findBySupplierId(pathResult.SupplierID.text())
  578. GormCustomer customer = GormCustomer.findByCustomerId(pathResult.CustomerID.text())
  579. GormClassificationGroup classificationGroup = GormClassificationGroup.findByClassificationGroupIdAndClassification(pathResult.ClassificationGroupID.text(),
  580. classification)
  581. if(!supplier || !customer || !classificationGroup) {
  582. return "failed"
  583. }
  584. GormSupplier2ClassificationGroup supplier2ClassificationGroup = GormSupplier2ClassificationGroup.findByClassificationGroupAndCustomerAndSupplier(
  585. classificationGroup,customer,supplier)
  586. if(!supplier2ClassificationGroup && updateType == "update") {
  587. return "failed"
  588. } else if(supplier2ClassificationGroup && (updateType == "update" || updateType == "insert-update")) {
  589. return "update"
  590. } else {
  591. supplier2ClassificationGroup = new GormSupplier2ClassificationGroup(supplier: supplier, customer: customer, classificationGroup: classificationGroup)
  592. supplier2ClassificationGroup.save(flush:true)
  593. return "insert"
  594. }
  595. }
  596. }
  597.  
  598. Closure insertOrUpdateSupplierProvisioning = { GPathResult pathResult ->
  599. GormSupplierProvisioning.withTransaction {
  600. String actionType = "undefined"
  601. if (!pathResult.SupplierId.text() || !pathResult.CustomerId.text()) {
  602. return "failed"
  603. }
  604.  
  605. GormSupplier supplier = GormSupplier.findBySupplierId(pathResult.SupplierID.text())
  606. GormCustomer customer = GormCustomer.findByCustomerId(pathResult.AddressID.text())
  607. if (!supplier || !customer) {
  608. return "failed"
  609. }
  610. GormSupplierProvisioning supplierProvisioning = null
  611. if (updateType == "insert-update") {
  612. supplierProvisioning = GormSupplierProvisioning.findBySupplierAndCustomer(supplier, customer)
  613. if (!supplierProvisioning) {
  614. supplierProvisioning = new GormSupplierProvisioning(supplier: supplier, customer: customer)
  615. supplierProvisioning = supplierProvisioning.save()
  616. actionType = "insert"
  617. } else {
  618. actionType = "update"
  619. }
  620. } else if (updateType == "insert") {
  621. supplierProvisioning = new GormSupplierProvisioning(supplier: supplier, customer: customer)
  622. supplierProvisioning = supplierProvisioning.save()
  623. actionType = "insert"
  624. } else if (updateType == "update") {
  625. supplierProvisioning = supplierProvisioning = GormSupplierProvisioning.findBySupplierAndCustomer(supplier, customer)
  626. if (!supplierProvisioning) {
  627. return "failed"
  628. }
  629. actionType = "update"
  630. }
  631.  
  632. supplierProvisioning.save(flush: true)
  633. return actionType
  634. }
  635. }
  636.  
  637. def deleteCatalog = delete(GormProductCatalog, "from GormProductCatalog as c where c.productCatalogId=:ProductCatalogID", ["ProductCatalogID"])
  638. def deleteCatalogUsage = delete(GormCatalogUsage, "from GormCatalogUsage as c where c.productCatalogId=:ProductCatalogID and cu.customer.customerId=:CustomerID ", ["ProductCatalogID", "CustomerID"])
  639. def deleteCatalogProvisioning = delete(GormCatalogUsage, "from GormCatalogUsage as c where c.productCatalogId=:ProductCatalogID and cu.customer.customerId=:CustomerID ", ["ProductCatalogID", "CustomerID"])
  640. def deleteContract = delete(GormContract, "from GormContract as c where c.contractId=:ContractID", ["ContractID"])
  641. def deleteContractDesc = delete(GormContractTranslation, "from GormContractTranslation as ct where ct.contract.contractId=:ContractID and ct.language.id=:LanguageID and ct.descLong=:DescLong", ["ContractID", "LanguageID", "DescLong"])
  642.  
  643.  
  644. Map closureMap = [
  645. "ProductCatalog":[insertUpdate: insertOrUpdateCatalog, delete: deleteCatalog],
  646. "CatalogUsage":[insertUpdate:insertOrUpdateCatalogUsage, delete: deleteCatalogUsage],
  647. "CatalogProvisioning": [insertUpdate:insertOrUpdateCatalogProvisioning, delete:deleteCatalogProvisioning],
  648. "Contract": [insertUpdate:insertOrUpdateCustomer, delete:deleteContract],
  649. "ContractDesc": [insertUpdate:insertOrUpdateCustomer2Address, delete:deleteContractDesc],
  650. "ContractedCatalog": [insertUpdate:insertOrUpdateClassificationUsage, delete:deleteClassificationUsage],
  651. "ContractedClassificationGroup": [insertUpdate:insertOrUpdateSupplierGroup, delete:deleteSupplierGroup],
  652. "ContractUsage": [insertUpdate:insertOrUpdateSupplier, delete:deleteSupplier],
  653. "ContractProvisioning": [insertUpdate:insertOrUpdateSupplier2Address, delete:deleteSupplier2Address],
  654. "ContractEntitlement": [insertUpdate:insertOrUpdateSupplier2ClassificationGroup, delete:deleteSupplier2ClassificationGroup],
  655. "ContractAttributeValue": [insertUpdate:insertOrUpdateSupplierProvisioning, delete:deleteSupplierProvisioning],
  656. "ContractCategory": [insertUpdate:insertOrUpdateSupplierProvisioning, delete:deleteSupplierProvisioning],
  657. "CatalogAttributeValue": [insertUpdate:insertOrUpdateSupplierProvisioning, delete:deleteSupplierProvisioning],
  658. "CatalogCategory": [insertUpdate:insertOrUpdateSupplierProvisioning, delete:deleteSupplierProvisioning],
  659. ]
  660.  
  661.  
  662. List filesList = ["ProductCatalog", "Address", "CustomerGroup", "Customer","Customer2Address",
  663. "ClassificationUsage", "SupplierGroup", "Supplier", "Supplier2Address","Supplier2ClassificationGroup",
  664. "SupplierProvisioning",
  665. // "SupplierProfile", "SupplierConnectivity","SupplierAttributeValue"
  666. ]
  667.  
  668.  
  669.  
  670. Map name2ClassName = ["ProductCatalog": GormProductCatalog, "Address":GormAddress, "CustomerGroup":GormCustomerGroup,
  671. "Customer":GormCustomer,"Customer2Address":GormCustomer2Address,
  672. "ClassificationUsage":GormClassificationUsage,
  673. "SupplierGroup":GormSupplierGroup, "Supplier":GormSupplier,
  674. "Supplier2Address":GormSupplier2Address,"Supplier2ClassificationGroup":GormSupplier2ClassificationGroup,
  675. "SupplierProvisioning":GormSupplierProvisioning,
  676. // todo ask about gorm supplier profile and SupplierConnectivity
  677. // "SupplierProfile":GormSupplier,"SupplierConnectivity":GormSupplierConnectivity,"SupplierAttributeValue":GormSupplier
  678. ]
  679.  
  680. Map flatDataMap = FlatMLImportUtils.getFlatFilesDataMap(flatMLDir,filesList, logWriter)
  681.  
  682. // read data and import to DB
  683. // todo do I need to check files existence ?
  684. for(String fileName : filesList) {
  685. XMLStreamReader reader = flatDataMap[fileName].reader
  686. GPathResult pathResult = FlatMLImportUtils.readRowElement(reader)
  687. while(pathResult) {
  688. if (updateType == "replace") {
  689. FlatMLImportUtils.replaceGormDataObject(fileName, pathResult, closureMap, name2ClassName,
  690. importStatistic, logWriter)
  691. } else if (updateType == "delete"){
  692. FlatMLImportUtils.deleteGormDataObject(fileName, pathResult, closureMap, name2ClassName,
  693. importStatistic, logWriter)
  694. }else {
  695. FlatMLImportUtils.insertOrUpdateGormDataObject(fileName, pathResult, closureMap, name2ClassName,
  696. importStatistic, logWriter, updateType)
  697. }
  698. pathResult = FlatMLImportUtils.readRowElement(reader)
  699. }
  700. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement