Skip to content

Commit

Permalink
Refactoring as preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
t3t5u committed Apr 22, 2024
1 parent ce7b2d9 commit 738985d
Show file tree
Hide file tree
Showing 47 changed files with 551 additions and 175 deletions.
96 changes: 96 additions & 0 deletions src/main/java/org/embulk/output/kintone/KintoneClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.embulk.output.kintone;

import com.kintone.client.KintoneClientBuilder;
import com.kintone.client.RecordClient;
import com.kintone.client.model.app.field.FieldProperty;
import com.kintone.client.model.record.FieldType;
import java.io.IOException;
import java.util.Map;
import java.util.function.Supplier;
import org.embulk.config.ConfigException;
import org.embulk.output.kintone.util.Lazy;
import org.embulk.spi.Column;
import org.embulk.spi.Schema;

public class KintoneClient implements AutoCloseable {
private final PluginTask task;
private final Schema schema;
private final com.kintone.client.KintoneClient client;
private final Map<String, FieldProperty> fields;

public static Lazy<KintoneClient> lazy(Supplier<PluginTask> task, Schema schema) {
return new Lazy<KintoneClient>() {
@Override
protected KintoneClient initialValue() {
return new KintoneClient(task.get(), schema);
}
};
}

private KintoneClient(PluginTask task, Schema schema) {
this.task = task;
this.schema = schema;
KintoneClientBuilder builder = KintoneClientBuilder.create("https://" + task.getDomain());
if (task.getGuestSpaceId().isPresent()) {
builder.setGuestSpaceId(task.getGuestSpaceId().get());
}
if (task.getBasicAuthUsername().isPresent() && task.getBasicAuthPassword().isPresent()) {
builder.withBasicAuth(task.getBasicAuthUsername().get(), task.getBasicAuthPassword().get());
}
if (task.getUsername().isPresent() && task.getPassword().isPresent()) {
builder.authByPassword(task.getUsername().get(), task.getPassword().get());
} else if (task.getToken().isPresent()) {
builder.authByApiToken(task.getToken().get());
} else {
throw new ConfigException("Username and password or token must be configured.");
}
client = builder.build();
fields = client.app().getFormFields(task.getAppId());
KintoneMode.of(task).validate(task, this);
}

public void validateUpdateKey(String columnName) {
Column column = getColumn(columnName);
if (column == null) {
throw new ConfigException("The column '" + columnName + "' for update does not exist.");
}
String fieldCode = getFieldCode(column);
FieldType fieldType = getFieldType(fieldCode);
if (fieldType == null) {
throw new ConfigException("The field '" + fieldCode + "' for update does not exist.");
}
if (fieldType != FieldType.SINGLE_LINE_TEXT && fieldType != FieldType.NUMBER) {
throw new ConfigException("The update_key must be 'SINGLE_LINE_TEXT' or 'NUMBER'.");
}
}

public Column getColumn(String columnName) {
return schema.getColumns().stream()
.filter(column -> column.getName().equals(columnName))
.findFirst()
.orElse(null);
}

public FieldType getFieldType(String fieldCode) {
FieldProperty field = fields.get(fieldCode);
return field == null ? null : field.getType();
}

public RecordClient record() {
return client.record();
}

@Override
public void close() {
try {
client.close();
} catch (IOException e) {
throw new RuntimeException("kintone throw exception", e);
}
}

private String getFieldCode(Column column) {
KintoneColumnOption option = task.getColumnOptions().get(column.getName());
return option != null ? option.getFieldCode() : column.getName();
}
}
17 changes: 16 additions & 1 deletion src/main/java/org/embulk/output/kintone/KintoneColumnType.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.kintone.client.model.record.NumberFieldValue;
import com.kintone.client.model.record.OrganizationSelectFieldValue;
import com.kintone.client.model.record.RadioButtonFieldValue;
import com.kintone.client.model.record.Record;
import com.kintone.client.model.record.RichTextFieldValue;
import com.kintone.client.model.record.SingleLineTextFieldValue;
import com.kintone.client.model.record.SubtableFieldValue;
Expand Down Expand Up @@ -49,6 +50,11 @@ public SingleLineTextFieldValue getFieldValue(String value, KintoneColumnOption
return new SingleLineTextFieldValue(value);
}

@Override
public Object getValue(Record record, String fieldCode) {
return record.getSingleLineTextFieldValue(fieldCode);
}

@Override
public void setUpdateKey(UpdateKey updateKey, String field) {
updateKey.setField(field);
Expand Down Expand Up @@ -132,6 +138,11 @@ public NumberFieldValue getFieldValue(Timestamp value, KintoneColumnOption optio
return (NumberFieldValue) getFieldValue(value.getEpochSecond(), option);
}

@Override
public Object getValue(Record record, String fieldCode) {
return record.getNumberFieldValue(fieldCode);
}

@Override
public void setUpdateKey(UpdateKey updateKey, String field) {
updateKey.setField(field);
Expand All @@ -144,7 +155,7 @@ public void setUpdateKey(UpdateKey updateKey, String field, FieldValue value) {

@Override
public Value asValue(FieldValue value) {
return ValueFactory.newString(((NumberFieldValue) value).getValue().toString());
return ValueFactory.newString(((NumberFieldValue) value).getValue().toPlainString());
}

@Override
Expand Down Expand Up @@ -540,6 +551,10 @@ public FieldValue getFieldValue(Value value, KintoneColumnOption option) {
}
}

public Object getValue(Record Record, String fieldCode) {
throw new UnsupportedOperationException();
}

public void setUpdateKey(UpdateKey updateKey, String field) {
throw new UnsupportedOperationException();
}
Expand Down
52 changes: 48 additions & 4 deletions src/main/java/org/embulk/output/kintone/KintoneMode.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,67 @@
package org.embulk.output.kintone;

import org.embulk.config.ConfigException;
import org.embulk.spi.Page;

public enum KintoneMode {
INSERT("insert"),
UPDATE("update"),
UPSERT("upsert");
INSERT("insert") {
@Override
public void validate(PluginTask task, KintoneClient client) {
if (task.getUpdateKeyName().isPresent()) {
throw new ConfigException("When mode is insert, require no update_key.");
}
}

@Override
public void add(Page page, KintonePageOutput output) {
output.insertPage(page);
}
},
UPDATE("update") {
@Override
public void validate(PluginTask task, KintoneClient client) {
if (!task.getUpdateKeyName().isPresent()) {
throw new ConfigException("When mode is update, require update_key.");
}
client.validateUpdateKey(task.getUpdateKeyName().orElse(null));
}

@Override
public void add(Page page, KintonePageOutput output) {
output.updatePage(page);
}
},
UPSERT("upsert") {
@Override
public void validate(PluginTask task, KintoneClient client) {
if (!task.getUpdateKeyName().isPresent()) {
throw new ConfigException("When mode is upsert, require update_key.");
}
client.validateUpdateKey(task.getUpdateKeyName().orElse(null));
}

@Override
public void add(Page page, KintonePageOutput output) {
output.upsertPage(page);
}
};
private final String value;

KintoneMode(String value) {
this.value = value;
}

public abstract void validate(PluginTask task, KintoneClient client);

public abstract void add(Page page, KintonePageOutput output);

@Override
public String toString() {
return value;
}

public static KintoneMode getKintoneModeByValue(String value) {
public static KintoneMode of(PluginTask task) {
String value = task.getMode();
for (KintoneMode mode : values()) {
if (mode.toString().equals(value)) {
return mode;
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/org/embulk/output/kintone/KintoneOutputPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Collections;
import java.util.List;
import org.embulk.config.ConfigDiff;
import org.embulk.config.ConfigException;
import org.embulk.config.ConfigSource;
import org.embulk.config.TaskReport;
import org.embulk.config.TaskSource;
Expand Down Expand Up @@ -40,22 +39,6 @@ public void cleanup(
@Override
public TransactionalPageOutput open(TaskSource taskSource, Schema schema, int taskIndex) {
PluginTask task = taskSource.loadTask(PluginTask.class);
KintoneMode mode = KintoneMode.getKintoneModeByValue(task.getMode());
switch (mode) {
case INSERT:
if (task.getUpdateKeyName().isPresent()) {
throw new ConfigException("when mode is insert, require no update_key.");
}
break;
case UPDATE:
case UPSERT:
if (!task.getUpdateKeyName().isPresent()) {
throw new ConfigException("when mode is update or upsert, require update_key.");
}
break;
default:
throw new ConfigException(String.format("Unknown mode '%s'", task.getMode()));
}
return task.getReduceKeyName().isPresent()
? new ReducedPageOutput(schema, taskIndex)
: new KintonePageOutput(task, schema);
Expand Down
Loading

0 comments on commit 738985d

Please sign in to comment.