Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.srg.smartclient.isomorphic.*;
import org.srg.smartclient.runtime.IDSRuntime;

import java.util.*;

public abstract class AbstractDSHandler extends RelationSupport implements DSHandler {
private static final String META_DATA_PREFIX = "_";
private static final Logger logger = LoggerFactory.getLogger(AbstractDSHandler.class);

private final IDSRegistry dsRegistry;
private final IDSRuntime dsRuntime;
private final DataSource datasource;
private transient Map<DSRequest.OperationType, List<OperationBinding>> bindingsMap;

public AbstractDSHandler(IDSRegistry dsRegistry, DataSource datasource) {
this.dsRegistry = dsRegistry;
public AbstractDSHandler(IDSRuntime dsRuntime, DataSource datasource) {
this.dsRuntime = dsRuntime;
this.datasource = datasource;
}

Expand Down Expand Up @@ -120,33 +121,29 @@ private DSResponse failureDueToUnsupportedOperation(DSRequest request) {
);
}

protected DSHandler getDataSourceHandlerById(String id) {
assert dsRegistry != null;
return dsRegistry.getDataSourceHandlerById(id);
protected IDSRuntime getDsRuntime() {
return dsRuntime;
}

protected DataSource getDataSourceById(String dsId) {
assert dsRegistry != null;
return dsRegistry.getDataSourceById(dsId);
protected DSHandler getDataSourceHandlerById(String id) {
assert dsRuntime != null;
return dsRuntime.getDataSourceHandlerById(id);
}

protected DataSource getDatasourceByTableName(String tableName) {
assert dsRegistry != null;
protected ImportFromRelation getImportFromRelation(DSField importFromField){
return getDsRuntime().getImportFromRelation(this.id(), importFromField.getName());
}

for (IHandler h: dsRegistry.handlers()) {
if (h instanceof DSHandler dsHandler) {
if (dsHandler.dataSource().getTableName().equalsIgnoreCase(tableName)) {
return dsHandler.dataSource();
}
}
}
return null;
protected ForeignKeyRelation getForeignKeyRelation(DSField importFromField){
return getDsRuntime().getForeignKeyRelation(this.id(), importFromField.getName());
}

@Deprecated(since ="Use getImportFromRelation() instead", forRemoval = true)
protected ImportFromRelation describeImportFrom(DSField importFromField) {
return RelationSupport.describeImportFrom(this::getDataSourceHandlerById, this.getDataSource(), importFromField);
}

@Deprecated(since ="Use getForeignKeyRelation() instead", forRemoval = true)
protected ForeignKeyRelation describeForeignKey(DSField foreignKeyField) {
return RelationSupport.describeForeignKey(this::getDataSourceHandlerById, this.getDataSource(), foreignKeyField);
}
Expand All @@ -158,17 +155,21 @@ protected ForeignRelation describeForeignRelation(DataSource dataSource, DSField
protected ForeignRelation determineEffectiveField(DSField dsf) {
final DataSource effectiveDS;
final DSField effectiveField;
final String effectiveRelatedTableAlias;

if (dsf.isIncludeField()) {
final ImportFromRelation relation = describeImportFrom(dsf);
final ImportFromRelation relation = getImportFromRelation(dsf);
effectiveDS = relation.getLast().foreign().dataSource();
effectiveField = relation.foreignDisplay();
effectiveRelatedTableAlias = relation.getLast().foreign().getRelatedTableAlias();
} else {
effectiveDS = getDataSource();
effectiveField = dsf;
// TODO: not sure what effectiveRelatedTableAlias should be there, figure this out later, as have more usecses
effectiveRelatedTableAlias = null;
}

return new ForeignRelation(effectiveDS.getId(), effectiveDS, effectiveField.getName(), effectiveField);
return new ForeignRelation(effectiveDS.getId(), effectiveDS, effectiveField.getName(), effectiveField, null, effectiveRelatedTableAlias);
}

protected OperationBinding getEffectiveOperationBinding(DSRequest.OperationType operationType, String operationId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.srg.smartclient.isomorphic.IDSRequestData;
import org.srg.smartclient.isomorphic.criteria.AdvancedCriteria;
import org.srg.smartclient.isomorphic.criteria.Criteria;
import org.srg.smartclient.runtime.IDSRuntime;

import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand All @@ -18,8 +19,8 @@
* https://stackoverrun.com/ru/q/5891230
*/
public class AdvancedJDBCHandler extends JDBCHandler {
public AdvancedJDBCHandler(JDBCPolicy jdbcPolicy, IDSRegistry dsRegistry, DataSource datasource) {
super(jdbcPolicy, dsRegistry, datasource);
public AdvancedJDBCHandler(JDBCPolicy jdbcPolicy, IDSRuntime scRuntime, DataSource datasource) {
super(jdbcPolicy, scRuntime, datasource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.LoggerFactory;
import org.srg.smartclient.isomorphic.DSField;
import org.srg.smartclient.isomorphic.DataSource;
import org.srg.smartclient.runtime.IDSRuntime;

import java.util.Collection;
import java.util.Map;
Expand All @@ -23,11 +24,11 @@ private static class BuilderContext extends RelationSupport {
private String dsName;
private int qntGeneratedFields;
private StringBuilder builder;
private final IDSRegistry dsRegistry;
private final IDSRuntime dsRuntime;
private final DataSource dataSource;

public BuilderContext(IDSRegistry dsRegistry, DataSource dataSource) {
this.dsRegistry = dsRegistry;
public BuilderContext(IDSRuntime dsRuntime, DataSource dataSource) {
this.dsRuntime = dsRuntime;
this.dataSource = dataSource;

clear();
Expand Down Expand Up @@ -64,17 +65,18 @@ public void write_if_notBlank(String str, String fmt, Object... args) {
// }

public ForeignKeyRelation describeForeignKey(DSField foreignKeyField) {
return RelationSupport.describeForeignKey(this.dsRegistry, this.dataSource, foreignKeyField);
// return RelationSupport.describeForeignKey(this.scRuntime, this.dataSource, foreignKeyField);
return this.dsRuntime.getForeignKeyRelation(this.dataSource.getId(), foreignKeyField.getName());
}
}

public static String build(IDSRegistry dsRegistry, String dispatcherUrl, DSHandler dsHandler) throws ClassNotFoundException {
return build(dsRegistry, dispatcherUrl, dsHandler.dataSource(), dsHandler.allowAdvancedCriteria());
public static String build(IDSRuntime scRuntime, String dispatcherUrl, DSHandler dsHandler) throws ClassNotFoundException {
return build(scRuntime, dispatcherUrl, dsHandler.dataSource(), dsHandler.allowAdvancedCriteria());
}

public static String build(IDSRegistry dsRegistry, String dispatcherUrl, DataSource dataSource, boolean allowAdvancedCriteria) throws ClassNotFoundException {
public static String build(IDSRuntime scRuntime, String dispatcherUrl, DataSource dataSource, boolean allowAdvancedCriteria) throws ClassNotFoundException {

final BuilderContext ctx = new BuilderContext(dsRegistry, dataSource);
final BuilderContext ctx = new BuilderContext(scRuntime, dataSource);

ctx.dsName = dataSource.getId();
final Collection<DSField> allFields = dataSource.getFields();
Expand Down
136 changes: 77 additions & 59 deletions smartclient-core/src/main/java/org/srg/smartclient/DSDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.slf4j.LoggerFactory;
import org.srg.smartclient.dmi.JDKDMIHandlerFactory;
import org.srg.smartclient.isomorphic.*;
import org.srg.smartclient.runtime.DSRuntime;
import org.srg.smartclient.runtime.IDSRuntime;
import org.srg.smartclient.utils.ContextualRuntimeException;
import org.srg.smartclient.utils.Serde;
import org.srg.smartclient.utils.Utils;
Expand All @@ -31,10 +33,11 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class DSDispatcher implements IDSDispatcher {
public class DSDispatcher implements IDSDispatcher, IDSRuntime {
private static Logger logger = LoggerFactory.getLogger(DSDispatcher.class);
private Map<String, IHandler> datasourceMap = new LinkedHashMap<>();
private DSRuntime dsRuntime = DSRuntime.create(Collections.emptyList());
private JDBCHandlerFactory jdbcHandlerFactory = new JDBCHandlerFactory();
private JDBCHandler.JDBCPolicy jdbcPolicy;
private JDKDMIHandlerFactory dmiHandlerFactory;
Expand All @@ -53,11 +56,24 @@ protected JDBCHandler.JDBCPolicy getJdbcPolicy() {
return jdbcPolicy;
}

@Override
public Iterator<IHandler> iterator() {
return dsRuntime.iterator();
}

@Override
public RelationSupport.ForeignKeyRelation getForeignKeyRelation(String dsId, String fieldName) {
return dsRuntime.getForeignKeyRelation(dsId, fieldName);
}

@Override
public RelationSupport.ImportFromRelation getImportFromRelation(String dsId, String fieldName) {
return dsRuntime.getImportFromRelation(dsId, fieldName);
}

@Override
public IHandler getHandlerByName(String dsId) {
final IHandler ds = datasourceMap.get(dsId);
return ds;
public IHandler getHandlerById(String dsId) {
return dsRuntime.getHandlerById(dsId);

// if (ds != null) {
// return ds;
Expand All @@ -66,27 +82,6 @@ public IHandler getHandlerByName(String dsId) {
// return Utils.throw_it("Unknown datasource '%s'", dsId);
}

// @Override
// public DataSource getDataSourceById(String dsId) {
// final IHandler handler = datasourceMap.get(dsId);
// if (handler == null){
// return null;
// }
//
// if (handler instanceof DSHandler dsHandler) {
// return dsHandler.dataSource();
// }
//
// throw new RuntimeException("Handler '%s' is not an instance of 'DSHandler'."
// .formatted(dsId)
// );
// }

@Override
public Collection<IHandler> handlers() {
return datasourceMap.values();
}

private ObjectWriter createObjectWriter() {
final SimpleFilterProvider filterProvider = new SimpleFilterProvider();

Expand Down Expand Up @@ -131,7 +126,7 @@ public void writeIndentation(JsonGenerator jg, int level) throws IOException {

protected DSResponse handleRequest(DSRequest request) {
try {
final IHandler ds = getHandlerByName(request.getDataSource());
final IHandler ds = getHandlerById(request.getDataSource());
final DSResponse response = ds.handle(request);

response.setOperationId( request.getOperationId());
Expand Down Expand Up @@ -237,12 +232,19 @@ public <A extends Appendable> A generateDSJavaScript(A out, String dispatcherUrl
out.append("const DISPATCHER_URL = \"%s\";\n"
.formatted(dispatcherUrl));

/*
* If dsIds is not provided, -- generate all registered handlers
*/
if (dsId.length == 0) {
dsId = datasourceMap.keySet().toArray(new String[0]);
final List<String> ids = StreamSupport.stream(dsRuntime.spliterator(), false)
.map(IHandler::id)
.collect(Collectors.toList());

dsId = ids.toArray(ids.toArray(new String[0]));
}

for (String name : dsId) {
final IHandler handler = getHandlerByName(name);
final IHandler handler = getHandlerById(name);

if (handler instanceof DSHandler ds) {
out.append(DSDeclarationBuilder.build(this, dispatcherUrl, ds));
Expand Down Expand Up @@ -326,7 +328,7 @@ public void loadFromResource(String path) throws Exception {
logger.info("Resource URL: %s".formatted( url1));

if (url.getProtocol().equals("jar")) {
logger.debug("Scanning a jar file via URL: %s".formatted( url1));
logger.debug("Scanning a jar file via URL: %s...".formatted( url1));


/* A JAR path */
Expand Down Expand Up @@ -369,23 +371,27 @@ public void loadFromResource(String path) throws Exception {
}
}

logger.debug("""
Jar scan was completed, the following matches were found in the '%s':
"""
.formatted(
url1,
result.stream()
.collect(Collectors.joining(",\n"))
)
);
files.addAll(result);
if (result.isEmpty()) {
logger.debug("Jar scan was completed, nothing ound (jar: '%s')".formatted(url1));
} else {
logger.debug(
"Jar scan was completed, %d matches were found(jar: '%s'):\n%s"
.formatted(
result.size(),
url1,
result.stream()
.collect(Collectors.joining(",\n ", " ", ""))
)
);
files.addAll(result);
}
} else {
File f = new File(url.getFile());

List<String> result;
if (f.isDirectory()) {

logger.info("loading Data Sources from resources, scanning resource directory '%s'."
logger.debug("Scanning resource directory '%s'..."
.formatted(f));


Expand All @@ -396,44 +402,56 @@ public void loadFromResource(String path) throws Exception {
result = List.of(f.getName());
}

logger.debug("""
Directory scan was completed, the following matches were found in the '%s':
"""
.formatted(
url1,
result.stream()
.collect(Collectors.joining(",\n"))
)
);
if (result.isEmpty()) {
logger.debug("Directory scan was completed, nothing found (jar: '%s')".formatted(url1));
} else {
logger.debug("Directory scan was completed, %d matches were found(dir: '%s'):\n%s"
.formatted(
result.size(),
url1,
result.stream()
.collect(Collectors.joining(",\n "," ",""))
)
);
}

files.addAll(result);
}
}

logger.info("""
BUILTIN RESOURCES SCAN COMPLETED, the following matches were found in the '%s':
%s
""".formatted(
logger.info("BUILTIN RESOURCES SCAN COMPLETED, %d matches were found(path '%s'):\n%s"
.formatted(
files.size(),
url,
files.stream()
.collect(Collectors.joining(",\n "))
.collect(Collectors.joining(",\n ", " ", ""))
)
);

final List<IHandler> handlers = new LinkedList<>();
for (String s: files ) {
try {
final IHandler ds = loadDsFromResource(new File(s));
registerHandler(ds);
handlers.add(ds);
} catch (Exception ex) {
logger.warn("Can't load Smart Client DSHandler from file '%s'".formatted(s), ex);
}
}

registerHandlers(handlers.toArray(new IHandler[0]));
}

@Override
public void registerHandler(IHandler handler) {
datasourceMap.put(handler.id(), handler);
logger.info("A new DSHandler has been registered as '%s' ".formatted(handler.id()));
public void registerHandlers(IHandler... handlers) {
dsRuntime = dsRuntime.registerHandlers(handlers);

logger.info("Following Handlers have been registered:\n%s"
.formatted(
Arrays.stream(handlers)
.map( IHandler::id)
.collect(Collectors.joining(",\n ", " ", ""))
)
);
}

@JsonFilter("PropertyFilter")
Expand Down
Loading