|
1 | 1 | package com.contentstack.cms.stack; |
2 | 2 |
|
3 | 3 | import com.contentstack.cms.core.ErrorMessages; |
| 4 | +import com.contentstack.cms.core.Util; |
4 | 5 |
|
5 | 6 | import com.contentstack.cms.BaseImplementation; |
6 | 7 | import okhttp3.ResponseBody; |
@@ -68,6 +69,37 @@ private void validateCT() { |
68 | 69 | Objects.requireNonNull(this.contentTypeUid, ERROR_CT_UID); |
69 | 70 | } |
70 | 71 |
|
| 72 | + private void validateVariantUid(@NotNull String variantUid) { |
| 73 | + Objects.requireNonNull(variantUid, ErrorMessages.VARIANT_UID_REQUIRED); |
| 74 | + if (variantUid.isEmpty()) { |
| 75 | + throw new IllegalArgumentException(ErrorMessages.VARIANT_UID_REQUIRED); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Sets the branch header for requests scoped to a stack branch (e.g. development). |
| 81 | + * |
| 82 | + * @param branchUid branch UID or alias target branch UID |
| 83 | + * @return this entry instance for chaining |
| 84 | + */ |
| 85 | + public Entry addBranch(@NotNull String branchUid) { |
| 86 | + this.headers.put(Util.BRANCH, branchUid); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Sets {@value Util#X_CS_VARIANT_UID} for {@link #fetch()} / {@link #fetchAsPojo()} to retrieve the base entry with a |
| 92 | + * specific variant applied (personalization). |
| 93 | + * |
| 94 | + * @param variantUid Content variant UID (e.g. {@code cs…}) |
| 95 | + * @return this entry instance for chaining |
| 96 | + */ |
| 97 | + public Entry withAppliedVariantUid(@NotNull String variantUid) { |
| 98 | + validateVariantUid(variantUid); |
| 99 | + this.headers.put(Util.X_CS_VARIANT_UID, variantUid); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
71 | 103 | /** |
72 | 104 | * Sets header for the request |
73 | 105 | * |
@@ -710,6 +742,75 @@ public Call<ResponseBody> importExisting() { |
710 | 742 | return this.service.importExisting(this.headers, this.contentTypeUid, this.entryUid, this.params); |
711 | 743 | } |
712 | 744 |
|
| 745 | + /** |
| 746 | + * Retrieves all entry variants for this entry. |
| 747 | + * <p> |
| 748 | + * Use {@link #addParam(String, Object)} for optional queries such as {@code locale}, {@code include_workflow}, |
| 749 | + * {@link #addBranch(String)} or stack-level branch header for branch-scoped stacks. |
| 750 | + * |
| 751 | + * @return Retrofit call for GET …/entries/{entry_uid}/variants |
| 752 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-entry-variants">Get all entry variants</a> |
| 753 | + */ |
| 754 | + public Call<ResponseBody> fetchEntryVariants() { |
| 755 | + validateCT(); |
| 756 | + validateEntry(); |
| 757 | + return this.service.fetchEntryVariants(this.headers, this.contentTypeUid, this.entryUid, this.params); |
| 758 | + } |
| 759 | + |
| 760 | + /** |
| 761 | + * Retrieves a single entry variant. |
| 762 | + * |
| 763 | + * @param variantUid variant UID path segment |
| 764 | + * @return Retrofit call for GET …/variants/{variant_uid} |
| 765 | + */ |
| 766 | + public Call<ResponseBody> fetchEntryVariant(@NotNull String variantUid) { |
| 767 | + validateCT(); |
| 768 | + validateEntry(); |
| 769 | + validateVariantUid(variantUid); |
| 770 | + return this.service.fetchEntryVariant(this.headers, this.contentTypeUid, this.entryUid, variantUid, this.params); |
| 771 | + } |
| 772 | + |
| 773 | + /** |
| 774 | + * Creates an entry variant. Uses PUT …/variants/{variant_uid} (CMA upsert — same URL as {@link #updateEntryVariant}). |
| 775 | + * |
| 776 | + * @param variantUid variant UID path segment |
| 777 | + * @param requestBody JSON body per API (typically wraps fields under {@code entry}) |
| 778 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#create-entry-variant">Create Entry Variant</a> |
| 779 | + */ |
| 780 | + public Call<ResponseBody> createEntryVariant(@NotNull String variantUid, @NotNull JSONObject requestBody) { |
| 781 | + validateCT(); |
| 782 | + validateEntry(); |
| 783 | + validateVariantUid(variantUid); |
| 784 | + return this.service.createEntryVariant(this.headers, this.contentTypeUid, this.entryUid, variantUid, this.params, |
| 785 | + requestBody); |
| 786 | + } |
| 787 | + |
| 788 | + /** |
| 789 | + * Updates an entry variant. Same HTTP request shape as create (PUT upsert). |
| 790 | + * |
| 791 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#update-entry-variant">Update Entry Variant</a> |
| 792 | + */ |
| 793 | + public Call<ResponseBody> updateEntryVariant(@NotNull String variantUid, @NotNull JSONObject requestBody) { |
| 794 | + validateCT(); |
| 795 | + validateEntry(); |
| 796 | + validateVariantUid(variantUid); |
| 797 | + return this.service.updateEntryVariant(this.headers, this.contentTypeUid, this.entryUid, variantUid, this.params, |
| 798 | + requestBody); |
| 799 | + } |
| 800 | + |
| 801 | + /** |
| 802 | + * Deletes an entry variant. |
| 803 | + * |
| 804 | + * @param variantUid variant UID path segment |
| 805 | + * @return Retrofit call for DELETE …/variants/{variant_uid} |
| 806 | + */ |
| 807 | + public Call<ResponseBody> deleteEntryVariant(@NotNull String variantUid) { |
| 808 | + validateCT(); |
| 809 | + validateEntry(); |
| 810 | + validateVariantUid(variantUid); |
| 811 | + return this.service.deleteEntryVariant(this.headers, this.contentTypeUid, this.entryUid, variantUid, this.params); |
| 812 | + } |
| 813 | + |
713 | 814 | /** |
714 | 815 | * To Publish an entry request lets you publish an entry either immediately or |
715 | 816 | * schedule it for a later date/time. |
@@ -752,7 +853,22 @@ public Call<ResponseBody> importExisting() { |
752 | 853 | public Call<ResponseBody> publish(@NotNull JSONObject requestBody) { |
753 | 854 | validateCT(); |
754 | 855 | validateEntry(); |
755 | | - return this.service.publish(this.headers, this.contentTypeUid, this.entryUid, requestBody); |
| 856 | + return this.service.publish(this.headers, this.contentTypeUid, this.entryUid, this.params, requestBody); |
| 857 | + } |
| 858 | + |
| 859 | + /** |
| 860 | + * Publishes entry variants using the entry publish endpoint with {@code entry.variants} in the body. |
| 861 | + * Sends header {@value Util#API_VERSION}={@value Util#API_VERSION_ENTRY_VARIANTS_PUBLISH} unless already set on this entry instance. |
| 862 | + * Use {@link #addParam(String, Object)} for optional {@code locale} query parameter; use {@link #addBranch(String)} for branch scope. |
| 863 | + * |
| 864 | + * @param requestBody full publish payload including {@code entry}, {@code locale}, etc. |
| 865 | + */ |
| 866 | + public Call<ResponseBody> publishEntryVariants(@NotNull JSONObject requestBody) { |
| 867 | + validateCT(); |
| 868 | + validateEntry(); |
| 869 | + HashMap<String, Object> publishHeaders = new HashMap<>(this.headers); |
| 870 | + publishHeaders.putIfAbsent(Util.API_VERSION, Util.API_VERSION_ENTRY_VARIANTS_PUBLISH); |
| 871 | + return this.service.publish(publishHeaders, this.contentTypeUid, this.entryUid, this.params, requestBody); |
756 | 872 | } |
757 | 873 |
|
758 | 874 | /** |
@@ -816,9 +932,20 @@ public Call<ResponseBody> publishWithReference(@NotNull JSONObject requestBody) |
816 | 932 | public Call<ResponseBody> unpublish(@NotNull JSONObject requestBody) { |
817 | 933 | validateCT(); |
818 | 934 | validateEntry(); |
819 | | - return this.service.unpublish(this.headers, this.contentTypeUid, this.entryUid, requestBody); |
| 935 | + return this.service.unpublish(this.headers, this.contentTypeUid, this.entryUid, this.params, requestBody); |
820 | 936 | } |
821 | 937 |
|
| 938 | + /** |
| 939 | + * Unpublishes entry variants via the entry unpublish endpoint with {@code entry.variants} in the body. |
| 940 | + * Sends header {@value Util#API_VERSION}={@value Util#API_VERSION_ENTRY_VARIANTS_PUBLISH} unless already set. |
| 941 | + */ |
| 942 | + public Call<ResponseBody> unpublishEntryVariants(@NotNull JSONObject requestBody) { |
| 943 | + validateCT(); |
| 944 | + validateEntry(); |
| 945 | + HashMap<String, Object> unpublishHeaders = new HashMap<>(this.headers); |
| 946 | + unpublishHeaders.putIfAbsent(Util.API_VERSION, Util.API_VERSION_ENTRY_VARIANTS_PUBLISH); |
| 947 | + return this.service.unpublish(unpublishHeaders, this.contentTypeUid, this.entryUid, this.params, requestBody); |
| 948 | + } |
822 | 949 |
|
823 | 950 | /** |
824 | 951 | * Get instance of taxonomy search filter class instance through which we can query on taxonomy based on content type |
|
0 commit comments