Search in sources :

Example 1 with ForeignKey

use of com.manydesigns.portofino.model.database.ForeignKey in project Portofino by ManyDesigns.

the class CrudAction method saveConfiguration.

@Override
protected boolean saveConfiguration(Object configuration) {
    CrudConfiguration crudConfiguration = (CrudConfiguration) configuration;
    List<SelectionProviderReference> sps = new ArrayList<>(crudConfiguration.getSelectionProviders());
    crudConfiguration.getSelectionProviders().clear();
    crudConfiguration.persistence = persistence;
    crudConfiguration.init();
    sps.forEach(sp -> {
        ForeignKey fk = DatabaseLogic.findForeignKeyByName(crudConfiguration.getActualTable(), sp.getSelectionProviderName());
        if (fk != null) {
            sp.setForeignKeyName(sp.getSelectionProviderName());
            sp.setSelectionProviderName(null);
        }
        if (sp.getSelectionProviderName() != null || sp.getForeignKeyName() != null) {
            crudConfiguration.getSelectionProviders().add(sp);
        }
    });
    List<CrudProperty> existingProperties = this.crudConfiguration.getProperties();
    List<CrudProperty> configuredProperties = crudConfiguration.getProperties();
    List<CrudProperty> newProperties = configuredProperties.stream().map(p1 -> {
        Optional<CrudProperty> maybeP2 = existingProperties.stream().filter(p2 -> p1.getName().equals(p2.getName())).findFirst();
        CrudProperty p2 = maybeP2.orElse(new CrudProperty());
        p2.setName(p1.getName());
        p2.setEnabled(p1.isEnabled());
        p2.setInsertable(p1.isInsertable());
        p2.setInSummary(p1.isInSummary());
        p2.setLabel(p1.getLabel());
        p2.setSearchable(p1.isSearchable());
        p2.setUpdatable(p1.isUpdatable());
        return p2;
    }).collect(Collectors.toList());
    crudConfiguration.setProperties(newProperties);
    return super.saveConfiguration(crudConfiguration);
}
Also used : Persistence(com.manydesigns.portofino.persistence.Persistence) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria) ResourceActionName(com.manydesigns.portofino.resourceactions.ResourceActionName) CrudConfiguration(com.manydesigns.portofino.resourceactions.crud.configuration.database.CrudConfiguration) QueryStringWithParameters(com.manydesigns.elements.text.QueryStringWithParameters) LoggerFactory(org.slf4j.LoggerFactory) Session(org.hibernate.Session) Autowired(org.springframework.beans.factory.annotation.Autowired) Insertable(com.manydesigns.elements.annotations.Insertable) ConfigurationClass(com.manydesigns.portofino.resourceactions.annotations.ConfigurationClass) SupportsPermissions(com.manydesigns.portofino.security.SupportsPermissions) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ElementsThreadLocals(com.manydesigns.elements.ElementsThreadLocals) DatabaseLogic(com.manydesigns.portofino.model.database.DatabaseLogic) TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) ClassAccessor(com.manydesigns.elements.reflection.ClassAccessor) Function(net.sf.jsqlparser.expression.Function) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) Table(com.manydesigns.portofino.model.database.Table) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) QueryUtils(com.manydesigns.portofino.persistence.QueryUtils) SelectionProviderReference(com.manydesigns.portofino.resourceactions.crud.configuration.database.SelectionProviderReference) AccessLevel(com.manydesigns.portofino.security.AccessLevel) Updatable(com.manydesigns.elements.annotations.Updatable) Logger(org.slf4j.Logger) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) CrudProperty(com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty) RequiresPermissions(com.manydesigns.portofino.security.RequiresPermissions) Collectors(java.util.stream.Collectors) IdStrategy(com.manydesigns.portofino.persistence.IdStrategy) RequestMessages(com.manydesigns.elements.messages.RequestMessages) Serializable(java.io.Serializable) PkHelper(com.manydesigns.portofino.util.PkHelper) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) ScriptTemplate(com.manydesigns.portofino.resourceactions.annotations.ScriptTemplate) Database(com.manydesigns.portofino.model.database.Database) Select(net.sf.jsqlparser.statement.select.Select) ForeignKey(com.manydesigns.portofino.model.database.ForeignKey) StringReader(java.io.StringReader) ActionInstance(com.manydesigns.portofino.resourceactions.ActionInstance) NotNull(org.jetbrains.annotations.NotNull) CCJSqlParserManager(net.sf.jsqlparser.parser.CCJSqlParserManager) SelectionProviderReference(com.manydesigns.portofino.resourceactions.crud.configuration.database.SelectionProviderReference) CrudProperty(com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty) ForeignKey(com.manydesigns.portofino.model.database.ForeignKey) CrudConfiguration(com.manydesigns.portofino.resourceactions.crud.configuration.database.CrudConfiguration)

Example 2 with ForeignKey

use of com.manydesigns.portofino.model.database.ForeignKey in project Portofino by ManyDesigns.

the class ConfigurationForm method writeTo.

public void writeTo(ManyToManyConfiguration m2mConfiguration) {
    m2mConfiguration.setViewType(viewType);
    m2mConfiguration.setDatabase(database);
    m2mConfiguration.setQuery(query);
    m2mConfiguration.setOneExpression(oneExpression);
    m2mConfiguration.setOnePropertyName(onePropertyName);
    if (StringUtils.isEmpty(oneSpName)) {
        m2mConfiguration.setOneSelectionProvider(null);
    } else {
        boolean found = false;
        for (ForeignKey fk : m2mConfiguration.getActualRelationTable().getForeignKeys()) {
            if (fk.getName().equals(oneSpName)) {
                SelectionProviderReference ref = new SelectionProviderReference();
                ref.setForeignKeyName(oneSpName);
                m2mConfiguration.setOneSelectionProvider(ref);
                found = true;
                break;
            }
        }
        if (!found) {
            for (ModelSelectionProvider sp : m2mConfiguration.getActualRelationTable().getSelectionProviders()) {
                if (sp.getName().equals(oneSpName)) {
                    SelectionProviderReference ref = new SelectionProviderReference();
                    ref.setSelectionProviderName(oneSpName);
                    m2mConfiguration.setOneSelectionProvider(ref);
                    break;
                }
            }
        }
    }
    if (StringUtils.isEmpty(manySpName) || m2mConfiguration.getActualRelationTable() == null) {
        m2mConfiguration.setManySelectionProvider(null);
    } else {
        boolean found = false;
        for (ForeignKey fk : m2mConfiguration.getActualRelationTable().getForeignKeys()) {
            if (fk.getName().equals(manySpName)) {
                SelectionProviderReference ref = new SelectionProviderReference();
                ref.setForeignKeyName(manySpName);
                m2mConfiguration.setManySelectionProvider(ref);
                found = true;
                break;
            }
        }
        if (!found) {
            for (ModelSelectionProvider sp : m2mConfiguration.getActualRelationTable().getSelectionProviders()) {
                if (sp.getName().equals(manySpName)) {
                    SelectionProviderReference ref = new SelectionProviderReference();
                    ref.setSelectionProviderName(manySpName);
                    m2mConfiguration.setManySelectionProvider(ref);
                    break;
                }
            }
        }
    }
}
Also used : SelectionProviderReference(com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference) ModelSelectionProvider(com.manydesigns.portofino.model.database.ModelSelectionProvider) ForeignKey(com.manydesigns.portofino.model.database.ForeignKey)

Example 3 with ForeignKey

use of com.manydesigns.portofino.model.database.ForeignKey in project Portofino by ManyDesigns.

the class SessionFactoryBuilder method mapRelationships.

public void mapRelationships(Table table) throws NotFoundException, CannotCompileException {
    for (ForeignKey foreignKey : table.getForeignKeys()) {
        if (checkValidFk(foreignKey)) {
            mapManyToOne(foreignKey);
            mapOneToMany(foreignKey);
        }
    }
}
Also used : ForeignKey(com.manydesigns.portofino.model.database.ForeignKey)

Aggregations

ForeignKey (com.manydesigns.portofino.model.database.ForeignKey)3 ElementsThreadLocals (com.manydesigns.elements.ElementsThreadLocals)1 Insertable (com.manydesigns.elements.annotations.Insertable)1 Updatable (com.manydesigns.elements.annotations.Updatable)1 RequestMessages (com.manydesigns.elements.messages.RequestMessages)1 ClassAccessor (com.manydesigns.elements.reflection.ClassAccessor)1 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)1 QueryStringWithParameters (com.manydesigns.elements.text.QueryStringWithParameters)1 Database (com.manydesigns.portofino.model.database.Database)1 DatabaseLogic (com.manydesigns.portofino.model.database.DatabaseLogic)1 ModelSelectionProvider (com.manydesigns.portofino.model.database.ModelSelectionProvider)1 Table (com.manydesigns.portofino.model.database.Table)1 IdStrategy (com.manydesigns.portofino.persistence.IdStrategy)1 Persistence (com.manydesigns.portofino.persistence.Persistence)1 QueryUtils (com.manydesigns.portofino.persistence.QueryUtils)1 TableCriteria (com.manydesigns.portofino.persistence.TableCriteria)1 TableAccessor (com.manydesigns.portofino.reflection.TableAccessor)1 ActionInstance (com.manydesigns.portofino.resourceactions.ActionInstance)1 ResourceActionName (com.manydesigns.portofino.resourceactions.ResourceActionName)1 ConfigurationClass (com.manydesigns.portofino.resourceactions.annotations.ConfigurationClass)1