Add dbresource service
Showing
1 changed file
with
231 additions
and
1 deletions
... | @@ -13,7 +13,6 @@ along with this software (see the LICENSE.md file). If not, see | ... | @@ -13,7 +13,6 @@ along with this software (see the LICENSE.md file). If not, see |
13 | <http://creativecommons.org/publicdomain/zero/1.0/>. | 13 | <http://creativecommons.org/publicdomain/zero/1.0/>. |
14 | --> | 14 | --> |
15 | <services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/service-definition-3.xsd"> | 15 | <services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/service-definition-3.xsd"> |
16 | |||
17 | <service verb="get" noun="GlobalGrapeVarList"> | 16 | <service verb="get" noun="GlobalGrapeVarList"> |
18 | <out-parameters> | 17 | <out-parameters> |
19 | <parameter name="baseLinkUrl"/> | 18 | <parameter name="baseLinkUrl"/> |
... | @@ -501,4 +500,235 @@ Could have a generic implementation that uses a hierarchical conditional structu | ... | @@ -501,4 +500,235 @@ Could have a generic implementation that uses a hierarchical conditional structu |
501 | <if condition="!htmlFile || !grapesFile"><return/></if> | 500 | <if condition="!htmlFile || !grapesFile"><return/></if> |
502 | </actions> | 501 | </actions> |
503 | </service> | 502 | </service> |
503 | |||
504 | <service verb="flush" noun="PublishedEmailsToDisk" authenticate="anonymous-all"> | ||
505 | <description>Given a productStoreId, find the PsstMjmlSaveLocation. If there isn't one error | ||
506 | Find the email templates in the productStore, for each email template in the product store, get the dbresource of the published content both json and html. Then save those files byte for byte to the PsstSaveLocation/dbresourcename, continue</description> | ||
507 | <in-parameters> | ||
508 | <parameter name="productStoreId" required="true"/> | ||
509 | </in-parameters> | ||
510 | <out-parameters> | ||
511 | <parameter name="productStoreId"/> | ||
512 | <parameter name="saveLocation"/> | ||
513 | </out-parameters> | ||
514 | <actions> | ||
515 | <entity-find entity-name="mantle.product.store.ProductStoreSetting" list="locationStoreSettingList"> | ||
516 | <econdition field-name="productStoreId"/> | ||
517 | <econdition field-name="settingTypeEnumId" value="PsstMjmlSaveLocation"/> | ||
518 | <econdition field-name="settingValue" operator="is-not-null"/> | ||
519 | <econdition field-name="settingValue" operator="not-equals" value=""/> | ||
520 | <date-filter/> | ||
521 | <order-by field-name="-fromDate"/> | ||
522 | </entity-find> | ||
523 | <set field="saveLocation" from="ec.resource.getLocationReference(locationStoreSettingList.getFirst().settingValue)"/> | ||
524 | <if condition="!saveLocation"><return message="No save location found for productStoreId ${productStoreId}"/></if> | ||
525 | <entity-find entity-name="mantle.product.store.ProductStoreEmailDetail" list="storeEmails"> | ||
526 | <econdition field-name="productStoreId"/> | ||
527 | </entity-find> | ||
528 | <iterate list="storeEmails" entry="storeEmail"> | ||
529 | <set field="htmlLocation" from="storeEmail.htmlLocation"/> | ||
530 | <set field="grapesLocation" from="storeEmail.grapesLocation"/> | ||
531 | <set field="htmlFile" from="ec.resource.getLocationReference(htmlLocation)"/> | ||
532 | <if condition="!htmlFile"> | ||
533 | <log level="warn" message="No html file found for emailTemplateId ${storeEmail.emailTemplateId} at ${htmlLocation}"/> | ||
534 | <continue/> | ||
535 | </if> | ||
536 | <if condition="!storeEmail.htmlPublishedVersionName"> | ||
537 | <log level="warn" message="No html published version name found for emailTemplateId ${storeEmail.emailTemplateId} at ${htmlLocation}"/> | ||
538 | <continue/> | ||
539 | </if> | ||
540 | <set field="htmlFile" from="htmlFile.getVersion(storeEmail.htmlPublishedVersionName)?.ref"/> | ||
541 | <if condition="!htmlFile"> | ||
542 | <log level="warn" message="No html file found for emailTemplateId ${storeEmail.emailTemplateId} at ${htmlLocation} with published version name ${storeEmail.htmlPublishedVersionName}"/> | ||
543 | <continue/> | ||
544 | </if> | ||
545 | <set field="grapesFile" from="ec.resource.getLocationReference(grapesLocation)"/> | ||
546 | <if condition="!grapesFile"> | ||
547 | <log level="warn" message="No grapes file found for emailTemplateId ${storeEmail.emailTemplateId} at ${grapesLocation}"/> | ||
548 | <continue/> | ||
549 | </if> | ||
550 | <if condition="!storeEmail.grapesPublishedVersionName"> | ||
551 | <log level="warn" message="No grapes published version name found for emailTemplateId ${storeEmail.emailTemplateId} at ${grapesLocation}"/> | ||
552 | <continue/> | ||
553 | </if> | ||
554 | <set field="grapesFile" from="grapesFile.getVersion(storeEmail.grapesPublishedVersionName)?.ref"/> | ||
555 | <if condition="!grapesFile"> | ||
556 | <log level="warn" message="No grapes file found for emailTemplateId ${storeEmail.emailTemplateId} at ${grapesLocation} with published version name ${storeEmail.grapesPublishedVersionName}"/> | ||
557 | <continue/> | ||
558 | </if> | ||
559 | <set field="htmlText" from="htmlFile?.getText()"/> | ||
560 | <set field="grapesText" from="grapesFile?.getText()"/> | ||
561 | <set field="outHtmlFile" from="saveLocation.getChild(htmlFile.getFileName())"/> | ||
562 | <set field="outHtmlLocation" from="outHtmlFile.location"/> | ||
563 | <log message="Saving html to ${outHtmlFile.location}"/> | ||
564 | <script><![CDATA[outHtmlFile.putText(htmlText)]]></script> | ||
565 | <set field="outGrapesFile" from="saveLocation.getChild(grapesFile.getFileName())"/> | ||
566 | <set field="outGrapesLocation" from="outGrapesFile.location"/> | ||
567 | <log message="Saving grapes to ${outGrapesFile.location}"/> | ||
568 | <script><![CDATA[outGrapesFile.putText(grapesText)]]></script> | ||
569 | </iterate> | ||
570 | |||
571 | </actions> | ||
572 | </service> | ||
573 | |||
574 | <service verb="get" noun="ZipOfResourceReference"> | ||
575 | <in-parameters> | ||
576 | <parameter name="location" required="true"/> | ||
577 | </in-parameters> | ||
578 | <out-parameters> | ||
579 | <parameter name="zipFilePath" type="String"/> | ||
580 | <!-- <parameter name="zipFile" type="" --> | ||
581 | </out-parameters> | ||
582 | <actions> | ||
583 | <script> | ||
584 | import java.io.*; | ||
585 | import java.util.zip.ZipEntry; | ||
586 | import java.util.zip.ZipOutputStream; | ||
587 | import org.moqui.resource.ResourceReference; | ||
588 | </script> | ||
589 | <set field="resourceReference" from="ec.resource.getLocationReference(location)"/> | ||
590 | |||
591 | <set field="tempDir" from="ec.resource.getLocationReference(ec.factory.getRuntimePath() + '/tmp')"/> | ||
592 | <set field="zipFile" from="tempDir.makeFile(resourceReference.getFileName() + '.zip')"/> | ||
593 | <set field="zipFilePath" from="zipFile.location"/> | ||
594 | <set field="filesToZip" from="resourceReference.getChildren()"/> | ||
595 | <script><![CDATA[ | ||
596 | try { | ||
597 | // Write directly to the ResourceReference's output stream | ||
598 | OutputStream fos = zipFile.getOutputStream() | ||
599 | ZipOutputStream zos = new ZipOutputStream(fos) | ||
600 | |||
601 | for (org.moqui.resource.ResourceReference file : filesToZip) { | ||
602 | ZipEntry entry = new ZipEntry(file.getFileName()) | ||
603 | zos.putNextEntry(entry) | ||
604 | |||
605 | InputStream fis = file.openStream() | ||
606 | byte[] buffer = new byte[4096] | ||
607 | int bytesRead | ||
608 | while ((bytesRead = fis.read(buffer)) != -1) { | ||
609 | zos.write(buffer, 0, bytesRead) | ||
610 | } | ||
611 | fis.close() | ||
612 | zos.closeEntry() | ||
613 | } | ||
614 | |||
615 | zos.flush() | ||
616 | zos.close() | ||
617 | fos.close() | ||
618 | } catch (IOException e) { | ||
619 | logger.error("Error creating zip file", e) | ||
620 | } catch (URISyntaxException e) { | ||
621 | logger.error("Error parsing file path", e) | ||
622 | } | ||
623 | ]]></script> | ||
624 | |||
625 | <!-- <set field="zipFileFile" from="zipFile. --> | ||
626 | </actions> | ||
627 | </service> | ||
628 | |||
629 | <service verb="forceUpdate" noun="DiskToDbResourceAndPublish"> | ||
630 | <in-parameters> | ||
631 | <parameter name="productStoreId" required="true"/> | ||
632 | <parameter name="updateEmailTemplate" type="Boolean" default="true"/> | ||
633 | </in-parameters> | ||
634 | <actions> | ||
635 | <!-- Find save location --> | ||
636 | <entity-find entity-name="mantle.product.store.ProductStoreSetting" list="saveLocationList"> | ||
637 | <econdition field-name="productStoreId"/> | ||
638 | <econdition field-name="settingTypeEnumId" value="PsstMjmlSaveLocation"/> | ||
639 | <econdition field-name="settingValue" operator="is-not-null"/> | ||
640 | <econdition field-name="settingValue" operator="not-equals" value=""/> | ||
641 | <date-filter/> | ||
642 | <order-by field-name="-fromDate"/> | ||
643 | </entity-find> | ||
644 | <if condition="saveLocationList.size() == 0"><return message="No save location found for productStoreId ${productStoreId}"/></if> | ||
645 | <set field="saveLocation" from="saveLocationList.getFirst()"/> | ||
646 | |||
647 | <!-- Get resource reference for save location --> | ||
648 | <set field="saveLocationRef" from="ec.resource.getLocationReference(saveLocation.settingValue)"/> | ||
649 | <if condition="!saveLocationRef.exists || !saveLocationRef.isDirectory()"> | ||
650 | <return message="Save location ${saveLocation.settingValue} does not exist or is not a directory"/> | ||
651 | </if> | ||
652 | |||
653 | <!-- Get all files in save location --> | ||
654 | <set field="children" from="saveLocationRef.getChildren()"/> | ||
655 | |||
656 | <set field="updatedEmailTemplateList" from="[]"/> | ||
657 | <iterate list="children" entry="child"> | ||
658 | <script><![CDATA[ | ||
659 | // Find parent path up to dbresource | ||
660 | def currentRef = child | ||
661 | def dbResourcePath = "" | ||
662 | while(currentRef != null) { | ||
663 | def parentRef = currentRef.getParent() | ||
664 | if(parentRef == null || parentRef.getFileName() == "dbresource") { | ||
665 | dbResourcePath = parentRef.getLocation() | ||
666 | break | ||
667 | } | ||
668 | currentRef = parentRef | ||
669 | } | ||
670 | ]]></script> | ||
671 | |||
672 | <set field="dbResource" from="ec.resource.getLocationReference(dbResourcePath)"/> | ||
673 | <if condition="!dbResource"><continue/></if> | ||
674 | |||
675 | <set field="dbResourceToUpdatePath" value="dbresource:/${child.location - dbResource.location}"/> | ||
676 | <set field="dbResourceToUpdate" from="ec.resource.getLocationReference(dbResourceToUpdatePath)"/> | ||
677 | |||
678 | <if condition="!dbResourceToUpdate.exists || !dbResourceToUpdate.isFile()"> | ||
679 | <log level="warn" message="dbResourceToUpdate ${dbResourceToUpdate.location} does not exist or is not a file"/> | ||
680 | <continue/> | ||
681 | </if> | ||
682 | |||
683 | <set field="dbResourceToUpdateText" from="dbResourceToUpdate.getText()"/> | ||
684 | <set field="childText" from="child.getText()"/> | ||
685 | |||
686 | <if condition="dbResourceToUpdateText != childText"><then> | ||
687 | <script><![CDATA[ | ||
688 | dbResourceToUpdate.putText(childText) | ||
689 | ]]></script> | ||
690 | <set field="versionName" from="dbResourceToUpdate.getCurrentVersion()?.getVersionName()"/> | ||
691 | <if condition="!versionName"> | ||
692 | <log level="warn" message="No version name found for dbresource ${dbResourceToUpdate.location}"/> | ||
693 | <continue/> | ||
694 | </if> | ||
695 | <if condition="updateEmailTemplate"> | ||
696 | <entity-find entity-name="moqui.basic.email.EmailTemplate" list="emailTemplateList" for-update="true"> | ||
697 | <econditions combine="or"> | ||
698 | <econdition field-name="grapesLocation" from="dbResourceToUpdate.location"/> | ||
699 | <econdition field-name="htmlLocation" from="dbResourceToUpdate.location"/> | ||
700 | </econditions> | ||
701 | <select-field field-name="emailTemplateId,grapesPublishedVersionName,htmlPublishedVersionName,grapesLocation,htmlLocation"/> | ||
702 | <order-by field-name="-lastUpdatedStamp"/> | ||
703 | </entity-find> | ||
704 | <if condition="emailTemplateList.size() == 0"> | ||
705 | <log level="warn" message="No email template found for dbresource ${dbResourceToUpdate.location}"/> | ||
706 | <continue/> | ||
707 | </if> | ||
708 | |||
709 | <iterate list="emailTemplateList" entry="emailTemplate"> | ||
710 | <set field="logText" value="At emailTemplate ${emailTemplate.emailTemplateId},"/> | ||
711 | <if condition="emailTemplate.grapesLocation == dbResourceToUpdate.location"> | ||
712 | <set field="emailTemplate.grapesPublishedVersionName" from="versionName"/> | ||
713 | <set field="updatedEmailTemplateList" from="updatedEmailTemplateList + [emailTemplate:emailTemplate,location:emailTemplate.grapesLocation,versionName:versionName,grapesOrHtml:'grapes']"/> | ||
714 | <set field="updatedEmailTemplate" from="true"/> | ||
715 | <set field="logText" from="logText + ' updating grapesPublishedVersionName to ' + versionName"/> | ||
716 | </if> | ||
717 | <if condition="emailTemplate.htmlLocation == dbResourceToUpdate.location"> | ||
718 | <set field="emailTemplate.htmlPublishedVersionName" from="versionName"/> | ||
719 | <set field="updatedEmailTemplateList" from="updatedEmailTemplateList + [emailTemplate:emailTemplate,location:emailTemplate.htmlLocation,versionName:versionName,grapesOrHtml:'html']"/> | ||
720 | <set field="updatedEmailTemplate" from="true"/> | ||
721 | <set field="logText" from="logText + ' updating htmlPublishedVersionName to ' + versionName"/> | ||
722 | </if> | ||
723 | <log level="info" message="${logText}"/> | ||
724 | <entity-update value-field="emailTemplate"/> | ||
725 | </iterate> | ||
726 | </if> | ||
727 | </then><else> | ||
728 | <log level="info" message="Not updating from disk because file at ${child.location} and dbresource at ${dbResourceToUpdate.location} are the same"/> | ||
729 | </else></if> | ||
730 | |||
731 | </iterate> | ||
732 | </actions> | ||
733 | </service> | ||
504 | </services> | 734 | </services> | ... | ... |
-
Please register or sign in to post a comment