use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class FlattenedSolution method getDataproviderLookup.
public IDataProviderLookup getDataproviderLookup(IFoundSetManagerInternal foundSetManager, final IPersist p) {
IDataProviderLookup retval = null;
synchronized (this) {
if (dataProviderLookups == null)
dataProviderLookups = new HashMap<IPersist, IDataProviderLookup>();
retval = dataProviderLookups.get(p);
if (retval != null)
return retval;
}
if (p instanceof Form) {
ITable t = null;
try {
if (foundSetManager == null) {
t = getTable(((Form) p).getDataSource());
} else {
t = foundSetManager.getTable(((Form) p).getDataSource());
}
} catch (RepositoryException e) {
Debug.error(e);
}
retval = new FormAndTableDataProviderLookup(this, (Form) p, t);
} else if (p instanceof Portal) {
ITable t = null;
Relation[] relations = getRelationSequence(((Portal) p).getRelationName());
if (relations == null) {
return null;
}
t = getTable(relations[relations.length - 1].getForeignDataSource());
retval = new FormAndTableDataProviderLookup(this, (Form) p.getParent(), t);
} else // solution
{
retval = new IDataProviderLookup() {
public IDataProvider getDataProvider(String id) throws RepositoryException {
return getGlobalDataProvider(id);
}
public Table getTable() throws RepositoryException {
return null;
}
};
}
synchronized (this) {
dataProviderLookups.put(p, retval);
}
return retval;
}
use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class FlattenedSolution method getValuelistSortRelation.
/**
* Get the internal relation that can be used to sort on this value list using the display values.
* @param valueList
* @param callingTable
* @param dataProviderID
* @param foundSetManager
* @return
* @throws RepositoryException
*/
public Relation getValuelistSortRelation(ValueList valueList, Table callingTable, String dataProviderID, IFoundSetManagerInternal foundSetManager) throws RepositoryException {
if (callingTable == null || valueList == null) {
return null;
}
String destDataSource;
Relation[] relationSequence;
String relationPrefix;
switch(valueList.getDatabaseValuesType()) {
case IValueListConstants.TABLE_VALUES:
// create an internal relation
relationSequence = null;
// $NON-NLS-1$
relationPrefix = "";
destDataSource = valueList.getDataSource();
break;
case IValueListConstants.RELATED_VALUES:
// replace the last relation in the sequence with an internal relation
relationSequence = getRelationSequence(valueList.getRelationName());
if (relationSequence == null) {
return null;
}
if (relationSequence.length > 1) {
for (Relation r : relationSequence) {
if (r.getJoinType() != INNER_JOIN) {
// outer join on the intermediate tables causes extra results that influence the sorting result
return null;
}
}
}
StringBuilder sb = new StringBuilder();
for (Relation r : relationSequence) {
sb.append('-').append(r.getName());
}
relationPrefix = sb.toString();
destDataSource = relationSequence[relationSequence.length - 1].getForeignDataSource();
break;
default:
return null;
}
if (destDataSource == null || !DataSourceUtils.isSameServer(callingTable.getDataSource(), destDataSource)) {
// do not create a cross-server relation
return null;
}
Table destTable = (Table) foundSetManager.getTable(destDataSource);
if (destTable == null) {
return null;
}
String relationName = // $NON-NLS-1$
Relation.INTERNAL_PREFIX + "VL-" + callingTable.getDataSource() + '-' + dataProviderID + relationPrefix + '-' + valueList.getName() + '-';
synchronized (this) {
Column callingColumn = callingTable.getColumn(dataProviderID);
if (callingColumn == null) {
return null;
}
Relation relation = getRelation(relationName);
if (relation == null) {
// create in internal relation
String dp;
int returnValues = valueList.getReturnDataProviders();
if ((returnValues & 1) != 0) {
dp = valueList.getDataProviderID1();
} else if ((returnValues & 2) != 0) {
dp = valueList.getDataProviderID2();
} else if ((returnValues & 4) != 0) {
dp = valueList.getDataProviderID3();
} else {
return null;
}
Column destColumn = destTable.getColumn(dp);
if (destColumn == null) {
return null;
}
// create internal value list relation
QueryTable callingQTable = new QueryTable(callingTable.getSQLName(), callingTable.getDataSource(), callingTable.getCatalog(), callingTable.getSchema());
QueryTable destQTable = new QueryTable(destTable.getSQLName(), destTable.getDataSource(), destTable.getCatalog(), destTable.getSchema());
List<ISQLTableJoin> joins = new ArrayList<ISQLTableJoin>();
ISQLTableJoin lastJoin = null;
if (relationSequence == null) {
// table values
joins.add(lastJoin = new QueryJoin(relationName, callingQTable, destQTable, new AndCondition(), LEFT_OUTER_JOIN, false));
if (// apply name as filter on column valuelist_name
valueList.getUseTableFilter()) {
lastJoin.getCondition().addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, new QueryColumn(destQTable, DBValueList.NAME_COLUMN), valueList.getName()));
}
} else {
// related values
QueryTable primaryQTable = callingQTable;
for (int i = 0; i < relationSequence.length; i++) {
Relation r = relationSequence[i];
QueryTable foreignQTable;
if (i == relationSequence.length - 1) {
// last one
foreignQTable = destQTable;
} else {
ITable relForeignTable = getTable(r.getForeignDataSource());
if (relForeignTable == null) {
return null;
}
foreignQTable = new QueryTable(relForeignTable.getSQLName(), relForeignTable.getDataSource(), relForeignTable.getCatalog(), relForeignTable.getSchema());
}
lastJoin = SQLGenerator.createJoin(this, r, primaryQTable, foreignQTable, false, new IGlobalValueEntry() {
public Object setDataProviderValue(String dpid, Object value) {
return null;
}
public Object getDataProviderValue(String dpid) {
// A value will be added when the relation is used, see SQLGenerator.createJoin
return new Placeholder(new ObjectPlaceholderKey<int[]>(null, dpid));
}
public boolean containsDataProvider(String dpid) {
return false;
}
});
joins.add(lastJoin);
primaryQTable = foreignQTable;
}
}
// add condition for return dp id
lastJoin.getCondition().addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, destColumn.queryColumn(destQTable), callingColumn.queryColumn(callingQTable)));
relation = getSolutionCopy().createNewRelation(new ScriptNameValidator(this), relationName, callingTable.getDataSource(), destDataSource, LEFT_OUTER_JOIN);
ISQLTableJoin join;
if (joins.size() == 1) {
join = lastJoin;
} else {
// combine joins
join = new QueryCompositeJoin(relationName, joins);
}
relation.setRuntimeProperty(Relation.RELATION_JOIN, join);
}
return relation;
}
}
use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class BasicFormController method setModel.
protected boolean setModel(IFoundSetInternal newModel) throws ServoyException {
if (newModel == formModel || adjustingModel) {
// same or adjusting do nothing
return true;
}
ITable formTable = application.getFoundSetManager().getTable(form.getDataSource());
if (newModel != null && ((formTable == null && newModel.getTable() != null) || (formTable != null && !formTable.equals(newModel.getTable())))) {
throw new IllegalArgumentException(application.getI18NMessage("servoy.formPanel.error.wrongFoundsetTable", new Object[] { // $NON-NLS-1$
newModel.getTable() == null ? "NONE" : newModel.getTable().getName(), // $NON-NLS-1$
form.getTableName() }));
}
try {
IView view = getViewComponent();
if (view != null && view.isEditing()) {
// TODO if save fails don't set the newModel??
int stopped = application.getFoundSetManager().getEditRecordList().stopEditing(false);
if (stopped != ISaveConstants.STOPPED && stopped != ISaveConstants.AUTO_SAVE_BLOCKED) {
return false;
}
}
adjustingModel = true;
if (formModel != null) {
try {
((ISwingFoundSet) formModel).getSelectionModel().removeListSelectionListener(this);
((ISwingFoundSet) formModel).getSelectionModel().removeFormController(this);
((ISwingFoundSet) formModel).removeTableModelListener(this);
// to make sure all data is gc'ed
if (formModel instanceof FoundSet)
((FoundSet) formModel).flushAllCachedItems();
} catch (Exception ex) {
Debug.error(ex);
}
}
setFormModelInternal(newModel == null ? ((FoundSetManager) application.getFoundSetManager()).getEmptyFoundSet(this) : newModel);
if (formScope != null) {
// $NON-NLS-1$
formScope.putWithoutFireChange("foundset", formModel);
if (formScope.getPrototype() == null) {
formScope.setPrototype(new SelectedRecordScope(this, formTable == null ? null : application.getScriptEngine().getTableScope(formTable)));
}
}
if (isFormVisible) {
((ISwingFoundSet) formModel).getSelectionModel().addListSelectionListener(this);
((ISwingFoundSet) formModel).getSelectionModel().addFormController(this);
((ISwingFoundSet) formModel).addTableModelListener(this);
if (// it may not yet exist
view != null) {
view.setModel(formModel);
}
// this was former a call to aggregateChange, but now does now unwanted parent traverse...
int[] idx = null;
if (getView() == RECORD_VIEW || getView() == LOCKED_RECORD_VIEW) {
int selIdx = formModel.getSelectedIndex();
if (selIdx != -1)
idx = new int[] { selIdx };
} else {
idx = formModel.getSelectedIndexes();
}
if (idx == null || idx.length == 0) {
refreshAllPartRenderers(new IRecordInternal[] { formModel.getPrototypeState() });
} else {
IRecordInternal[] row = new IRecordInternal[idx.length];
for (int i = 0; i < idx.length; i++) row[i] = formModel.getRecord(idx[i]);
refreshAllPartRenderers(row);
}
}
} finally {
adjustingModel = false;
}
return true;
}
use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class FindState method createFindStateJoins.
/**
* Find all processable related find states and create joins. A find state is processable when it has changed or when a related find state has changed.
* @param sqlSelect
* @param relations path to this state
* @param selectTable
* @param provider
* @return
* @throws RepositoryException
*/
public List<RelatedFindState> createFindStateJoins(QuerySelect sqlSelect, List<IRelation> relations, BaseQueryTable selectTable, IGlobalValueEntry provider) throws RepositoryException {
List<RelatedFindState> relatedFindStates = null;
List<Relation> searchRelations = getValidSearchRelations();
// find processable find states of related find states
for (Relation relation : searchRelations) {
if (relation != null) {
IFoundSetInternal set = relatedStates.get(relation.getName());
if (set != null && set.getSize() > 0) {
ISQLTableJoin existingJoin = (ISQLTableJoin) sqlSelect.getJoin(selectTable, relation.getName());
BaseQueryTable foreignQTable;
if (existingJoin == null) {
ITable foreignTable = parent.getFoundSetManager().getApplication().getFlattenedSolution().getTable(relation.getForeignDataSource());
foreignQTable = new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema());
} else {
foreignQTable = existingJoin.getForeignTable();
}
FindState fs = (FindState) set.getRecord(0);
List<IRelation> nextRelations = new ArrayList<IRelation>(relations);
nextRelations.add(relation);
List<RelatedFindState> rfs = fs.createFindStateJoins(sqlSelect, nextRelations, foreignQTable, provider);
if (rfs != null && rfs.size() > 0) {
// changed related findstate, add self with join
if (relatedFindStates == null) {
relatedFindStates = rfs;
} else {
relatedFindStates.addAll(rfs);
}
if (existingJoin == null) {
sqlSelect.addJoin(SQLGenerator.createJoin(parent.getFoundSetManager().getApplication().getFlattenedSolution(), relation, selectTable, foreignQTable, false, provider));
}
}
}
}
}
// add yourself if you have changed or one or more related states has changed
if (isChanged() || (relatedFindStates != null && relatedFindStates.size() > 0)) {
if (relatedFindStates == null) {
relatedFindStates = new ArrayList<RelatedFindState>();
}
relatedFindStates.add(new RelatedFindState(this, relations, selectTable));
}
return relatedFindStates;
}
use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class I18NMessagesTable method createMessagesTable.
public static ITable createMessagesTable(IServerInternal server, String tablename, int primaryKeySequenceType) throws RepositoryException, SQLException {
// Create the table in the repository.
IValidateName validator = DummyValidator.INSTANCE;
ITable table = server.createNewTable(validator, tablename);
if (primaryKeySequenceType == ColumnInfo.UUID_GENERATOR) {
Column column = table.createNewColumn(validator, "message_id", IColumnTypes.TEXT, 36);
column.setDatabasePK(true);
column.setSequenceType(primaryKeySequenceType);
column.setFlag(IBaseColumn.UUID_COLUMN, true);
} else {
Column column = table.createNewColumn(validator, "message_id", Types.INTEGER, 0, 0, false, true);
column.setSequenceType(primaryKeySequenceType);
}
table.createNewColumn(validator, "message_key", Types.VARCHAR, 150, 0, false);
Column messageLanguage = table.createNewColumn(validator, "message_language", Types.VARCHAR, 150, 0, true);
table.createNewColumn(validator, "message_value", Types.VARCHAR, 2000, 0, true);
server.syncTableObjWithDB(table, false, false);
try {
server.createIndex(table, table.getName() + "_m_l", new Column[] { messageLanguage }, false);
} catch (Exception e) {
Debug.error("Failed to create an index on the messages table", e);
}
return table;
}
Aggregations