org.eclipse.emf.ecore.util.ECrossReferenceAdapter Java Examples

The following examples show how to use org.eclipse.emf.ecore.util.ECrossReferenceAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetaService.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
@GET
@Path("/loadSbiModel/{bmId}")
public Response loadSbiModel(@PathParam("bmId") Integer bmId, @Context HttpServletRequest req) {

	try {
		IMetaModelsDAO businessModelsDAO = DAOFactory.getMetaModelsDAO();
		businessModelsDAO.setUserProfile((IEngUserProfile) req.getSession().getAttribute(IEngUserProfile.ENG_USER_PROFILE));

		EmfXmiSerializer serializer = new EmfXmiSerializer();
		Content lastFileModelContent = businessModelsDAO.lastFileModelMeta(bmId);
		InputStream is = new ByteArrayInputStream(lastFileModelContent.getFileModel());
		Model model = serializer.deserialize(is);

		// Create ResourceSet and add CrossReferenceAdapter
		ResourceSet resourceSet = new ResourceSetImpl();
		URI uri = URI.createURI(KNOWAGE_MODEL_URI + model.getName());
		Resource resource = resourceSet.createResource(uri);
		resource.getContents().add(model);
		ECrossReferenceAdapter crossReferenceAdapter = new ECrossReferenceAdapter();
		resource.getResourceSet().eAdapters().add(crossReferenceAdapter);
		req.getSession().setAttribute(EMF_MODEL_CROSS_REFERENCE, crossReferenceAdapter);

		req.getSession().setAttribute(EMF_MODEL, model);

		JSONObject translatedModel = createJson(model);

		return Response.ok(translatedModel.toString()).build();

	} catch (Throwable t) {
		throw new SpagoBIServiceException(req.getPathInfo(), t);
	}
}
 
Example #2
Source File: MetaService.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void addCrossReferenceAdapterToResource(Model model, ECrossReferenceAdapter crossReferenceAdapter) {
	ResourceSet resourceSet = new ResourceSetImpl();
	URI uri = URI.createURI(MetaService.KNOWAGE_MODEL_URI);
	Resource resource = resourceSet.createResource(uri);
	resource.getContents().add(model);

	resource.getResourceSet().eAdapters().add(crossReferenceAdapter);
}
 
Example #3
Source File: MetaService.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
@GET
@Path("/updatePhysicalModel")
public Response updatePhysicalModel(@Context HttpServletRequest req)
		throws ClassNotFoundException, NamingException, SQLException, JSONException, EMFUserError {
	PhysicalModelInitializer physicalModelInitializer = new PhysicalModelInitializer();
	Model model = (Model) req.getSession().getAttribute(EMF_MODEL);
	setProfileDialectThreadLocal(model);
	if (model.eAdapters().isEmpty())
		model.eAdapters().add(new ECrossReferenceAdapter());
	ECrossReferenceAdapter crossReferenceAdapter = (ECrossReferenceAdapter) model.eAdapters().get(0); // req.getSession().getAttribute(EMF_MODEL_CROSS_REFERENCE);
	physicalModelInitializer.setCrossReferenceAdapter(crossReferenceAdapter);

	String modelName = model.getName();
	IMetaModelsDAO businessModelsDAO = DAOFactory.getMetaModelsDAO();
	businessModelsDAO.setUserProfile((IEngUserProfile) req.getSession().getAttribute(IEngUserProfile.ENG_USER_PROFILE));
	IEngUserProfile profile = (IEngUserProfile) req.getSession().getAttribute(IEngUserProfile.ENG_USER_PROFILE);
	if (profile != null) {
		UserProfile userProfile = (UserProfile) profile;
		TenantManager.setTenant(new Tenant(userProfile.getOrganization()));
	}
	MetaModel metamodel = businessModelsDAO.loadMetaModelByName(modelName);
	String dataSourceLabel = metamodel.getDataSourceLabel();
	IDataSourceDAO dataSourceDAO = DAOFactory.getDataSourceDAO();
	IDataSource dataSource = dataSourceDAO.loadDataSourceByLabel(dataSourceLabel);
	// PhysicalModel phyMod = model.getPhysicalModels().get(0);
	// IDataSource dataSource = phyMod.getDataSource();

	List<String> missingTables = physicalModelInitializer.getMissingTablesNames(dataSource, model.getPhysicalModels().get(0), metamodel);
	List<String> missingColumns = physicalModelInitializer.getMissingColumnsNames(dataSource, model.getPhysicalModels().get(0), metamodel);
	List<String> removingItems = physicalModelInitializer.getRemovedTablesAndColumnsNames(dataSource, model.getPhysicalModels().get(0), metamodel);
	JSONObject resp = new JSONObject();
	resp.put("missingTables", new JSONArray(JsonConverter.objectToJson(missingTables, missingTables.getClass())));
	resp.put("missingColumns", new JSONArray(JsonConverter.objectToJson(missingColumns, missingColumns.getClass())));
	resp.put("removingItems", new JSONArray(JsonConverter.objectToJson(removingItems, removingItems.getClass())));
	return Response.ok(resp.toString()).build();
}
 
Example #4
Source File: PageResource.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @param model
 * @param crossReferenceAdapter
 */
private void addCrossReferenceAdapterToResource(Model model, ECrossReferenceAdapter crossReferenceAdapter) {
	ResourceSet resourceSet = new ResourceSetImpl();
	URI uri = URI.createURI(MetaService.KNOWAGE_MODEL_URI);
	Resource resource = resourceSet.createResource(uri);
	resource.getContents().add(model);

	resource.getResourceSet().eAdapters().add(crossReferenceAdapter);
}
 
Example #5
Source File: PhysicalModelInitializer.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Remove the physical foreign key from the Physical Model and also remove pending references (ex in BusinessRelationship)
 *
 */
public void removePhysicalForeignKey(PhysicalModel physicalModel, PhysicalForeignKey physicalForeignKey) {
	physicalModel.getForeignKeys().remove(physicalForeignKey);

	// remove inverse references (if any)
	// ModelSingleton modelSingleton = ModelSingleton.getInstance();
	ECrossReferenceAdapter adapter = getCrossReferenceAdapter();
	Collection<Setting> settings = adapter.getInverseReferences(physicalForeignKey, true);
	for (Setting setting : settings) {
		EObject eobject = setting.getEObject();
		if (eobject instanceof BusinessRelationship) {
			BusinessRelationship businessRelationship = (BusinessRelationship) eobject;
			if (businessRelationship.getPhysicalForeignKey().equals(physicalForeignKey)) {
				// remove reference
				businessRelationship.setPhysicalForeignKey(null);
			}
		}
	}
}
 
Example #6
Source File: M2DocUtils.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Create a new {@link ResourceSet} suitable for loading {@link EObject} from the given options.
 * 
 * @param exceptions
 *            the {@link List} of resulting exceptions (filled by this method)
 * @param key
 *            the {@link Object} key
 * @param defaultResourceSet
 *            the default {@link ResourceSet} to use if none is created
 * @param options
 *            the {@link Map} of existing options.
 * @return the {@link ResourceSet} suitable for loading {@link EObject} from the given options if any, the default {@link ResourceSet}
 *         otherwise
 * @see #cleanResourceSetForModels(Object)
 */
public static ResourceSet createResourceSetForModels(List<Exception> exceptions, Object key,
        ResourceSet defaultResourceSet, Map<String, String> options) {
    ResourceSet res = null;

    for (IServicesConfigurator configurator : getConfigurators()) {
        try {
            res = configurator.createResourceSetForModels(key, options);
            if (res != null) {
                break;
            }
            // CHECKSTYLE:OFF
        } catch (Exception e) {
            // CHECKSTYLE:ON
            exceptions.add(e);
        }
    }

    if (res == null) {
        res = defaultResourceSet;
    }

    if (Boolean.valueOf(options.get(INSTALL_CROSS_REFERENCE_ADAPTER_OPTION))) {
        final ECrossReferenceAdapter adapter = new ECrossReferenceAdapter();
        adapter.setTarget(res);
        res.eAdapters().add(adapter);
        CROSS_REFERENCE_ADAPTERS.put(key, adapter);
    }

    return res;
}
 
Example #7
Source File: M2DocUtils.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Cleans the {@link #createResourceSetForModels(List, Object, ResourceSet, Map) created} {@link ResourceSet} for the
 * given {@link Object} key.
 * 
 * @param key
 *            the {@link Object} key
 * @param resourceSet
 *            the {@link ResourceSet} created using {@link #createResourceSetForModels(List, Object, ResourceSet, Map)}
 */
public static void cleanResourceSetForModels(Object key, ResourceSet resourceSet) {
    final ECrossReferenceAdapter adapter = CROSS_REFERENCE_ADAPTERS.remove(key);
    if (adapter != null) {
        adapter.unsetTarget(resourceSet);
        resourceSet.eAdapters().remove(adapter);
    }
    for (IServicesConfigurator configurator : getConfigurators()) {
        configurator.cleanResourceSetForModels(key);
    }
}
 
Example #8
Source File: ECrossReferenceAdapterCrossReferenceProvider.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the {@link ECrossReferenceAdapter} either the {@link #adapter} or the one of the given {@link EObject}.
 * 
 * @param self
 *            the {@link EObject}
 * @return the {@link ECrossReferenceAdapter} either the {@link #adapter} or the one of the given {@link EObject} if any, <code>null</code>
 *         otherwise
 */
private ECrossReferenceAdapter getAdapter(EObject self) {
    final ECrossReferenceAdapter res;

    if (adapter != null) {
        res = adapter;
    } else {
        res = ECrossReferenceAdapter.getCrossReferenceAdapter(self);
    }

    return res;
}
 
Example #9
Source File: ECrossReferenceAdapterCrossReferenceProvider.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Collection<Setting> getInverseReferences(EObject self) {
    final Collection<Setting> res;

    final ECrossReferenceAdapter crossReferenceAdapter = getAdapter(self);
    if (crossReferenceAdapter != null) {
        res = crossReferenceAdapter.getInverseReferences(self);
    } else {
        throw new IllegalStateException("No ECrossReferenceAdapter found for :" + self);
    }

    return res;
}
 
Example #10
Source File: MetaService.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@POST
@Path("/updatePhysicalModel")
@SuppressWarnings("unchecked")
public Response applyUpdatePhysicalModel(@Context HttpServletRequest req)
		throws ClassNotFoundException, NamingException, SQLException, JSONException, IOException, EMFUserError {
	Model model = (Model) req.getSession().getAttribute(EMF_MODEL);
	setProfileDialectThreadLocal(model);
	JSONObject oldJsonModel = createJson(model);

	String modelName = model.getName();
	IMetaModelsDAO businessModelsDAO = DAOFactory.getMetaModelsDAO();
	businessModelsDAO.setUserProfile((IEngUserProfile) req.getSession().getAttribute(IEngUserProfile.ENG_USER_PROFILE));
	IEngUserProfile profile = (IEngUserProfile) req.getSession().getAttribute(IEngUserProfile.ENG_USER_PROFILE);
	if (profile != null) {
		UserProfile userProfile = (UserProfile) profile;
		TenantManager.setTenant(new Tenant(userProfile.getOrganization()));
	}
	MetaModel metamodel = businessModelsDAO.loadMetaModelByName(modelName);
	String dataSourceLabel = metamodel.getDataSourceLabel();
	IDataSourceDAO dataSourceDAO = DAOFactory.getDataSourceDAO();
	IDataSource dataSource = dataSourceDAO.loadDataSourceByLabel(dataSourceLabel);

	JSONObject json = RestUtilities.readBodyAsJSONObject(req);
	List<String> tables = (List<String>) JsonConverter.jsonToObject(json.getString("tables"), List.class);
	PhysicalModelInitializer physicalModelInitializer = new PhysicalModelInitializer();
	if (model.eAdapters().isEmpty())
		model.eAdapters().add(new ECrossReferenceAdapter());
	ECrossReferenceAdapter crossReferenceAdapter = (ECrossReferenceAdapter) model.eAdapters().get(0); // req.getSession().getAttribute(EMF_MODEL_CROSS_REFERENCE);
	physicalModelInitializer.setCrossReferenceAdapter(crossReferenceAdapter);
	physicalModelInitializer.setRootModel(model);
	PhysicalModel originalPM = model.getPhysicalModels().get(0);
	List<String> currTables = new ArrayList<String>();
	for (PhysicalTable pt : originalPM.getTables()) {
		currTables.add(pt.getName());
	}
	currTables.addAll(tables);

	PhysicalModel phyMod = physicalModelInitializer.initializeLigth(originalPM, currTables, dataSource);
	physicalModelInitializer.updateModel(originalPM, phyMod, tables);

	JSONObject jsonModel = createJson(model);
	ObjectMapper mapper = new ObjectMapper();
	JsonNode patch = JsonDiff.asJson(mapper.readTree(oldJsonModel.toString()), mapper.readTree(jsonModel.toString()));

	return Response.ok(patch.toString()).build();
}
 
Example #11
Source File: ModelSingleton.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @return the crossReferenceAdapter
 */
public ECrossReferenceAdapter getCrossReferenceAdapter() {
	return crossReferenceAdapter;
}
 
Example #12
Source File: PhysicalModelInitializer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @return the crossReferenceAdapter
 */
public ECrossReferenceAdapter getCrossReferenceAdapter() {
	return crossReferenceAdapter;
}
 
Example #13
Source File: PhysicalModelInitializer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @param crossReferenceAdapter the crossReferenceAdapter to set
 */
public void setCrossReferenceAdapter(ECrossReferenceAdapter crossReferenceAdapter) {
	this.crossReferenceAdapter = crossReferenceAdapter;
}
 
Example #14
Source File: M2DocUtils.java    From M2Doc with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Gets the initialized {@link IQueryEnvironment} for the given {@link ResourceSet} and options.
 * 
 * @param resourceSetForModels
 *            the {@link ResourceSet} for model elements
 * @param templateURI
 *            the template {@link URI}
 * @param options
 *            the {@link Map} of options
 * @return the initialized {@link IQueryEnvironment} for the given {@link ResourceSet} and options
 */
public static IQueryEnvironment getQueryEnvironment(ResourceSet resourceSetForModels, URI templateURI,
        Map<String, String> options) {
    final ECrossReferenceAdapterCrossReferenceProvider crossReferenceProvider = new ECrossReferenceAdapterCrossReferenceProvider(
            ECrossReferenceAdapter.getCrossReferenceAdapter(resourceSetForModels));
    final ResourceSetRootEObjectProvider rootProvider = new ResourceSetRootEObjectProvider(resourceSetForModels);

    return getQueryEnvironment(resourceSetForModels, crossReferenceProvider, rootProvider, templateURI, options);
}
 
Example #15
Source File: ModelSingleton.java    From Knowage-Server with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * @param crossReferenceAdapter
 *            the crossReferenceAdapter to set
 */
public void setCrossReferenceAdapter(ECrossReferenceAdapter crossReferenceAdapter) {
	this.crossReferenceAdapter = crossReferenceAdapter;
}
 
Example #16
Source File: ECrossReferenceAdapterCrossReferenceProvider.java    From M2Doc with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Constructor.
 * 
 * @param adapter
 *            the {@link ECrossReferenceAdapter}
 */
public ECrossReferenceAdapterCrossReferenceProvider(ECrossReferenceAdapter adapter) {
    this.adapter = adapter;
}