99import com .google .gson .GsonBuilder ;
1010import com .google .gson .JsonElement ;
1111import com .google .gson .JsonParser ;
12- import eu .luminis .elastic .document .DocumentService ;
13- import eu .luminis .elastic .document .IndexRequest ;
14- import eu .luminis .elastic .document .UpdateRequest ;
15- import eu .luminis .elastic .index .IndexService ;
16- import eu .luminis .elastic .search .SearchService ;
1712import io .academic .dao .DcDao ;
1813import io .academic .entity .*;
14+ import org .apache .commons .io .IOUtils ;
1915import org .apache .http .HttpHost ;
16+ import org .elasticsearch .ElasticsearchException ;
17+ import org .elasticsearch .action .DocWriteResponse ;
2018import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
19+ import org .elasticsearch .action .index .IndexRequest ;
20+ import org .elasticsearch .action .index .IndexResponse ;
2121import org .elasticsearch .action .search .*;
22+ import org .elasticsearch .action .support .replication .ReplicationResponse ;
2223import org .elasticsearch .client .RestClient ;
2324import org .elasticsearch .client .RestHighLevelClient ;
2425import org .elasticsearch .common .unit .TimeValue ;
26+ import org .elasticsearch .common .xcontent .XContentType ;
2527import org .elasticsearch .index .query .QueryBuilders ;
28+ import org .elasticsearch .rest .RestStatus ;
2629import org .elasticsearch .search .Scroll ;
2730import org .elasticsearch .search .SearchHit ;
2831import org .elasticsearch .search .builder .SearchSourceBuilder ;
3942import org .springframework .stereotype .Service ;
4043
4144import javax .transaction .Transactional ;
45+ import java .io .BufferedInputStream ;
46+ import java .io .BufferedReader ;
4247import java .io .IOException ;
48+ import java .io .InputStreamReader ;
49+ import java .net .MalformedURLException ;
50+ import java .net .URL ;
51+ import java .net .URLConnection ;
4352import java .time .LocalDate ;
4453import java .time .LocalDateTime ;
4554import java .time .format .DateTimeFormatter ;
@@ -81,28 +90,30 @@ public RestHighLevelClient getRestClient() {
8190 public static final String INDEX = "harvester" ;
8291 private static final String TYPE = "oai" ;
8392
84- private DocumentService documentService = null ;
85- private IndexService indexService = null ;
86- private SearchService searchService = null ;
87- private IndexRequest request ;
8893
8994 @ Autowired
90- public OaiService (DocumentService documentService , IndexService indexService , SearchService searchService ) {
91- this .documentService = documentService ;
92- this .indexService = indexService ;
93- // indexService.createIndex();
94- this .searchService = searchService ;
95- }
96-
97- public String elasticSave (Article article ) {
98- IndexRequest request = new IndexRequest (INDEX , TYPE ).setEntity (article );
95+ public OaiService ()
96+ {
9997
98+ }
10099
101- if (article .getId () != null ) {
102- request .setId (String .valueOf (article .getId ()));
103- }
100+ public void elasticSave (Article article ) throws IOException {
101+ IndexRequest request = new IndexRequest (INDEX ,TYPE );
102+ request .setPipeline ("academic-pdf" );
103+ // before using this pipeline we have to add pipeline to the elasticsearch by following command
104+ // PUT _ingest/pipeline/academic-pdf
105+ // {
106+ // "description": "parse pdfs and index into ES",
107+ // "processors" :
108+ // [
109+ // { "attachment" : { "field": "pdf" } },
110+ // { "remove" : { "field": "pdf" } }
111+ // ]
112+ // }
113+
114+ request .source (new Gson ().toJson (article ), XContentType .JSON );
115+ IndexResponse indexResponse = restClient .index (request );
104116
105- return documentService .index (request );
106117 }
107118
108119
@@ -131,6 +142,7 @@ public void saveRecords(List<RecordType> recordTypes) {
131142 oaiRecord .setState (0 );
132143 oaiRecords .add (oaiRecord );
133144
145+ //TODO: we ave to check all the parts name and assigned according to related name not order
134146 String [] parts = parsedDc .getDc ().split (";;" );
135147 Article article = new Article ();
136148 article .setTitle (parts [0 ].split ("::" )[1 ]);
@@ -140,10 +152,27 @@ public void saveRecords(List<RecordType> recordTypes) {
140152 article .setPublisher (parts [4 ].split ("::" )[1 ]);
141153 article .setDate (parts [5 ].split ("::" )[1 ]);
142154 article .setType (parts [6 ].split ("::" )[1 ]);
155+ if (parts .length >10 )
156+ {
157+ String downlaodUrl = parts [10 ].split ("::" )[1 ];
158+ article .setRelation (downlaodUrl );
159+ article .setBase64 (UrlPdftoBase64 (downlaodUrl ));
160+ }
161+ else
162+ {
163+ article .setRelation ("not available" );
164+ article .setBase64 ("bm90IGF2YWlsYWJsZQ==" ); //it means not available in base 64
165+ }
143166 article .setDc (parsedDc .getDc ());
167+ article .setArticleIdentifier (parseIdentifier (oaiRecord .getIdentifier ()));
168+
144169 articles .add (article );
145170
146- elasticSave (article );
171+ try {
172+ elasticSave (article );
173+ } catch (IOException e ) {
174+ e .printStackTrace ();
175+ }
147176 });
148177
149178 oaiRecordRepository .save (oaiRecords );
@@ -167,6 +196,31 @@ private String marshallDc(MetadataType metadataType) {
167196 }
168197 }
169198
199+ public String UrlPdftoBase64 (String url ) {
200+ URL oracle = null ;
201+ String base64 = "bm90IGF2YWlsYWJsZQ==" ; //means not available
202+ System .out .println (url );
203+ try {
204+ oracle = new URL (url );
205+ URLConnection yc = oracle .openConnection ();
206+
207+ BufferedInputStream bis = new BufferedInputStream (yc .getInputStream ());
208+
209+ byte bytes [] = IOUtils .toByteArray (bis );
210+ bis .close ();
211+ base64 = Base64 .getEncoder ().encodeToString (bytes );
212+ System .out .println (url );
213+ System .out .println (base64 );
214+ } catch (MalformedURLException e ) {
215+ e .printStackTrace ();
216+ } catch (IOException e ) {
217+ e .printStackTrace ();
218+ }
219+
220+ return base64 ;
221+
222+ }
223+
170224 private LocalDateTime parseDateTime (String string ) {
171225 LocalDateTime ldt ;
172226 DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd['T'HH:mm:ss'Z']" );
@@ -182,12 +236,27 @@ private LocalDateTime parseDateTime(String string) {
182236// return LocalDateTime.parse(string, formatter);
183237 }
184238
239+ private String parseIdentifier (String oaiId ){
240+ String Id ="" ;
241+ Id = oaiId .substring (oaiId .lastIndexOf (':' ) + 1 ); // split identifier with ":" and take last part
242+ Id = Id .substring (Id .lastIndexOf ('/' ) + 1 ); // split identifier with "/" and take last part
243+ return Id ;
244+ }
185245
186- public void delete () throws IOException {
187246
188- //TODO:check if there is any indices with that name
189- DeleteIndexRequest request = new DeleteIndexRequest ("harvester" );
190- restClient .indices ().deleteIndex (request );
247+ public void delete () {
248+
249+ try {
250+ DeleteIndexRequest request = new DeleteIndexRequest ("harvester" );
251+ restClient .indices ().deleteIndex (request );
252+ } catch (ElasticsearchException exception ) {
253+ if (exception .status () == RestStatus .NOT_FOUND ) {
254+ System .out .println ("Index not found and not deleted" );
255+ }
256+ } catch (IOException e ) {
257+ e .printStackTrace ();
258+ }
259+
191260
192261 }
193262
0 commit comments