|
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; |
7 | 8 | import org.jetbrains.annotations.NotNull; |
| 9 | +import org.jetbrains.annotations.Nullable; |
8 | 10 | import org.json.simple.JSONObject; |
9 | 11 | import retrofit2.Call; |
10 | 12 | import retrofit2.Retrofit; |
@@ -68,6 +70,56 @@ private void validateCT() { |
68 | 70 | Objects.requireNonNull(this.contentTypeUid, ERROR_CT_UID); |
69 | 71 | } |
70 | 72 |
|
| 73 | + private void validateVariantUid(@NotNull String variantUid) { |
| 74 | + Objects.requireNonNull(variantUid, ErrorMessages.VARIANT_UID_REQUIRED); |
| 75 | + if (variantUid.trim().isEmpty()) { |
| 76 | + throw new IllegalArgumentException(ErrorMessages.VARIANT_UID_REQUIRED); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Header map for a variant request when {@code branchUid} is supplied for this call only (does not mutate {@link #headers}). |
| 82 | + * Null or blank {@code branchUid} keeps {@link #headers} as-is (stack / {@link #addBranch(String)} behavior). |
| 83 | + */ |
| 84 | + private Map<String, Object> variantHeadersWithOptionalBranch(@Nullable String branchUid) { |
| 85 | + if (branchUid == null || branchUid.trim().isEmpty()) { |
| 86 | + return this.headers; |
| 87 | + } |
| 88 | + HashMap<String, Object> copy = new HashMap<>(this.headers); |
| 89 | + copy.put(Util.BRANCH, branchUid); |
| 90 | + return copy; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Sets the branch header for requests scoped to a stack branch (e.g. development). |
| 95 | + * Overrides the branch set via {@link com.contentstack.cms.Contentstack#stack(String, String, String)} for this |
| 96 | + * {@link Entry} instance only (including entry-variant CRUD, publish, and unpublish). Uses header key {@value Util#BRANCH}. |
| 97 | + * |
| 98 | + * @param branchUid branch UID or alias target branch UID |
| 99 | + * @return this entry instance for chaining |
| 100 | + */ |
| 101 | + public Entry addBranch(@NotNull String branchUid) { |
| 102 | + Objects.requireNonNull(branchUid, ErrorMessages.BRANCH_UID_REQUIRED); |
| 103 | + if (branchUid.trim().isEmpty()) { |
| 104 | + throw new IllegalArgumentException(ErrorMessages.BRANCH_UID_REQUIRED); |
| 105 | + } |
| 106 | + this.headers.put(Util.BRANCH, branchUid); |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Sets {@value Util#X_CS_VARIANT_UID} for {@link #fetch()} / {@link #fetchAsPojo()} to retrieve the base entry with a |
| 112 | + * specific variant applied (personalization). |
| 113 | + * |
| 114 | + * @param variantUid Content variant UID (e.g. {@code cs…}) |
| 115 | + * @return this entry instance for chaining |
| 116 | + */ |
| 117 | + public Entry withAppliedVariantUid(@NotNull String variantUid) { |
| 118 | + validateVariantUid(variantUid); |
| 119 | + this.headers.put(Util.X_CS_VARIANT_UID, variantUid); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
71 | 123 | /** |
72 | 124 | * Sets header for the request |
73 | 125 | * |
@@ -710,6 +762,102 @@ public Call<ResponseBody> importExisting() { |
710 | 762 | return this.service.importExisting(this.headers, this.contentTypeUid, this.entryUid, this.params); |
711 | 763 | } |
712 | 764 |
|
| 765 | + /** |
| 766 | + * Retrieves all entry variants for this entry. |
| 767 | + * <p> |
| 768 | + * Use {@link #addParam(String, Object)} for optional queries such as {@code locale}, {@code include_workflow}. |
| 769 | + * Branch scope: stack {@value Util#BRANCH} from {@link com.contentstack.cms.Contentstack#stack(String, String, String)} |
| 770 | + * is forwarded; {@link #addBranch(String)} overrides for this entry only. |
| 771 | + * |
| 772 | + * @return Retrofit call for GET …/entries/{entry_uid}/variants |
| 773 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-entry-variants">Get all entry variants</a> |
| 774 | + */ |
| 775 | + public Call<ResponseBody> fetchEntryVariants() { |
| 776 | + validateCT(); |
| 777 | + validateEntry(); |
| 778 | + return this.service.fetchEntryVariants(this.headers, this.contentTypeUid, this.entryUid, this.params); |
| 779 | + } |
| 780 | + |
| 781 | + /** |
| 782 | + * Retrieves a single entry variant using {@link #headers} for {@value Util#BRANCH} (stack default and/or {@link #addBranch(String)}). |
| 783 | + * |
| 784 | + * @param variantUid variant UID path segment |
| 785 | + * @return Retrofit call for GET …/variants/{variant_uid} |
| 786 | + * @see #fetchEntryVariant(String, String) |
| 787 | + */ |
| 788 | + public Call<ResponseBody> fetchEntryVariant(@NotNull String variantUid) { |
| 789 | + return fetchEntryVariant(variantUid, null); |
| 790 | + } |
| 791 | + |
| 792 | + /** |
| 793 | + * Retrieves a single entry variant with an optional per-call {@value Util#BRANCH} override. |
| 794 | + * <p> |
| 795 | + * When {@code branchUid} is non-blank, it replaces {@value Util#BRANCH} on this request only (stack and {@link #addBranch(String)} |
| 796 | + * values are not mutated on the entry). When {@code branchUid} is {@code null} or blank, behavior matches {@link #fetchEntryVariant(String)}. |
| 797 | + * {@link #withAppliedVariantUid(String)} ({@value Util#X_CS_VARIANT_UID}) is unrelated to branch. |
| 798 | + * |
| 799 | + * @param variantUid variant UID path segment |
| 800 | + * @param branchUid optional branch UID or alias for this request only; {@code null} or empty to use entry headers |
| 801 | + * @return Retrofit call for GET …/variants/{variant_uid} |
| 802 | + */ |
| 803 | + public Call<ResponseBody> fetchEntryVariant(@NotNull String variantUid, @Nullable String branchUid) { |
| 804 | + validateCT(); |
| 805 | + validateEntry(); |
| 806 | + validateVariantUid(variantUid); |
| 807 | + return this.service.fetchEntryVariant(variantHeadersWithOptionalBranch(branchUid), this.contentTypeUid, |
| 808 | + this.entryUid, variantUid, this.params); |
| 809 | + } |
| 810 | + |
| 811 | + /** |
| 812 | + * Creates an entry variant. Uses PUT …/variants/{variant_uid} (CMA upsert — same URL as {@link #updateEntryVariant}). |
| 813 | + * <p> |
| 814 | + * Branch scope: inherits stack {@value Util#BRANCH}; override with {@link #addBranch(String)} or {@link #addHeader(String, String)} |
| 815 | + * ({@value Util#BRANCH}) on this entry. Variant personalization header {@value Util#X_CS_VARIANT_UID} is orthogonal. |
| 816 | + * |
| 817 | + * @param variantUid variant UID path segment |
| 818 | + * @param requestBody JSON body per API (typically wraps fields under {@code entry}) |
| 819 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#create-entry-variant">Create Entry Variant</a> |
| 820 | + */ |
| 821 | + public Call<ResponseBody> createEntryVariant(@NotNull String variantUid, @NotNull JSONObject requestBody) { |
| 822 | + validateCT(); |
| 823 | + validateEntry(); |
| 824 | + validateVariantUid(variantUid); |
| 825 | + return this.service.createEntryVariant(this.headers, this.contentTypeUid, this.entryUid, variantUid, this.params, |
| 826 | + requestBody); |
| 827 | + } |
| 828 | + |
| 829 | + /** |
| 830 | + * Updates an entry variant. Same HTTP request shape as create (PUT upsert). |
| 831 | + * <p> |
| 832 | + * Branch scope: inherits stack {@value Util#BRANCH}; override with {@link #addBranch(String)} or {@link #addHeader(String, String)} |
| 833 | + * ({@value Util#BRANCH}) on this entry. |
| 834 | + * |
| 835 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#update-entry-variant">Update Entry Variant</a> |
| 836 | + */ |
| 837 | + public Call<ResponseBody> updateEntryVariant(@NotNull String variantUid, @NotNull JSONObject requestBody) { |
| 838 | + validateCT(); |
| 839 | + validateEntry(); |
| 840 | + validateVariantUid(variantUid); |
| 841 | + return this.service.updateEntryVariant(this.headers, this.contentTypeUid, this.entryUid, variantUid, this.params, |
| 842 | + requestBody); |
| 843 | + } |
| 844 | + |
| 845 | + /** |
| 846 | + * Deletes an entry variant. |
| 847 | + * <p> |
| 848 | + * Branch scope: inherits stack {@value Util#BRANCH}; override with {@link #addBranch(String)} or {@link #addHeader(String, String)} |
| 849 | + * ({@value Util#BRANCH}) on this entry. |
| 850 | + * |
| 851 | + * @param variantUid variant UID path segment |
| 852 | + * @return Retrofit call for DELETE …/variants/{variant_uid} |
| 853 | + */ |
| 854 | + public Call<ResponseBody> deleteEntryVariant(@NotNull String variantUid) { |
| 855 | + validateCT(); |
| 856 | + validateEntry(); |
| 857 | + validateVariantUid(variantUid); |
| 858 | + return this.service.deleteEntryVariant(this.headers, this.contentTypeUid, this.entryUid, variantUid, this.params); |
| 859 | + } |
| 860 | + |
713 | 861 | /** |
714 | 862 | * To Publish an entry request lets you publish an entry either immediately or |
715 | 863 | * schedule it for a later date/time. |
@@ -752,7 +900,25 @@ public Call<ResponseBody> importExisting() { |
752 | 900 | public Call<ResponseBody> publish(@NotNull JSONObject requestBody) { |
753 | 901 | validateCT(); |
754 | 902 | validateEntry(); |
755 | | - return this.service.publish(this.headers, this.contentTypeUid, this.entryUid, requestBody); |
| 903 | + return this.service.publish(this.headers, this.contentTypeUid, this.entryUid, this.params, requestBody); |
| 904 | + } |
| 905 | + |
| 906 | + /** |
| 907 | + * Publishes entry variants using the entry publish endpoint with {@code entry.variants} in the body. |
| 908 | + * Sends header {@value Util#API_VERSION}={@value Util#API_VERSION_ENTRY_VARIANTS_PUBLISH} unless already set on this entry instance. |
| 909 | + * Use {@link #addParam(String, Object)} for optional {@code locale} query parameter. |
| 910 | + * <p> |
| 911 | + * Branch scope: stack {@value Util#BRANCH} is copied into the publish request headers together with {@code api_version}; |
| 912 | + * override with {@link #addBranch(String)} or {@link #addHeader(String, String)} ({@value Util#BRANCH}) on this entry. |
| 913 | + * |
| 914 | + * @param requestBody full publish payload including {@code entry}, {@code locale}, etc. |
| 915 | + */ |
| 916 | + public Call<ResponseBody> publishEntryVariants(@NotNull JSONObject requestBody) { |
| 917 | + validateCT(); |
| 918 | + validateEntry(); |
| 919 | + HashMap<String, Object> publishHeaders = new HashMap<>(this.headers); |
| 920 | + publishHeaders.putIfAbsent(Util.API_VERSION, Util.API_VERSION_ENTRY_VARIANTS_PUBLISH); |
| 921 | + return this.service.publish(publishHeaders, this.contentTypeUid, this.entryUid, this.params, requestBody); |
756 | 922 | } |
757 | 923 |
|
758 | 924 | /** |
@@ -816,9 +982,23 @@ public Call<ResponseBody> publishWithReference(@NotNull JSONObject requestBody) |
816 | 982 | public Call<ResponseBody> unpublish(@NotNull JSONObject requestBody) { |
817 | 983 | validateCT(); |
818 | 984 | validateEntry(); |
819 | | - return this.service.unpublish(this.headers, this.contentTypeUid, this.entryUid, requestBody); |
| 985 | + return this.service.unpublish(this.headers, this.contentTypeUid, this.entryUid, this.params, requestBody); |
820 | 986 | } |
821 | 987 |
|
| 988 | + /** |
| 989 | + * Unpublishes entry variants via the entry unpublish endpoint with {@code entry.variants} in the body. |
| 990 | + * Sends header {@value Util#API_VERSION}={@value Util#API_VERSION_ENTRY_VARIANTS_PUBLISH} unless already set. |
| 991 | + * <p> |
| 992 | + * Branch scope: stack {@value Util#BRANCH} is forwarded; override with {@link #addBranch(String)} or {@link #addHeader(String, String)} |
| 993 | + * ({@value Util#BRANCH}) on this entry. |
| 994 | + */ |
| 995 | + public Call<ResponseBody> unpublishEntryVariants(@NotNull JSONObject requestBody) { |
| 996 | + validateCT(); |
| 997 | + validateEntry(); |
| 998 | + HashMap<String, Object> unpublishHeaders = new HashMap<>(this.headers); |
| 999 | + unpublishHeaders.putIfAbsent(Util.API_VERSION, Util.API_VERSION_ENTRY_VARIANTS_PUBLISH); |
| 1000 | + return this.service.unpublish(unpublishHeaders, this.contentTypeUid, this.entryUid, this.params, requestBody); |
| 1001 | + } |
822 | 1002 |
|
823 | 1003 | /** |
824 | 1004 | * Get instance of taxonomy search filter class instance through which we can query on taxonomy based on content type |
|
0 commit comments