use of com.servoy.j2db.util.FormatParser.ParsedFormat in project servoy-client by Servoy.
the class ComponentFormat method getComponentFormat.
public static ComponentFormat getComponentFormat(String formatProperty, int dpType, IServiceProvider application) {
if (// $NON-NLS-1$
"converter".equals(formatProperty)) {
return new ComponentFormat(FormatParser.parseFormatProperty(null), IColumnTypes.TEXT, IColumnTypes.TEXT);
}
int uiType = dpType;
// parse format to see if it contains UI converter info
boolean hasUIConverter = false;
ParsedFormat parsedFormat = FormatParser.parseFormatProperty(formatProperty, null, application);
if (parsedFormat.getUIConverterName() != null) {
IUIConverter uiConverter = ((FoundSetManager) application.getFoundSetManager()).getUIConverterManager().getConverter(parsedFormat.getUIConverterName());
if (uiConverter != null) {
hasUIConverter = true;
int convType = uiConverter.getToObjectType(parsedFormat.getUIConverterProperties());
if (convType != Integer.MAX_VALUE) {
uiType = Column.mapToDefaultType(convType);
}
}
}
String defaultFormat = parsedFormat.isEmpty() ? TagResolver.getDefaultFormatForType(application, uiType) : null;
String formatString;
if (parsedFormat.isEmpty() && !hasUIConverter && !parsedFormat.useLocalDateTime()) {
formatString = defaultFormat;
} else {
formatString = application.getI18NMessageIfPrefixed(parsedFormat.getFormatString());
}
return new ComponentFormat(FormatParser.parseFormatProperty(formatString, defaultFormat), dpType, uiType);
}
use of com.servoy.j2db.util.FormatParser.ParsedFormat in project servoy-client by Servoy.
the class TagResolver method formatObject.
public static String formatObject(Object value, String format, IServiceProvider application) {
if (value == null || value == Scriptable.NOT_FOUND) {
return null;
}
String formatString = null;
int maxLength = -1;
if (format != null) {
ParsedFormat parsedFormat = FormatParser.parseFormatProperty(format);
formatString = parsedFormat.getDisplayFormat();
if (parsedFormat.getMaxLength() != null) {
maxLength = parsedFormat.getMaxLength().intValue();
if (formatString == null) {
char[] chars = new char[maxLength];
for (int i = 0; i < chars.length; i++) chars[i] = '#';
formatString = new String(chars);
}
}
} else {
formatString = getFormatString(value.getClass(), application);
}
if (formatString == null) {
return value.toString();
}
if (value instanceof Date) {
return new SimpleDateFormat(formatString).format(value);
}
if (value instanceof Number) /* Integer extends Number */
{
DecimalFormat decimalFormat = new DecimalFormat(formatString, RoundHalfUpDecimalFormat.getDecimalFormatSymbols(application.getLocale()));
String formatedValue = decimalFormat.format(value);
if (maxLength > -1 && maxLength <= formatedValue.length())
formatedValue = formatedValue.substring(0, maxLength);
return formatedValue;
}
return value.toString();
}
use of com.servoy.j2db.util.FormatParser.ParsedFormat in project servoy-client by Servoy.
the class SQLGenerator method createConditionFromFindState.
private ISQLCondition createConditionFromFindState(FindState s, QuerySelect sqlSelect, IGlobalValueEntry provider, List<IQuerySelectValue> pkQueryColumns) throws RepositoryException {
ISQLCondition and = null;
List<RelatedFindState> relatedFindStates = s.createFindStateJoins(sqlSelect, Collections.<IRelation>emptyList(), sqlSelect.getTable(), provider);
for (int i = 0; relatedFindStates != null && i < relatedFindStates.size(); i++) {
RelatedFindState rfs = relatedFindStates.get(i);
FindState state = rfs.getFindState();
BaseQueryTable columnTable = rfs.getPrimaryTable();
SQLSheet sheet = state.getParentFoundSet().getSQLSheet();
Table table = sheet.getTable();
Iterator<Map.Entry<String, Object>> it = state.getColumnData().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> elem = it.next();
final String dataProviderID = elem.getKey();
Object raw = elem.getValue();
if (raw == null)
continue;
int dataProviderType = -1;
ConverterInfo columnConverterInfo = null;
IColumnConverter columnConverter = null;
IQuerySelectValue qCol = null;
IColumn c = table.getColumn(dataProviderID);
if (c != null) {
dataProviderType = c.getDataProviderType();
columnConverterInfo = sheet.getColumnConverterInfo(dataProviderID);
if (columnConverterInfo != null) {
columnConverter = application.getFoundSetManager().getColumnConverterManager().getConverter(columnConverterInfo.converterName);
if (columnConverter instanceof ITypedColumnConverter) {
int convType = ((ITypedColumnConverter) columnConverter).getToObjectType(columnConverterInfo.props);
if (convType != Integer.MAX_VALUE) {
dataProviderType = mapToDefaultType(convType);
}
}
}
// a column
qCol = ((Column) c).queryColumn(columnTable);
} else {
// not a column, check for aggregates
Iterator<AggregateVariable> aggregateVariables = application.getFlattenedSolution().getAggregateVariables(sheet.getTable(), false);
while (c == null && aggregateVariables.hasNext()) {
AggregateVariable agg = aggregateVariables.next();
if (dataProviderID.equals(agg.getDataProviderID())) {
// found aggregate
c = agg;
}
}
if (c != null) {
dataProviderType = c.getDataProviderType();
Map<String, QuerySelect> aggregates = sheet.getAggregates();
if (aggregates != null) {
QuerySelect aggregateSelect = aggregates.get(dataProviderID);
if (aggregateSelect != null) {
qCol = ((List<IQuerySelectValue>) AbstractBaseQuery.relinkTable(aggregateSelect.getTable(), columnTable, aggregateSelect.getColumnsClone())).get(0);
}
}
}
}
if (qCol == null) {
// not a column and not an aggregate
// $NON-NLS-1$ //$NON-NLS-2$
Debug.log("Ignoring search on unknown/unsupported data provider '" + dataProviderID + "'");
continue;
}
ParsedFormat format = state.getFormat(dataProviderID);
String formatString = null;
if (format != null) {
formatString = format.getEditFormat();
if (formatString == null) {
formatString = format.getDisplayFormat();
}
}
if (Utils.stringIsEmpty(formatString)) {
formatString = TagResolver.getDefaultFormatForType(application, dataProviderType);
}
ISQLCondition or = null;
if (raw.getClass().isArray()) {
int length = Array.getLength(raw);
Object[] elements = new Object[length];
for (int e = 0; e < length; e++) {
Object obj = Array.get(raw, e);
if (obj instanceof Wrapper) {
obj = ((Wrapper) obj).unwrap();
}
// Have to use getAsRightType twice here, once to parse using format (getAsType(dataProviderType, formatString))
// and once to convert for query (getAsType(c.getDataProviderType(), null))
Object converted = convertFromObject(application, columnConverter, columnConverterInfo, dataProviderID, c.getDataProviderType(), Column.getAsRightType(dataProviderType, c.getFlags(), obj, formatString, c.getLength(), null, false, false), false);
elements[e] = Column.getAsRightType(c.getDataProviderType(), c.getFlags(), converted, null, c.getLength(), null, false, false);
}
// where qCol in (e1, e2, ..., en)
or = new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, new IQuerySelectValue[] { qCol }, new Object[][] { elements }, true);
} else {
final IColumnConverter fColumnConverter = columnConverter;
final ConverterInfo fColumnConverterInfo = columnConverterInfo;
final int fDataProviderType = c.getDataProviderType();
or = (ISQLCondition) BaseSQLGenerator.parseFindExpression(QueryFactory.INSTANCE, raw, qCol, columnTable, dataProviderType, formatString, c, rfs.getRelations().size() > 0 && relatedNullSearchAddPkCondition(), new IValueConverter() {
@Override
public Object convertFromObject(Object value) {
return SQLGenerator.convertFromObject(application, fColumnConverter, fColumnConverterInfo, dataProviderID, fDataProviderType, value, false);
}
}, new ITypeConverter() {
@Override
public Object getAsRightType(int type, int flags, Object obj, int l, boolean throwOnFail) {
return Column.getAsRightType(type, flags, obj, l, throwOnFail, false);
}
@Override
public Object getAsRightType(int type, int flags, Object obj, String format, int l, boolean throwOnFail) {
return Column.getAsRightType(type, flags, obj, format, l, null, throwOnFail, false);
}
}, table.getRowIdentColumns().get(0), Debug.LOGGER);
}
if (or != null) {
ISQLCondition condition;
if (c instanceof AggregateVariable) {
condition = createExistsCondition(application.getFlattenedSolution(), sqlSelect, or, rfs.getRelations(), columnTable, provider, pkQueryColumns.toArray(new QueryColumn[pkQueryColumns.size()]));
} else {
condition = or;
}
and = AndCondition.and(and, condition);
}
}
}
return and;
}
use of com.servoy.j2db.util.FormatParser.ParsedFormat in project servoy-client by Servoy.
the class AbstractRuntimeValuelistComponent method setValueListItems.
public void setValueListItems(Object value) {
if (getComponent() instanceof ISupportValueList) {
IValueList list = ((ISupportValueList) getComponent()).getValueList();
if (list != null && (value instanceof JSDataSet || value instanceof IDataSet)) {
String name = list.getName();
ValueList valuelist = application.getFlattenedSolution().getValueList(name);
if (valuelist != null && valuelist.getValueListType() == ValueList.CUSTOM_VALUES) {
ParsedFormat format = null;
int type = 0;
if (list instanceof CustomValueList) {
format = ((CustomValueList) list).getFormat();
type = ((CustomValueList) list).getValueType();
}
IValueList newVl = ValueListFactory.fillRealValueList(application, valuelist, ValueList.CUSTOM_VALUES, format, type, value);
((ISupportValueList) getComponent()).setValueList(newVl);
}
}
}
}
use of com.servoy.j2db.util.FormatParser.ParsedFormat in project servoy-client by Servoy.
the class ValueListPropertyType method toSabloComponentValue.
@Override
public ValueListTypeSabloValue toSabloComponentValue(Object rhinoValue, ValueListTypeSabloValue previousComponentValue, PropertyDescription pd, IWebObjectContext webObjectContext) {
if (rhinoValue == null || RhinoConversion.isUndefinedOrNotFound(rhinoValue))
return null;
if (previousComponentValue == null) {
return rhinoValue instanceof String ? createValuelistSabloValueByNameFromRhino((String) rhinoValue, pd, webObjectContext) : null;
}
if (!previousComponentValue.isInitialized()) {
if (rhinoValue instanceof String) {
// weird; but we are going to create a new value anyway so it doesn't matter much
return createValuelistSabloValueByNameFromRhino((String) rhinoValue, pd, webObjectContext);
} else {
// we cannot set values from a dataset if the previous value is not ready for it
Debug.error("Trying to make changes (assignment) to an uninitialized valuelist property (this is not allowed): " + pd + " of " + webObjectContext, new RuntimeException());
return previousComponentValue;
}
}
ParsedFormat format = null;
int type = -1;
IValueList list = previousComponentValue.getValueList();
if (list.getName().equals(rhinoValue)) {
// no need to create a new value if we have the same valuelist name
return previousComponentValue;
}
ValueListTypeSabloValue newValue;
IValueList newVl = null;
// see if it's a component.setValuelistItems (legacy) equivalent
if (list != null && list instanceof CustomValueList && (rhinoValue instanceof JSDataSet || rhinoValue instanceof IDataSet)) {
// here we create a NEW, separate (runtime) custom valuelist instance for this component only (no longer the 'global' custom valuelist with that name that can be affected by application.setValuelistItems(...))
INGApplication application = previousComponentValue.getDataAdapterList().getApplication();
ValueList valuelist = application.getFlattenedSolution().getValueList(list.getName());
if (valuelist != null && valuelist.getValueListType() == IValueListConstants.CUSTOM_VALUES) {
format = ((CustomValueList) list).getFormat();
type = ((CustomValueList) list).getValueType();
newVl = ValueListFactory.fillRealValueList(application, valuelist, IValueListConstants.CUSTOM_VALUES, format, type, rhinoValue);
if (newVl != null) {
previousComponentValue.setNewCustomValuelistInstance(newVl, rhinoValue);
newValue = previousComponentValue;
} else {
// should never happen; ValueListFactory.fillRealValueList seems to always return non-null
Debug.error("Assignment to Valuelist typed property '" + pd.getName() + "' of component '" + webObjectContext + "' failed for an unknown reason; dataset: " + rhinoValue, new RuntimeException());
// just keep old value
newValue = previousComponentValue;
}
} else {
Debug.error("Assignment to Valuelist typed property '" + pd.getName() + "' of component '" + webObjectContext + "' failed. Assigning a dataset is ONLY allowed for custom valuelists; dataset: " + rhinoValue, new RuntimeException());
newValue = previousComponentValue;
}
} else if (rhinoValue instanceof String) {
// the Rhino value is a different valuelist name; create a full new one
newValue = createValuelistSabloValueByNameFromRhino((String) rhinoValue, pd, webObjectContext);
} else {
Debug.error("Assignment to Valuelist typed property '" + pd.getName() + "' of component '" + webObjectContext + "' failed. Assigning this value is not supported: " + rhinoValue, new RuntimeException());
// whatever was set here is not supported; so keep the previous value
newValue = previousComponentValue;
}
return newValue;
}
Aggregations