VML勾画流程图(四)db4o存放流程实例数据

VML勾画流程图(四)db4o存放流程实例数据

db4o的官方网站
http://www.db4o.com/

官方API
http://developer.db4o.com/Documentation/Reference/db4o-7.4/java/tutorial/

晕,官方文档也是有点过时的,下载了7.12版本后,里面的doc文件下面才有最新的文档。
第一步
ivy.xml中导入db4o的相关包(其实也是我自己下载了放到nexus里面的):
<!--  db4o -->
<dependency org="com/db4o" name="db4o" rev="7.12" />
第二步
撰写了一个简单的类来处理db4o,Db4oDAOImpl.java如下:
package com.sillycat.easyworkflow.dao.impl;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectServer;
import com.db4o.cs.Db4oClientServer;
import com.sillycat.easyworkflow.dao.Db4oDAO;
public class Db4oDAOImpl implements Db4oDAO{
private ObjectContainer db;
private ObjectServer server;
// 是否是本地服务
private boolean enableLocal = true;
// 本地数据库文件名字
private String localFileName = "local.yap";
// 服务端数据库文件名字
private String serverFileName = "server.yap";
// 服务器端口
private int port = 1212;
// 用户名
private String username = "sillycat";
// 密码
private String password = "111111";
// 服务器地址
private String serverhost = "localhost";
public void init() {
if (this.isEnableLocal()) {
db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), this
.getLocalFileName());
} else {
server = Db4oClientServer.openServer(Db4oClientServer
.newServerConfiguration(), this.getServerFileName(), this
.getPort());
server.grantAccess(this.getUsername(), this.getPassword());
db = Db4oClientServer.openClient(Db4oClientServer
.newClientConfiguration(), this.getServerhost(), this
.getPort(), this.getUsername(), this.getPassword());
}
}
public void destroy() {
if (db != null) {
db.close();
}
if (server != null) {
server.close();
}
}
public ObjectContainer getDB() {
if (db == null) {
init();
}
return db;
}
...snip the getter and setter...
}

第三步
spring的配置文件上配置如下,resource-context.xml:
<bean id="db4oDAO" class="com.sillycat.easyworkflow.dao.impl.Db4oDAOImpl" init-method="init">
<property name="enableLocal" value="${db4o.enableLocal}" />
<property name="localFileName" value="${db4o.local.filename}" />
<property name="serverFileName" value="${db4o.server.filename}" />
<property name="port" value="${db4o.server.port}" />
<property name="username" value="${db4o.server.username}" />
<property name="password" value="${db4o.server.password}" />
<property name="serverhost" value="${db4o.server.serverhost}" />
</bean>
properties配置文件如下,easyworkflow.properties:
################################################
# db4o
################################################
db4o.enableLocal=true
db4o.local.filename=d:/work/easyworkflow/db/local.yap
db4o.server.filename=d:/work/easyworkflow/db/server.yap
db4o.server.port=1212
db4o.server.username=sillycat
db4o.server.password=111111
db4o.server.serverhost=localhost

第四步
一切具备了,那么只剩下单元测试了,Db4oDAOTest.java如下:
package com.sillycat.easyworkflow.dao;

import javax.annotation.Resource;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.db4o.ObjectContainer;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class Db4oDAOTest {
private Db4oDAO db4oDAO;
@Resource
public void setDb4oDAO(Db4oDAO db4oDAO) {
this.db4oDAO = db4oDAO;
}
@Test
public void getDB() {
ObjectContainer db = db4oDAO.getDB();
Assert.assertNotNull(db);
}
}
第五步
这里测试也就是得到ObjectContainer,如果要具体保存和得到对象的话,简单的示例WorkflowManagerImpl.java如下:
package com.sillycat.easyworkflow.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.db4o.ObjectSet;
import com.sillycat.easyworkflow.dao.Db4oDAO;
import com.sillycat.easyworkflow.model.Workflow;
import com.sillycat.easyworkflow.service.WorkflowManager;
@Service("workflowManager")
public class WorkflowManagerImpl implements WorkflowManager {
private Db4oDAO db4oDAO;
@Autowired
public void setDb4oDAO(Db4oDAO db4oDAO) {
this.db4oDAO = db4oDAO;
}
public Workflow getWorkflowById(String workflowInstanceId) {
Workflow workflow = new Workflow(workflowInstanceId);
ObjectSet<Workflow> os = db4oDAO.getDB().queryByExample(workflow);
if (os != null && os.hasNext()) {
workflow = os.next();
}
return workflow;
}
public void saveWorkflow(Workflow workflow) {
db4oDAO.getDB().store(workflow);
}

public List<WorkflowDefinition> allWorkflowDefinition() {
List<WorkflowDefinition> list = null;
WorkflowDefinition wfDefinition = new WorkflowDefinition();
ObjectSet<WorkflowDefinition> os = db4oDAO.getDB().queryByExample(
wfDefinition);
Db4oUtil<WorkflowDefinition> db4oUtil = new Db4oUtil<WorkflowDefinition>();
list = db4oUtil.retrieveList(os);
return list;
}

public void deleteWorkflowDefinitionByWorkflowName(String workflowName) {
if (StringUtil.isBlank(workflowName)) {
logger
.info("workflowName can't be blank when delete workflowdefinition!");
return;
}
WorkflowDefinition workflowDefinition = getWorkflowDefinitionByWorkflowName(workflowName);
if (workflowDefinition == null) {
logger.info("I can't find workflowDefinition=" + workflowName
+ " in database");
return;
}
db4oDAO.getDB().delete(workflowDefinition);
}
}

差不多我的使用情况就是这样,如果以后要用到高级应用,那么还要再翻翻文档才行。

另外看到还可以在eclipse上安装一个Object Manager Enterprise installation(OME)
1.To install the plugin, you need to have a version of Eclipse >= 3.3 installed.
2.Unzip the file to a folder of your choice.
3.我本机是安装的eclipse 3.5,所以,我就按照如下过程
Help---->Install New Software---->Add----->
Name d4b
Locationn:  -----> Archive...
4.安装成功后,Window---->Open Perspective ---->OME
使用OME来查看本地的库中的数据还是比较方便写程序的:)

你可能感兴趣的:(DAO,eclipse,workflow,OS,JUnit)