Search in sources :

Example 6 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class DefaultDbImportAction method saveLoaded.

/**
 * Save imported data.
 * This can create DataMap and/or Project files.
 */
protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException {
    ConfigurationTree<ConfigurationNode> projectRoot;
    if (config.getCayenneProject() == null) {
        // Old version of cdbimport, no Cayenne project, need to save only DataMap
        projectRoot = new ConfigurationTree<>(dataMap);
    } else {
        // Cayenne project is present
        DataChannelDescriptor dataChannelDescriptor;
        if (config.getCayenneProject().exists()) {
            // Cayenne project file exists, need to read it and push DataMap inside
            URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL());
            ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource);
            if (!configurationTree.getLoadFailures().isEmpty()) {
                throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription());
            }
            dataChannelDescriptor = configurationTree.getRootNode();
            // remove old copy of DataMap if it's there
            DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName());
            if (oldDataMap != null) {
                dataChannelDescriptor.getDataMaps().remove(oldDataMap);
            }
        } else {
            // No project file yet, can simply create empty project with resulting DataMap
            dataChannelDescriptor = new DataChannelDescriptor();
            dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName()));
            dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL()));
            logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath());
        }
        dataChannelDescriptor.getDataMaps().add(dataMap);
        projectRoot = new ConfigurationTree<>(dataChannelDescriptor);
    }
    Project project = new Project(projectRoot);
    projectSaver.save(project);
    logger.info("");
    logger.info("All changes saved.");
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) Project(org.apache.cayenne.project.Project) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Example 7 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class ObjAttributeTableModel method setObjAttributeType.

private void setObjAttributeType(ObjAttributeWrapper attribute, Object value) {
    String oldType = attribute.getType();
    String newType = value != null ? value.toString() : null;
    attribute.setType(newType);
    if (oldType == null || newType == null) {
        return;
    }
    String[] registeredTypes = ModelerUtil.getRegisteredTypeNames();
    Collection<String> registeredTypesList = Arrays.asList(registeredTypes);
    if (registeredTypesList.contains(oldType) == registeredTypesList.contains(newType)) {
        return;
    }
    ObjEntity entity = attribute.getEntity();
    ObjAttribute attributeNew;
    if (registeredTypesList.contains(newType) || !mediator.getEmbeddableNamesInCurrentDataDomain().contains(newType)) {
        attributeNew = new ObjAttribute();
        attributeNew.setDbAttributePath(attribute.getDbAttributePath());
    } else {
        attributeNew = new EmbeddedAttribute();
        attributeNew.setDbAttributePath(null);
    }
    attributeNew.setName(attribute.getName());
    attributeNew.setEntity(entity);
    attributeNew.setParent(attribute.getParent());
    attributeNew.setType(attribute.getType());
    attributeNew.setUsedForLocking(attribute.isUsedForLocking());
    entity.updateAttribute(attributeNew);
    mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.CHANGE));
    mediator.fireObjEntityDisplayEvent(new EntityDisplayEvent(this, mediator.getCurrentObjEntity(), mediator.getCurrentDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
    mediator.fireObjAttributeEvent(new AttributeEvent(this, attributeNew, entity, MapEvent.CHANGE));
    mediator.fireObjAttributeDisplayEvent(new AttributeDisplayEvent(this, attributeNew, mediator.getCurrentObjEntity(), mediator.getCurrentDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) AttributeDisplayEvent(org.apache.cayenne.modeler.event.AttributeDisplayEvent) EntityDisplayEvent(org.apache.cayenne.modeler.event.EntityDisplayEvent) ObjAttribute(org.apache.cayenne.map.ObjAttribute) EntityEvent(org.apache.cayenne.map.event.EntityEvent) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute) AttributeEvent(org.apache.cayenne.map.event.AttributeEvent)

Example 8 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class ObjEntityTab method initController.

private void initController() {
    // initialize events processing and tracking of UI updates...
    mediator.addObjEntityDisplayListener(this);
    dbEntityCombo.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // Change DbEntity for current ObjEntity
            ObjEntity entity = mediator.getCurrentObjEntity();
            DbEntity dbEntity = (DbEntity) dbEntityCombo.getSelectedItem();
            if (dbEntity != entity.getDbEntity()) {
                entity.setDbEntity(dbEntity);
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
    superEntityCombo.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // Change super-entity
            ObjEntity superEntity = (ObjEntity) superEntityCombo.getSelectedItem();
            String name = (superEntity == null || superEntity == NO_INHERITANCE) ? null : superEntity.getName();
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (!Util.nullSafeEquals(name, entity.getSuperEntityName())) {
                List<ObjAttribute> duplicateAttributes = null;
                if (name != null) {
                    duplicateAttributes = getDuplicatedAttributes(superEntity);
                }
                if (duplicateAttributes != null && duplicateAttributes.size() > 0) {
                    DuplicatedAttributesDialog.showDialog(Application.getFrame(), duplicateAttributes, superEntity, entity);
                    if (DuplicatedAttributesDialog.getResult().equals(DuplicatedAttributesDialog.CANCEL_RESULT)) {
                        superEntityCombo.setSelectedItem(entity.getSuperEntity());
                        superClassName.setText(entity.getSuperClassName());
                        return;
                    }
                }
                entity.setSuperEntityName(name);
                // drop not valid dbAttributePath
                if (name == null) {
                    for (ObjAttribute objAttribute : entity.getAttributes()) {
                        if (objAttribute.getDbAttribute() == null) {
                            objAttribute.setDbAttributePath(null);
                        }
                    }
                }
                if (name == null) {
                    dbEntityCombo.setEnabled(true);
                } else {
                    dbEntityCombo.setEnabled(false);
                    dbEntityCombo.getModel().setSelectedItem(null);
                }
                // if a super-entity selected, disable table selection
                // and also update parent DbEntity selection...
                toggleEnabled(name == null, !serverOnly.isSelected());
                dbEntityCombo.getModel().setSelectedItem(entity.getDbEntity());
                superClassName.setText(entity.getSuperClassName());
                // fire both EntityEvent and EntityDisplayEvent;
                // the later is to update attribute and relationship display
                DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();
                DataMap map = mediator.getCurrentDataMap();
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
                mediator.fireObjEntityDisplayEvent(new EntityDisplayEvent(this, entity, map, domain));
            }
        }
    });
    tableLabel.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // Jump to DbEntity of the current ObjEntity
            DbEntity entity = mediator.getCurrentObjEntity().getDbEntity();
            if (entity != null) {
                DataChannelDescriptor dom = (DataChannelDescriptor) mediator.getProject().getRootNode();
                mediator.fireDbEntityDisplayEvent(new EntityDisplayEvent(this, entity, entity.getDataMap(), dom));
            }
        }
    });
    readOnly.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (entity != null) {
                entity.setReadOnly(readOnly.isSelected());
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
    optimisticLocking.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (entity != null) {
                entity.setDeclaredLockType(optimisticLocking.isSelected() ? ObjEntity.LOCK_TYPE_OPTIMISTIC : ObjEntity.LOCK_TYPE_NONE);
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
    serverOnly.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (entity != null) {
                entity.setServerOnly(serverOnly.isSelected());
                toggleEnabled(dbEntityCombo.isEnabled(), !serverOnly.isSelected());
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
    isAbstract.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (entity != null) {
                entity.setAbstract(isAbstract.isSelected());
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ItemEvent(java.awt.event.ItemEvent) EntityDisplayEvent(org.apache.cayenne.modeler.event.EntityDisplayEvent) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ActionEvent(java.awt.event.ActionEvent) DataMap(org.apache.cayenne.map.DataMap) ObjEntity(org.apache.cayenne.map.ObjEntity) ActionListener(java.awt.event.ActionListener) DbEntity(org.apache.cayenne.map.DbEntity) EntityEvent(org.apache.cayenne.map.event.EntityEvent) List(java.util.List) LinkedList(java.util.LinkedList) ItemListener(java.awt.event.ItemListener)

Example 9 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class QueryType method fireQueryEvent.

/**
 * Fires events when a query was added
 */
public static void fireQueryEvent(Object src, ProjectController mediator, DataMap dataMap, QueryDescriptor query) {
    mediator.fireQueryEvent(new QueryEvent(src, query, MapEvent.ADD, dataMap));
    mediator.fireQueryDisplayEvent(new QueryDisplayEvent(src, query, dataMap, (DataChannelDescriptor) mediator.getProject().getRootNode()));
}
Also used : QueryDisplayEvent(org.apache.cayenne.modeler.event.QueryDisplayEvent) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) QueryEvent(org.apache.cayenne.configuration.event.QueryEvent)

Example 10 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class ProcedureParameterErrorMsg method displayField.

public void displayField(ProjectController mediator, JFrame frame) {
    Object object = super.validationFailure.getSource();
    DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();
    ProcedureParameter procedureParameter = (ProcedureParameter) object;
    Procedure procedure = procedureParameter.getProcedure();
    DataMap map = procedure.getDataMap();
    // Race condition between the two events...?
    // first display the stored procedure
    // for whatever reason, other validators do not require this step
    // (E.g. DbAttributeErrorMsg)
    ProcedureDisplayEvent procedureEvent = new ProcedureDisplayEvent(frame, procedure, map, domain);
    procedureEvent.setTabReset(true);
    mediator.fireProcedureDisplayEvent(procedureEvent);
    // now show the failed parameter
    ProcedureParameterDisplayEvent event = new ProcedureParameterDisplayEvent(frame, procedureParameter, procedure, map, domain);
    event.setTabReset(true);
    mediator.fireProcedureParameterDisplayEvent(event);
}
Also used : ProcedureParameter(org.apache.cayenne.map.ProcedureParameter) ProcedureDisplayEvent(org.apache.cayenne.modeler.event.ProcedureDisplayEvent) ProcedureParameterDisplayEvent(org.apache.cayenne.modeler.event.ProcedureParameterDisplayEvent) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) Procedure(org.apache.cayenne.map.Procedure) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)100 DataMap (org.apache.cayenne.map.DataMap)50 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)28 Test (org.junit.Test)20 ObjEntity (org.apache.cayenne.map.ObjEntity)18 DbEntity (org.apache.cayenne.map.DbEntity)12 ProjectController (org.apache.cayenne.modeler.ProjectController)12 URL (java.net.URL)11 Embeddable (org.apache.cayenne.map.Embeddable)11 ArrayList (java.util.ArrayList)10 URLResource (org.apache.cayenne.resource.URLResource)10 ObjAttribute (org.apache.cayenne.map.ObjAttribute)8 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)8 EntityDisplayEvent (org.apache.cayenne.modeler.event.EntityDisplayEvent)8 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 DataNodeEvent (org.apache.cayenne.configuration.event.DataNodeEvent)6 Injector (org.apache.cayenne.di.Injector)6 Procedure (org.apache.cayenne.map.Procedure)6 ProcedureDisplayEvent (org.apache.cayenne.modeler.event.ProcedureDisplayEvent)6 Resource (org.apache.cayenne.resource.Resource)6