1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.appsearch.testutil;
18 
19 import androidx.annotation.RestrictTo;
20 import androidx.appsearch.annotation.CanIgnoreReturnValue;
21 import androidx.appsearch.app.AppSearchSchema;
22 import androidx.appsearch.app.AppSearchSchema.PropertyConfig;
23 import androidx.appsearch.app.AppSearchSchema.StringPropertyConfig;
24 import androidx.appsearch.app.GenericDocument;
25 
26 import org.jspecify.annotations.NonNull;
27 import org.jspecify.annotations.Nullable;
28 
29 /**
30  * Encapsulates a {@link GenericDocument} that represent an email.
31  *
32  * <p>This class is a higher level implement of {@link GenericDocument}.
33  *
34  * @exportToFramework:hide
35  */
36 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
37 public class AppSearchEmail extends GenericDocument {
38     /** The name of the schema type for {@link AppSearchEmail} documents.*/
39     public static final String SCHEMA_TYPE = "builtin:Email";
40 
41     private static final String KEY_FROM = "from";
42     private static final String KEY_TO = "to";
43     private static final String KEY_CC = "cc";
44     private static final String KEY_BCC = "bcc";
45     private static final String KEY_SUBJECT = "subject";
46     private static final String KEY_BODY = "body";
47 
48     public static final AppSearchSchema SCHEMA = new AppSearchSchema.Builder(SCHEMA_TYPE)
49             .addProperty(new StringPropertyConfig.Builder(KEY_FROM)
50                     .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
51                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
52                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
53                     .build()
54 
55             ).addProperty(new StringPropertyConfig.Builder(KEY_TO)
56                     .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
57                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
58                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
59                     .build()
60 
61             ).addProperty(new StringPropertyConfig.Builder(KEY_CC)
62                     .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
63                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
64                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
65                     .build()
66 
67             ).addProperty(new StringPropertyConfig.Builder(KEY_BCC)
68                     .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
69                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
70                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
71                     .build()
72 
73             ).addProperty(new StringPropertyConfig.Builder(KEY_SUBJECT)
74                     .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
75                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
76                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
77                     .build()
78 
79             ).addProperty(new StringPropertyConfig.Builder(KEY_BODY)
80                     .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
81                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
82                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
83                     .build()
84 
85             ).build();
86 
87     /**
88      * Creates a new {@link AppSearchEmail} from the contents of an existing
89      * {@link GenericDocument}.
90      *
91      * @param document The {@link GenericDocument} containing the email content.
92      */
AppSearchEmail(@onNull GenericDocument document)93     public AppSearchEmail(@NonNull GenericDocument document) {
94         super(document);
95     }
96 
97     /**
98      * Gets the from address of {@link AppSearchEmail}.
99      *
100      * @return The subject of {@link AppSearchEmail} or {@code null} if it's not been set yet.
101      */
getFrom()102     public @Nullable String getFrom() {
103         return getPropertyString(KEY_FROM);
104     }
105 
106     /**
107      * Gets the destination addresses of {@link AppSearchEmail}.
108      *
109      * @return The destination addresses of {@link AppSearchEmail} or {@code null} if it's not
110      *         been set yet.
111      */
getTo()112     public String @Nullable [] getTo() {
113         return getPropertyStringArray(KEY_TO);
114     }
115 
116     /**
117      * Gets the CC list of {@link AppSearchEmail}.
118      *
119      * @return The CC list of {@link AppSearchEmail} or {@code null} if it's not been set yet.
120      */
getCc()121     public String @Nullable [] getCc() {
122         return getPropertyStringArray(KEY_CC);
123     }
124 
125     /**
126      * Gets the BCC list of {@link AppSearchEmail}.
127      *
128      * @return The BCC list of {@link AppSearchEmail} or {@code null} if it's not been set yet.
129      */
getBcc()130     public String @Nullable [] getBcc() {
131         return getPropertyStringArray(KEY_BCC);
132     }
133 
134     /**
135      * Gets the subject of {@link AppSearchEmail}.
136      *
137      * @return The value subject of {@link AppSearchEmail} or {@code null} if it's not been set yet.
138      */
getSubject()139     public @Nullable String getSubject() {
140         return getPropertyString(KEY_SUBJECT);
141     }
142 
143     /**
144      * Gets the body of {@link AppSearchEmail}.
145      *
146      * @return The body of {@link AppSearchEmail} or {@code null} if it's not been set yet.
147      */
getBody()148     public @Nullable String getBody() {
149         return getPropertyString(KEY_BODY);
150     }
151 
152     /**
153      * The builder class for {@link AppSearchEmail}.
154      */
155     public static class Builder extends GenericDocument.Builder<Builder> {
156         /**
157          * Creates a new {@link Builder}
158          *
159          * @param namespace The namespace of the Email.
160          * @param id The ID of the Email.
161          */
Builder(@onNull String namespace, @NonNull String id)162         public Builder(@NonNull String namespace, @NonNull String id) {
163             super(namespace, id, SCHEMA_TYPE);
164         }
165 
166         /**
167          * Sets the from address of {@link AppSearchEmail}
168          */
169         @CanIgnoreReturnValue
setFrom(@onNull String from)170         public @NonNull Builder setFrom(@NonNull String from) {
171             return setPropertyString(KEY_FROM, from);
172         }
173 
174         /**
175          * Sets the destination address of {@link AppSearchEmail}
176          */
177         @CanIgnoreReturnValue
setTo(String @onNull .... to)178         public @NonNull Builder setTo(String @NonNull ... to) {
179             return setPropertyString(KEY_TO, to);
180         }
181 
182         /**
183          * Sets the CC list of {@link AppSearchEmail}
184          */
185         @CanIgnoreReturnValue
setCc(String @onNull .... cc)186         public @NonNull Builder setCc(String @NonNull ... cc) {
187             return setPropertyString(KEY_CC, cc);
188         }
189 
190         /**
191          * Sets the BCC list of {@link AppSearchEmail}
192          */
193         @CanIgnoreReturnValue
setBcc(String @onNull .... bcc)194         public @NonNull Builder setBcc(String @NonNull ... bcc) {
195             return setPropertyString(KEY_BCC, bcc);
196         }
197 
198         /**
199          * Sets the subject of {@link AppSearchEmail}
200          */
201         @CanIgnoreReturnValue
setSubject(@onNull String subject)202         public @NonNull Builder setSubject(@NonNull String subject) {
203             return setPropertyString(KEY_SUBJECT, subject);
204         }
205 
206         /**
207          * Sets the body of {@link AppSearchEmail}
208          */
209         @CanIgnoreReturnValue
setBody(@onNull String body)210         public @NonNull Builder setBody(@NonNull String body) {
211             return setPropertyString(KEY_BODY, body);
212         }
213 
214         /** Builds the {@link AppSearchEmail} object. */
215         @Override
build()216         public @NonNull AppSearchEmail build() {
217             return new AppSearchEmail(super.build());
218         }
219     }
220 }
221