[LIB-13] Add common scanner functional
[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.regex.RegexType;
26 import org.hedgecode.chess.scanner.spi.ServiceRegistry;
27
28 /**
29  * AbstractSettingsScanner
30  *
31  * @author Dmitry Samoshin aka gotty
32  */
33 public abstract class AbstractSettingsScanner extends AbstractRequestScanner implements Initiable {
34
35     private Settings settings;
36
37     protected abstract String getResourceName();
38
39     @Override
40     public void init() throws ChessHogScannerException {
41         SettingsBuilder settingsBuilder = ServiceRegistry.singleProvider(
42                 SettingsBuilder.class
43         );
44         settings = settingsBuilder.build(
45                 getResourceName()
46         );
47     }
48
49     protected Settings getSettings() {
50         return settings;
51     }
52
53     protected String assignUrl(String tournamentId, String pageId) {
54         return RegexBuilder.build(
55                 RegexType.TOURNAMENT,
56                 settings.getTournamentUrl(),
57                 new RegexParams(
58                         tournamentId,
59                         settings.isTournamentMultiPage() ? pageId : null
60                 )
61         );
62     }
63
64     protected String assignUrl(String gameId) {
65         String gameUrl = settings.getGamePgnUrl() != null
66                 ? settings.getGamePgnUrl()
67                 : settings.getGameUrl();
68         return RegexBuilder.build(
69                 RegexType.GAME,
70                 gameUrl,
71                 new RegexParams(gameId)
72         );
73     }
74
75     protected String assignUrl(String query, boolean isUrlEncode) {
76         return RegexBuilder.build(
77                 RegexType.QUERY,
78                 settings.getTournamentQuery(),
79                 new RegexParams(query, isUrlEncode)
80         );
81     }
82
83 }