use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.
the class UsedDataProviderTracker method usedFromRecord.
/**
* Used name from Record,
* @param record may be null (for global relations)
* @param name
*/
protected void usedFromRecord(IRecordInternal record, String name) {
IRecordInternal currentRecord = record;
try {
// $NON-NLS-1$
String[] parts = name.split("\\.");
for (String part : parts) {
Relation relation = flattenedSolution.getRelation(part);
if (relation != null) {
// calc depends on the relation, add a dependency for the primary data providers for the relation
IDataProvider[] primaryDataProviders = relation.getPrimaryDataProviders(flattenedSolution);
for (IDataProvider prim : primaryDataProviders) {
if (prim instanceof LiteralDataprovider)
continue;
String primdp = prim.getDataProviderID();
if (ScopesUtils.isVariableScope(primdp)) {
// global
usedGlobal(primdp);
} else {
// column
if (currentRecord != null) {
if (currentRecord.getRawData() == null) {
if (currentRecord instanceof PrototypeState) {
Debug.trace("Calculation '" + name + "' depends on field '" + part + "' from PrototypeState " + currentRecord);
} else {
// should not happen
Debug.error("Unexpected state: calculation '" + name + "' depends on column '" + part + "' from record without pk: " + currentRecord, new IllegalStateException());
}
} else {
usedColumn(currentRecord.getParentFoundSet().getDataSource(), currentRecord.getRawData().getPKHashKey(), primdp);
}
}
}
}
IFoundSetInternal foundSet = null;
if (currentRecord != null)
foundSet = currentRecord.getRelatedFoundSet(relation.getName());
currentRecord = null;
if (foundSet instanceof RelatedFoundSet) {
usedRelatedFoundSet(relation.getName(), (RelatedFoundSet) foundSet);
currentRecord = foundSet.getRecord(foundSet.getSelectedIndex());
}
} else {
if (currentRecord != null) {
IFoundSetInternal foundSet = currentRecord.getParentFoundSet();
if (foundSet.getSQLSheet().containsAggregate(part)) {
// aggregate
usedAggregate(foundSet, part);
} else {
// field or calc
if (currentRecord.has(part)) {
if (currentRecord.getRawData() == null) {
if (currentRecord instanceof PrototypeState) {
Debug.trace("Calculation '" + name + "' depends on field '" + part + "' from PrototypeState " + currentRecord);
} else {
// should not happen
Debug.error("Unexpected state: calculation '" + name + "' depends on field '" + part + "' from record without pk: " + currentRecord, new IllegalStateException());
}
} else {
usedColumn(foundSet.getDataSource(), currentRecord.getRawData().getPKHashKey(), part);
}
}
}
}
return;
}
if (currentRecord == null) {
return;
}
}
} catch (RepositoryException e) {
Debug.error(e);
}
}
use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.
the class ViewFoundSet method getRelatedFoundSet.
/*
* (non-Javadoc)
*
* @see com.servoy.j2db.dataprocessing.IFoundSetInternal#getRelatedFoundSet(com.servoy.j2db.dataprocessing.IRecordInternal, java.lang.String,
* java.util.List)
*/
@Override
public IFoundSetInternal getRelatedFoundSet(IRecordInternal record, String fullRelationName, List<SortColumn> defaultSortColumns) throws ServoyException {
if (fullRelationName == null) {
return null;
}
IFoundSetInternal retval = null;
IRecordInternal currentRecord = record;
// $NON-NLS-1$
String[] parts = fullRelationName.split("\\.");
for (int i = 0; i < parts.length; i++) {
// if this is a findstate and that is not the source record then leave the relation lookup to the findstate itself.
if (currentRecord instanceof FindState && i != 0) {
String leftPart = parts[i];
for (int k = i + 1; k < parts.length; k++) {
// $NON-NLS-1$
leftPart += "." + parts[k];
}
return currentRecord.getRelatedFoundSet(leftPart);
}
RowManager rowManager = manager.getRowManager(table.getDataSource());
SQLSheet relatedSheet = rowManager.getSQLSheet().getRelatedSheet(getFoundSetManager().getApplication().getFlattenedSolution().getRelation(parts[i]), ((FoundSetManager) getFoundSetManager()).getSQLGenerator());
if (relatedSheet == null) {
retval = getFoundSetManager().getGlobalRelatedFoundSet(parts[i]);
} else {
retval = ((FoundSetManager) getFoundSetManager()).getRelatedFoundSet(currentRecord, relatedSheet, parts[i], defaultSortColumns);
if (retval != null) {
if (retval.getSize() == 0 && !currentRecord.existInDataSource()) {
Relation r = getFoundSetManager().getApplication().getFlattenedSolution().getRelation(parts[i]);
if (// TODO add unique column test instead of pk requirement
r != null && r.isExactPKRef(getFoundSetManager().getApplication().getFlattenedSolution())) {
((FoundSet) retval).newRecord(record.getRawData(), 0, true, false);
}
}
retval.addParent(currentRecord);
}
}
if (retval == null) {
return null;
}
if (i < parts.length - 1) {
currentRecord = retval.getRecord(retval.getSelectedIndex());
if (currentRecord == null) {
return null;
}
}
}
return retval;
}
use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.
the class LookupValueList method deregister.
public void deregister() {
clear();
if (tableListener != null && table != null) {
FlattenedSolution fs = application.getFlattenedSolution();
((FoundSetManager) application.getFoundSetManager()).removeTableListener(table, tableListener);
Relation[] relations = application.getFlattenedSolution().getRelationSequence(valueList.getRelationName());
for (int i = 0; relations != null && i < relations.length - 1; i++) {
((FoundSetManager) application.getFoundSetManager()).removeTableListener(fs.getTable(relations[i].getForeignDataSource()), tableListener);
}
tableListener = null;
}
}
use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.
the class PrototypeState method getRelatedFoundSet.
/**
* Get related foundset, relationName may be multiple-levels deep
*/
@Override
public IFoundSetInternal getRelatedFoundSet(String relationName, List<SortColumn> defaultSortColumns) {
if (parent != null) {
if (relationName != null) {
int dot = relationName.indexOf('.');
String firstRelation = (dot > 0) ? relationName.substring(0, dot) : relationName;
Relation relation = parent.getFoundSetManager().getApplication().getFlattenedSolution().getRelation(firstRelation);
if (// only do handle global relations or pass the same foundset
relation != null && (relation.isGlobal() || relation.isParentRef())) {
return super.getRelatedFoundSet(relationName, defaultSortColumns);
}
}
}
return null;
}
use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.
the class JSSolutionModel method getRelations.
/**
* @clonedesc getRelations(String)
* @sampleas getRelations(String)
* @param servername the specified name of the server for the specified table
* @param tablename the specified name of the table
*
* @return an array of all relations (all elements in the array are of type JSRelation)
*/
@JSFunction
public JSRelation[] getRelations(String servername, String tablename) {
FlattenedSolution fs = application.getFlattenedSolution();
try {
Table primaryTable = null;
if (servername != null && tablename != null) {
IServer primaryServer = fs.getSolution().getServer(servername);
if (primaryServer == null)
throw new RuntimeException("can't list relations, primary server not found: " + servername);
primaryTable = (Table) primaryServer.getTable(tablename);
if (primaryTable == null)
throw new RuntimeException("can't list relations, primary table not found: " + tablename);
}
List<JSRelation> relations = new ArrayList<JSRelation>();
Iterator<Relation> iterator = fs.getRelations(primaryTable, true, true);
while (iterator.hasNext()) {
Relation relation = iterator.next();
if (((primaryTable == null) == relation.isGlobal()) && !relation.isInternal()) {
relations.add(new JSRelation(relation, application, false));
}
}
return relations.toArray(new JSRelation[relations.size()]);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations