[LIB-13] Add function to Scanner API
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / AbstractSettingsScanner.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
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 org.hedgecode.chess.scanner.portal;
18
19 import org.hedgecode.chess.scanner.ChessHogScannerException;
20 import org.hedgecode.chess.scanner.Initiable;
21 import org.hedgecode.chess.scanner.Settings;
22 import org.hedgecode.chess.scanner.SettingsBuilder;
23 import org.hedgecode.chess.scanner.regex.RegexBuilder;
24 import org.hedgecode.chess.scanner.regex.RegexParams;
25 import org.hedgecode.chess.scanner.spi.ServiceRegistry;
26
27 import static org.hedgecode.chess.scanner.regex.RegexBuilder.Type;
28
29 /**
30  * AbstractSettingsScanner
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public abstract class AbstractSettingsScanner extends AbstractRequestScanner implements Initiable {
35
36     private Settings settings;
37
38     protected abstract String getResourceName();
39
40     @Override
41     public void init() throws ChessHogScannerException {
42         SettingsBuilder settingsBuilder = ServiceRegistry.singleProvider(
43                 SettingsBuilder.class
44         );
45         settings = settingsBuilder.build(
46                 getResourceName()
47         );
48     }
49
50     protected Settings getSettings() {
51         return settings;
52     }
53
54     protected String assignUrl(String tournamentId, String pageId) {
55         String tournamentUrl = assignUrlWithParams(
56                 settings.getTournamentUrl(), settings.getTournamentQueryParams()
57         );
58         return RegexBuilder.build(
59                 Type.TOURNAMENT,
60                 tournamentUrl,
61                 new RegexParams(
62                         tournamentId,
63                         settings.isTournamentMultiPage() ? pageId : null
64                 )
65         );
66     }
67
68     protected String assignUrl(String gameId) {
69         String gameUrl = settings.getGamePgnUrl() != null
70                 ? settings.getGamePgnUrl()
71                 : settings.getGameUrl();
72         return RegexBuilder.build(
73                 Type.GAME,
74                 assignUrlWithParams(gameUrl, settings.getGameQueryParams()),
75                 new RegexParams(gameId)
76         );
77     }
78
79     protected String assignUrl(String gameId, String tournamentId, boolean withTournament) {
80         String gameUrl = settings.getGamePgnUrl() != null
81                 ? settings.getGamePgnUrl()
82                 : settings.getGameUrl();
83         return RegexBuilder.build(
84                 Type.GAME,
85                 assignUrlWithParams(gameUrl, settings.getGameQueryParams()),
86                 new RegexParams(
87                         gameId,
88                         tournamentId,
89                         withTournament
90                 )
91         );
92     }
93
94     protected String assignUrl(String query, boolean isUrlEncode) {
95         return RegexBuilder.build(
96                 Type.QUERY,
97                 settings.getTournamentQuery(),
98                 new RegexParams(
99                         query,
100                         isUrlEncode
101                 )
102         );
103     }
104
105     private String assignUrlWithParams(String url, String params) {
106         return params != null
107                 ? url.concat(params)
108                 : url;
109     }
110
111 }