2525import notion .api .v1 .model .pages .PageProperty ;
2626import notion .api .v1 .model .pages .PageProperty .RichText ;
2727import notion .api .v1 .request .databases .QueryDatabaseRequest ;
28+ import org .jspecify .annotations .Nullable ;
2829import org .springframework .batch .extensions .notion .mapping .PropertyMapper ;
29- import org .springframework .batch .item .ExecutionContext ;
30- import org .springframework .batch .item .ItemReader ;
31- import org .springframework .batch .item .data .AbstractPaginatedDataItemReader ;
32- import org .springframework .beans .factory .InitializingBean ;
30+ import org .springframework .batch .infrastructure .item .ExecutionContext ;
31+ import org .springframework .batch .infrastructure .item .ItemReader ;
32+ import org .springframework .batch .infrastructure .item .data .AbstractPaginatedDataItemReader ;
3333import org .springframework .util .Assert ;
3434
3535import java .util .Collections ;
5757 * @author Stefano Cordio
5858 * @param <T> Type of item to be read
5959 */
60- public class NotionDatabaseItemReader <T > extends AbstractPaginatedDataItemReader <T > implements InitializingBean {
61-
62- private static final String DEFAULT_BASE_URL = "https://api.notion.com/v1" ;
60+ public class NotionDatabaseItemReader <T > extends AbstractPaginatedDataItemReader <T > {
6361
6462 private static final int DEFAULT_PAGE_SIZE = 100 ;
6563
66- private String baseUrl ;
64+ private static final String DEFAULT_BASE_URL = "https://api.notion.com/v1" ;
65+
66+ private final String token ;
6767
68- private String token ;
68+ private final String databaseId ;
6969
70- private String databaseId ;
70+ private final PropertyMapper < T > propertyMapper ;
7171
72- private PropertyMapper < T > propertyMapper ;
72+ private String baseUrl = DEFAULT_BASE_URL ;
7373
74- private QueryTopLevelFilter filter ;
74+ private @ Nullable QueryTopLevelFilter filter ;
7575
76- private List <QuerySort > sorts ;
76+ private @ Nullable List <QuerySort > sorts ;
7777
78- private NotionClient client ;
78+ private @ Nullable NotionClient client ;
7979
8080 private boolean hasMore ;
8181
82- private String nextCursor ;
82+ private @ Nullable String nextCursor ;
8383
8484 /**
85- * Create a new {@link NotionDatabaseItemReader} with the following defaults:
86- * <ul>
87- * <li>{@code baseUrl} = {@value #DEFAULT_BASE_URL}</li>
88- * <li>{@code pageSize} = {@value #DEFAULT_PAGE_SIZE}</li>
89- * </ul>
85+ * Create a new {@link NotionDatabaseItemReader}.
86+ * @param token the Notion integration token
87+ * @param databaseId UUID of the database to read from
88+ * @param propertyMapper the {@link PropertyMapper} responsible for mapping properties
89+ * of a Notion item into a Java object
9090 */
91- public NotionDatabaseItemReader () {
92- this .baseUrl = DEFAULT_BASE_URL ;
91+ public NotionDatabaseItemReader (String token , String databaseId , PropertyMapper <T > propertyMapper ) {
92+ this .token = Objects .requireNonNull (token );
93+ this .databaseId = Objects .requireNonNull (databaseId );
94+ this .propertyMapper = Objects .requireNonNull (propertyMapper );
9395 this .pageSize = DEFAULT_PAGE_SIZE ;
9496 }
9597
@@ -106,37 +108,6 @@ public void setBaseUrl(String baseUrl) {
106108 this .baseUrl = Objects .requireNonNull (baseUrl );
107109 }
108110
109- /**
110- * The Notion integration token.
111- * <p>
112- * Always required.
113- * @param token the token
114- */
115- public void setToken (String token ) {
116- this .token = Objects .requireNonNull (token );
117- }
118-
119- /**
120- * UUID of the database to read from.
121- * <p>
122- * Always required.
123- * @param databaseId the database UUID
124- */
125- public void setDatabaseId (String databaseId ) {
126- this .databaseId = Objects .requireNonNull (databaseId );
127- }
128-
129- /**
130- * The {@link PropertyMapper} responsible for mapping Notion item properties into a
131- * Java object.
132- * <p>
133- * Always required.
134- * @param propertyMapper the property mapper
135- */
136- public void setPropertyMapper (PropertyMapper <T > propertyMapper ) {
137- this .propertyMapper = Objects .requireNonNull (propertyMapper );
138- }
139-
140111 /**
141112 * {@link Filter} condition to limit the returned items.
142113 * <p>
@@ -175,6 +146,19 @@ public void setPageSize(int pageSize) {
175146 super .setPageSize (pageSize );
176147 }
177148
149+ /**
150+ * {@inheritDoc}
151+ */
152+ @ Override
153+ protected void doOpen () {
154+ client = new NotionClient (token );
155+ client .setHttpClient (new JavaNetHttpClient ());
156+ client .setLogger (new Slf4jLogger ());
157+ client .setBaseUrl (baseUrl );
158+
159+ hasMore = true ;
160+ }
161+
178162 /**
179163 * {@inheritDoc}
180164 */
@@ -190,6 +174,7 @@ protected Iterator<T> doPageRead() {
190174 request .setStartCursor (nextCursor );
191175 request .setPageSize (pageSize );
192176
177+ @ SuppressWarnings ("DataFlowIssue" )
193178 QueryResults queryResults = client .queryDatabase (request );
194179
195180 hasMore = queryResults .getHasMore ();
@@ -198,7 +183,7 @@ protected Iterator<T> doPageRead() {
198183 return queryResults .getResults ()
199184 .stream ()
200185 .map (NotionDatabaseItemReader ::getProperties )
201- .map (properties -> propertyMapper . map ( properties ) )
186+ .map (propertyMapper :: map )
202187 .iterator ();
203188 }
204189
@@ -224,19 +209,7 @@ private static String getPlainText(List<RichText> texts) {
224209 /**
225210 * {@inheritDoc}
226211 */
227- @ Override
228- protected void doOpen () {
229- client = new NotionClient (token );
230- client .setHttpClient (new JavaNetHttpClient ());
231- client .setLogger (new Slf4jLogger ());
232- client .setBaseUrl (baseUrl );
233-
234- hasMore = true ;
235- }
236-
237- /**
238- * {@inheritDoc}
239- */
212+ @ SuppressWarnings ("DataFlowIssue" )
240213 @ Override
241214 protected void doClose () {
242215 client .close ();
@@ -255,14 +228,4 @@ protected void jumpToItem(int itemIndex) throws Exception {
255228 }
256229 }
257230
258- /**
259- * {@inheritDoc}
260- */
261- @ Override
262- public void afterPropertiesSet () {
263- Assert .state (token != null , "'token' must be set" );
264- Assert .state (databaseId != null , "'databaseId' must be set" );
265- Assert .state (propertyMapper != null , "'propertyMapper' must be set" );
266- }
267-
268231}
0 commit comments