58ac5b0f65ed4008e02ee4c66bb49bd9c6a0537e
[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.getGameQueryParams()
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 query, boolean isUrlEncode) {
80         return RegexBuilder.build(
81                 Type.QUERY,
82                 settings.getTournamentQuery(),
83                 new RegexParams(query, isUrlEncode)
84         );
85     }
86
87     private String assignUrlWithParams(String url, String params) {
88         return params != null
89                 ? url.concat(params)
90                 : url;
91     }
92
93 }