001/**
002 * Copyright (C) 2006-2023 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.sample;
017
018import static java.util.Optional.ofNullable;
019import static org.talend.sdk.component.api.component.Icon.IconType.USER_CIRCLE;
020
021import java.io.BufferedReader;
022import java.io.File;
023import java.io.FileNotFoundException;
024import java.io.IOException;
025import java.io.Serializable;
026import java.util.Map;
027
028import javax.annotation.PostConstruct;
029import javax.annotation.PreDestroy;
030
031import org.talend.sdk.component.api.component.Icon;
032import org.talend.sdk.component.api.component.MigrationHandler;
033import org.talend.sdk.component.api.component.Version;
034import org.talend.sdk.component.api.configuration.Option;
035import org.talend.sdk.component.api.input.Emitter;
036import org.talend.sdk.component.api.input.Producer;
037import org.talend.sdk.component.api.service.Service;
038
039import lombok.AllArgsConstructor;
040import lombok.extern.slf4j.Slf4j;
041
042@Version(value = 2, migrationHandler = PersonReader.Migrations.class)
043@Icon(USER_CIRCLE)
044@Emitter(name = "reader")
045public class PersonReader implements Serializable {
046
047    private final File file;
048
049    private final FileService service;
050
051    private transient BufferedReader reader;
052
053    public PersonReader(@Option("file") final File file, final FileService service) {
054        this.file = file;
055        this.service = service;
056    }
057
058    // tag::open[]
059    @PostConstruct
060    public void open() throws FileNotFoundException {
061        reader = service.createInput(file);
062    }
063    // end::open[]
064
065    // tag::next[]
066    @Producer
067    public Person readNext() throws IOException {
068        final String line = reader.readLine();
069        if (line == null) { // end of the data is marked with null
070            return null;
071        }
072        final String[] info = line.split(";"); // poor csv parser
073        return new Person(info[0], Integer.parseInt(info[1].trim()));
074    }
075    // end::next[]
076
077    // tag::close[]
078    @PreDestroy
079    public void close() throws IOException {
080        if (reader != null) {
081            reader.close();
082        }
083    }
084    // end::close[]
085
086    @Slf4j
087    @AllArgsConstructor
088    public static class Migrations implements MigrationHandler {
089
090        private final MigrationV1V2 migrationV1V2;
091
092        @Override
093        public Map<String, String> migrate(final int incomingVersion, final Map<String, String> incomingData) {
094            switch (incomingVersion) {
095            case 1:
096                log.info("Migration incoming data to version 2 from version 1");
097                return migrationV1V2.migrate(incomingData);
098            default:
099                log.info("No active migration from version " + incomingData);
100            }
101            return incomingData;
102        }
103    }
104
105    @Service
106    public static class MigrationV1V2 {
107
108        public Map<String, String> migrate(final Map<String, String> incomingData) {
109            ofNullable(incomingData.get("old_file")).ifPresent(v -> {
110                incomingData.put("file", v);
111                incomingData.remove("old_file");
112            });
113            return incomingData;
114        }
115    }
116}