-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from trocco-io/update_by_id
Add support for update by id
- Loading branch information
Showing
143 changed files
with
1,284 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/org/embulk/output/kintone/KintoneClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
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.record.Id; | ||
import org.embulk.output.kintone.util.Lazy; | ||
import org.embulk.spi.Column; | ||
import org.embulk.spi.Schema; | ||
import org.embulk.spi.type.Type; | ||
import org.embulk.spi.type.Types; | ||
|
||
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 validateIdOrUpdateKey(String columnName) { | ||
Column column = getColumn(columnName); | ||
if (column == null) { | ||
throw new ConfigException("The column '" + columnName + "' for update does not exist."); | ||
} | ||
validateId(column); | ||
validateUpdateKey(column); | ||
} | ||
|
||
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 void validateId(Column column) { | ||
if (!column.getName().equals(Id.FIELD)) { | ||
return; | ||
} | ||
Type type = column.getType(); | ||
if (!type.equals(Types.LONG)) { | ||
throw new ConfigException("The id column must be 'long'."); | ||
} | ||
} | ||
|
||
private void validateUpdateKey(Column column) { | ||
if (column.getName().equals(Id.FIELD)) { | ||
return; | ||
} | ||
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'."); | ||
} | ||
} | ||
|
||
private String getFieldCode(Column column) { | ||
KintoneColumnOption option = task.getColumnOptions().get(column.getName()); | ||
return option != null ? option.getFieldCode() : column.getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.