sirius.db.mixing.properties.LongProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sirius-db Show documentation
Show all versions of sirius-db Show documentation
Provides a modern and highly flexible ORM and lightweight connectivity for JDBC, MongoDB, Redis, Elasticsearch.
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.db.mixing.properties;
import com.alibaba.fastjson.JSONObject;
import sirius.db.es.ESPropertyInfo;
import sirius.db.es.IndexMappings;
import sirius.db.es.annotations.IndexMode;
import sirius.db.jdbc.schema.SQLPropertyInfo;
import sirius.db.jdbc.schema.Table;
import sirius.db.jdbc.schema.TableColumn;
import sirius.db.mixing.AccessPath;
import sirius.db.mixing.BaseMapper;
import sirius.db.mixing.EntityDescriptor;
import sirius.db.mixing.Mixable;
import sirius.db.mixing.Property;
import sirius.db.mixing.PropertyFactory;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Value;
import sirius.kernel.di.std.Register;
import java.lang.reflect.Field;
import java.sql.Types;
import java.util.function.Consumer;
/**
* Represents an {@link Long} field within a {@link Mixable}.
*/
public class LongProperty extends Property implements SQLPropertyInfo, ESPropertyInfo {
/**
* Factory for generating properties based on their field type
*/
@Register
public static class Factory implements PropertyFactory {
@Override
public boolean accepts(EntityDescriptor descriptor, Field field) {
return Long.class.equals(field.getType()) || long.class.equals(field.getType());
}
@Override
public void create(EntityDescriptor descriptor,
AccessPath accessPath,
Field field,
Consumer propertyConsumer) {
propertyConsumer.accept(new LongProperty(descriptor, accessPath, field));
}
}
LongProperty(EntityDescriptor descriptor, AccessPath accessPath, Field field) {
super(descriptor, accessPath, field);
}
@Override
public Object transformValue(Value value) {
if (value.isFilled()) {
Long result = value.getLong();
if (result == null) {
throw illegalFieldValue(value);
}
return result;
}
if (this.isNullable() || Strings.isEmpty(defaultValue)) {
return null;
}
return Value.of(defaultValue).getLong();
}
@Override
protected Object transformFromDatasource(Class extends BaseMapper, ?, ?>> mapperType, Value object) {
if (object.get() instanceof Integer) {
return Long.valueOf((Integer) object.get());
}
if (object.is(String.class) ) {
return object.getLong();
}
return object.get();
}
@Override
protected Object transformToDatasource(Class extends BaseMapper, ?, ?>> mapperType, Object object) {
return object;
}
@Override
public void contributeToTable(Table table) {
table.getColumns().add(new TableColumn(this, Types.BIGINT));
}
@Override
public void describeProperty(JSONObject description) {
description.put(IndexMappings.MAPPING_TYPE, "long");
transferOption(IndexMappings.MAPPING_STORED, getAnnotation(IndexMode.class), IndexMode::stored, description);
transferOption(IndexMappings.MAPPING_INDEX, getAnnotation(IndexMode.class), IndexMode::indexed, description);
transferOption(IndexMappings.MAPPING_DOC_VALUES,
getAnnotation(IndexMode.class),
IndexMode::docValues,
description);
}
}