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());
}
}
}
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?
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?
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
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.