New datatape

Hi

I would like to create a new datatype, which is basically a string and automatically converts all the contents into uppercase.

What would be the best solution for that. Do I need to create and implement it through datatypes.xml?

A sample would be highly appreciated.

Thanks

Hi Darius,

You can find a few samples here: https://github.com/knstvk/cuba-sample-datatype

1 Like

Hi Konstantin.
Thank you for the prompt reply.
I created a new datatype class and also added an entry to metadata.xml, but my new datatype still does not appear in Studio. What might be a problem? Thanks


package com.company.sample.datatype;
import com.haulmont.chile.core.datatypes.impl.StringDatatype;
import javax.annotation.Nonnull;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.text.ParseException;
import java.util.Locale;
public class CodeDatatype extends StringDatatype {
public static final String NAME = "code";
@Override
public Class getJavaClass() {
return String.class;
}
@Override
public String getName() {
return NAME;
}
@Override
public int getSqlType() {
return Types.VARCHAR;
}
@Nonnull
@Override
public String format(Object value) {
return (value == null ? "" : (String) value).toUpperCase();
}
@Nonnull
@Override
public String format(Object value, Locale locale) {
return (value == null ? "" : (String) value).toUpperCase(locale);
}
@Override
public String parse(String value) {
return value.toUpperCase();
}
@Override
public String parse(String value, Locale locale) throws ParseException {
return value.toUpperCase(locale);
}
@Override
public String read(ResultSet resultSet, int index) throws SQLException {
return resultSet.getString(index).toUpperCase();
}
@Override
public void write(PreparedStatement statement, int index, Object value) throws SQLException {
if (value == null) {
statement.setString(index, null);
} else {
String s = (String) value;
statement.setString(index, s.toUpperCase());
}
}
}

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<metadata xmlns="[url=http://schemas.haulmont.com/cuba/metadata.xsd]http://schemas.haulmont.com/cuba/metadata.xsd">[/url];
<datatypes>
<datatype class="com.company.sample.datatype.CodeDatatype" javaType="java.lang.String"></datatype>
</datatypes>
<metadata-model root-package="com.company.sample" namespace="sample"/>
</metadata>

Are you sure it doesn’t appear? It can be on the next page of the drop-down list, have you scrolled to the end?

1 Like

Ups. You are right. It is indeed on the next page. Learning new things every day. Thanks

The only thing is, I do not see length attribute, and the DB scripts are created without the length.
Do I need to do this manually? Thanks

e154baa2459347a356ec96089c79cbcd

ddc8ca2d9ee380e6bcb4ec298b5c3017

Hi Konstantin,

I would like to limit the length of the column. If I modify the entity java file, by adding length attribute to the column, and amending the table creation script, next time when I use cuba studio to add additional fields, these changes I did (i. e. length attribute) get overwritten by the studio.

Another thing I noticed is, if I use custom data types, Entity Inspector doesn’t work anymore. This is the case even in the sample application cuba-sample-datatype. If I open Entity Inspector in Administration menu and try to create a new record I get the following error message

If I need these custom data type columns in almost all of my entities. If I do that I will loose the very convenient way to quickly insppect and amend records through entity inspector.

Could you please suggest, what would be the best way to deal that? Should I use some different approach to create a column which converts and displays it’s contents in uppercase?

Thank you very much.

Hi Darius,

In order to tell Studio what column type to generate, add sqlType=“varchar(50)” attribute to your type registration in metadata.xml as is done here for PhoneDatatype.

As for Entity Inspector error on custom datatypes, it is definitely a bug and we’ll fix it in one of the next bug-fix releases.

Also, have you seen this improvement that was done in response to your request about automatic converting to upper case?

Hi Konstantin.

I saw that it is possible to define teh field length through sqlTYpe attribute. And this is fine for the special type of field like phone. But wouldn’t it be better to have ability to set the length of the field for the data type derived from StringDatatype. Maybe sometimes in the future version.

I 've never seen that improvement, and I am very impressed, that Cuba team did that. I was not using the studio and platform for some time, thats probably why I missed that. Many thanks for that :slight_smile:

Could I suggest one more improvement? It would be nice if we can define this option not on UI, but on entity level. Then it would work even in Entity Inspector and we would never need to worry about UI, for it would propagate to UI also.

Thanks.

I’ve created two issues:

STUDIO-3720 Optional arbitrary column definition for entity attribute - it will enable defining custom column parameters for any attribute

PL-9149 Annotation on entity attribute to convert string values to upper case - it will be a nice feature

Thank you for your feedback!

1 Like

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-9146