I am struggling to make it work. This is just a plain code peace from (supposed) documentation and although it works with no error, nothing is changed in the database! I can't see a new class named V1! I am using 2.0.7 and Java 1.7
OrientGraphFactory factory = new OrientGraphFactory("plocal:D:/orientdb/databases/GratefulDeadConcerts");
OrientGraphNoTx graph = factory.getNoTx();
try{
OClass V = graph.getRawGraph().getMetadata().getSchema().getClass("V");
OClass V1 = graph.getRawGraph().getMetadata().getSchema().createClass("V1", V);
V1.createProperty("prop1", OType.STRING);
V1.createProperty("prop2", OType.STRING);
ODocument od = new ODocument(V1);
od.field("prop1", "value1");
od.field("prop2", "value2");
od.save();
} catch( Exception e ) {
System.out.println("ERROR:"+e.getMessage());
graph.rollback();
}
finally {
graph.shutdown();
factory.close();
}
Related
I try to print a query through the java console but nothing comes out. this is my code someone could help me.
I'm new to OrientDB and I'm just learning.
The query I need is to know the shortest path between two nodes and print this query on the Java console. It does not give me any errors but nothing comes out.
public class Graph {
private static final String DB_PATH = "C:/OrientDataBase/shortest_path";
static OrientGraphNoTx DBGraph;
static OrientGraphFactory factory;
public static void main(String[] args) {
factory = new OrientGraphFactory("plocal:"+DB_PATH);
DBGraph = factory.getNoTx();
HashMap<String, Vertex> nodes = new HashMap<String, Vertex>();
for(int i = 0; i <= 1000; i++)
{
Vertex v = DBGraph.addVertex("class:V");
v.setProperty("vertexID", i+"");
nodes.put(i+"", v);
}
try(BufferedReader br = new BufferedReader(new FileReader("C:/OrientDataBase/sp1.csv"))) {
int i=0;
for(String line; (line = br.readLine()) !=null ; ) {
if(i==0){
i++;
}
else{
String[] vertices = line.split(",");
String vertex1 = vertices[0];
String vertex2 = vertices[1];
String weight= vertices[2];
vertex2 = vertex2.replaceAll(" ", "");
Vertex v1 = nodes.get(vertex1);
Vertex v2 = nodes.get(vertex2);
Edge eLives = DBGraph.addEdge(null, v1, v2, "belongs");
eLives.setProperty("weight", weight);
System.out.println(v1+","+v2+","+weight);
String query = "select expand(shortestPath) from (select shortestPath(#10:0,#10:2,BOTH))";
Iterable<OrientVertex> res = DBGraph.command(new OCommandSQL(query)).execute();
while(res.iterator().hasNext()){
OrientVertex v = res.iterator().next();
System.out.println("rid: "+v.getId().toString()+"\tn:"+v.getProperty("n"));
}
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
I tried your code and you have to put the ticks when you do the query so, it becomes:
String query = "select expand(shortestPath) from (select shortestPath(#10:0,#10:2,'BOTH'))";
I used this csv file.
Hope it helps.
Regards
I searched some examples on the Internet and many of them use the OGraphDatabase. However, when these code file was compiled, a exception that says cant find the symbol of "OGraphDatabase" was throwed.
Blow is the source code:
public class TestTreeGraph {
static OGraphDatabase db;
//static int i=0;
//static ODocument currentNode;
public static void main(String[] args) throws FileNotFoundException{
String dbpath="/Users/wuguirongsg/orientdb/orientdbgraph";
File dbfile = new File(dbpath);
if(!dbfile.exists()){
//dbfile.mkdirs();
db = new OGraphDatabase("local:"+dbpath).create();
db = new OGraphDatabase("local:"+dbpath).open("admin", "admin");
}else{
db = new OGraphDatabase("local:"+dbpath).open("admin", "admin");
}
ODocument rootNode = db.createVertex().field("id", 0);
int i=1;
createNode(rootNode,i);
db.setRoot("treegraph", rootNode);
}
private static void createNode(ODocument node,int i){
if(i>=20){
System.out.println("i>=10================== back ");
return ;
}
ODocument leftNode = db.createVertex().field("id", i + "_vertex_left");
System.out.println("create "+i + "_vertex_left ");
ODocument rightNode = db.createVertex().field("id", i + "_vertex_right");
System.out.println("create "+i + "_vertex_right ");
ODocument edgeleft = db.createEdge( node, leftNode);
ODocument edgeright = db.createEdge( node, rightNode);
edgeleft.save();
edgeright.save();
//currentNode = leftNode;
System.out.println("go left");
createNode(leftNode,i+1);
System.out.println("go right");
createNode(rightNode,i+1);
System.out.println("==================");
}
}
OGraphDatabase has been deprecated ages ago. You can use OrientGraph.
Example
String dbpath="C:/test";
OrientGraphFactory factory = new OrientGraphFactory("plocal:"+dbpath);
// if the database doesn't exist it is created and opened
// if the database exists, it is opened
OrientGraph db = factory.getTx();
// inserting a vertex
Vertex rootNode=db.addVertex("class:V");
rootNode.setProperty("myId","0");
db.shutdown();
I am using a stored procedure in my SQL Server database to take input of the data through the datatable. As I am using ASP.NET MVC now, I want to use Entity Framework instead of ado.net
public void BulkUpload(DataTable dt)
{
dt.TableName = "MainTable";
DataSet dataset = new DataSet();
DataTable dataTable = new DataTable();
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
conn.Open();
{
SqlCommand cmd = new SqlCommand("DatatableToDataBase", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#mode", SqlDbType.VarChar).Value = "MainTB";
cmd.Parameters.AddWithValue("#Details", SqlDbType.VarChar).Value = dt;
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
catch (Exception)
{ }
}
It's very easy just add an entity datamodel to your program
connect to you model :
"entitymodel" context = this.CurrentDataSource;
And pass the stored procedure where you like .
Thats everything
Example:
[WebGet]
public List<callersW> GetCaller()
{
testCDREntities1 context = this.CurrentDataSource;
//sql parameters here
List<callersW> result = context.Database.SqlQuery<callersW>("StoredProcedure").ToList();
return result;
}
(PostgreSQL 9.1, Telerik OpenAccess v2.0.50727, PgAdmin III).
I'm having difficulty calling a stored procedure from the (Telerik) Entity Framework. The exact error is:
NpgsqlException was unhandled by user code.
ERROR: 42703: column "cpatient" does not exist.
The Telerik templated call is:
public int SaveDx(string cpatient, Object o, Object n)
{
OAParameter parameterCpatient = new OAParameter();
parameterCpatient.ParameterName = "cpatient";
parameterCpatient.Size = -1;
if(cpatient != null)
{
parameterCpatient.Value = cpatient;
}
else
{
parameterCpatient.DbType = DbType.String;
parameterCpatient.Value = DBNull.Value;
}
OAParameter parameterO = new OAParameter();
parameterO.ParameterName = "o";
parameterO.Value = o;
OAParameter parameterN = new OAParameter();
parameterN.ParameterName = "n";
parameterN.Value = n;
int queryResult = this.ExecuteNonQuery("SELECT * FROM \"public\".\"g_savedx\"(cpatient, o, n)", CommandType.Text, parameterCpatient, parameterO, parameterN);
return queryResult;
}
Where the ExecuteNonQuery statement generates the error. The PostgreSQL stored procedure is:
FUNCTION g_savedx(cpatient character varying, o view_dx, n view_dx)
RETURNS void AS ...
The postgreSQL function has been tested to work correctly from pgAdmin.
So where is the column "cpatient" coming from?? What am I doing wrong?
TIA
I never could get the Telerik EntitiesModel ExecuteNonQuery to work under any conditions. Hence the suggested code of:
using (var cxt = new Nova.Data.Data())
{
cxt.SaveDx();
cxt.SaveChanges();
}
where cxt.SaveDx() is the domain model name for the postgresql g_savedx stored procedure, fails.
My eventual workaround for PostgreSQL is to use Npgsql directly as:
public void SaveDx(View_dx dx, bool alldx = false)
{
using (var cxt = new Nova.Data.Data())
{
string connstring = cxt.Connection.ConnectionString;
using (NpgsqlConnection conn = new NpgsqlConnection(connstring))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "g_savedx";
cmd.CommandType = CommandType.StoredProcedure;
NpgsqlCommandBuilder.DeriveParameters(cmd);
cmd.Parameters["groupid"].Value = ....
var rowsAffected = cmd.ExecuteNonQuery();
}
}
}
}
When doing it this way, only use the types defined in the NpgsqlDbType enumeration in the PostgreSQL procedure interface. (PostgreSQL can use composite types, Npgsql not so much).
It would sure be nice for Telerik ExecuteNonQuery to work.
I'm a new developper in CRM 2011. I've made that simple code for tests.
It appears that the plugin works fine (checked with the ITracingService) but it seems that the attributes doesn't take the new values.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
namespace AgeUpdatePlugin
{
public class AgeUpdatePlugin:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
try
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (tracingService == null)
throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
tracingService.Trace("Plugin has started..");
//l'expression de condition
ConditionExpression condition = new ConditionExpression("new_datenaissance", ConditionOperator.NotNull);
FilterExpression filter = new FilterExpression();
filter.Conditions.Add(condition);
ColumnSet cols = new ColumnSet();
//cols.AddColumn("Id");
cols.AddColumn("new_candidatid");
cols.AddColumn("new_age");
cols.AddColumn("new_datenaissance");
QueryExpression query = new QueryExpression();
query.Criteria = filter;
query.EntityName = "new_candidat";
query.ColumnSet = cols;
tracingService.Trace("appel retrieve multiple with date=" + DateTime.Now.ToString() + "/"+DateTime.Now.ToLocalTime().ToString());
var retrieve = service.RetrieveMultiple(query).Entities;
foreach (var c in retrieve)
{
var dt =(DateTime)c.Attributes["new_datenaissance"];
tracingService.Trace(dt.ToString());
if (dt.Month == DateTime.Now.Month && dt.Day == DateTime.Now.Day)
{
tracingService.Trace((DateTime.Now.Year - DateTime.Parse(c["new_datenaissance"].ToString()).Year).ToString());
c.Attributes["new_age"] = DateTime.Now.Year - DateTime.Parse(c["new_datenaissance"].ToString()).Year;
tracingService.Trace(c.Attributes["new_candidatid"].ToString() +" - "+c.Attributes["new_age"].ToString());
service.Update(c);
tracingService.Trace("updated");
}
}
tracingService.Trace("Plugin done working");
throw new InvalidPluginExecutionException("This is from a plugin that Mehdi has created ");
}
catch (Exception exc)
{
throw exc;
}
}
}
}
Whenever you throw an exception within a plugin the transaction is aborted so you won't see your changes applied. Remove the following line:
throw new InvalidPluginExecutionException("This is from a plugin that Mehdi has created ");
I assume you added this to see the trace output which is only available when an exception is thrown. With CRM Online your best bet for tracing is to write to a custom trace entity.