diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..3d3e2c4 --- /dev/null +++ b/build.gradle @@ -0,0 +1,124 @@ + +apply plugin: 'java' + +[compileJava,compileTestJava]*.options*.encoding = 'UTF-8' + +ext { + /** + * 项目中依赖的jar的路径 + * 1.如果依赖的jar需要打包到zip中,放置在lib根目录下 + * 2.如果依赖的jar仅仅是编译时需要,防止在lib下子目录下即可 + */ + libPath = "$projectDir/../webroot/WEB-INF/lib" + + /** + * 是否对插件的class进行加密保护,防止反编译 + */ + guard = false + + def pluginInfo = getPluginInfo() + pluginPre = "fine-plugin" + pluginName = pluginInfo.id + pluginVersion = pluginInfo.version + + outputPath = "$projectDir/../webroot/WEB-INF/plugins/plugin-" + pluginName + "-1.0/classes" +} + +group = 'com.fr.plugin' +version = '10.0' +sourceCompatibility = '8' + +sourceSets { + main { + java.outputDir = file(outputPath) + output.resourcesDir = file(outputPath) + } +} + +ant.importBuild("encrypt.xml") +//定义ant变量 +ant.projectDir = projectDir +ant.references["compile.classpath"] = ant.path { + fileset(dir: libPath, includes: '**/*.jar') + fileset(dir: ".",includes:"**/*.jar" ) +} + +classes.dependsOn('clean') + +task copyFiles(type: Copy,dependsOn: 'classes'){ + from outputPath + into "$projectDir/classes" +} + +task preJar(type:Copy,dependsOn: guard ? 'compile_encrypt_javas' : 'compile_plain_javas'){ + from "$projectDir/classes" + into "$projectDir/transform-classes" + include "**/*.*" +} +jar.dependsOn("preJar") + +task makeJar(type: Jar,dependsOn: preJar){ + from fileTree(dir: "$projectDir/transform-classes") + baseName pluginPre + appendix pluginName + version pluginVersion + destinationDir = file("$buildDir/libs") + + doLast(){ + delete file("$projectDir/classes") + delete file("$projectDir/transform-classes") + } +} + +task copyFile(type: Copy,dependsOn: ["makeJar"]){ + from "$buildDir/libs" + from("$projectDir/lib") { + include "*.jar" + } + from "$projectDir/plugin.xml" + into file("$buildDir/temp/plugin") +} + +task zip(type:Zip,dependsOn:["copyFile"]){ + from "$buildDir/temp/plugin" + destinationDir file("$buildDir/install") + baseName pluginPre + appendix pluginName + version pluginVersion +} + +//控制build时包含哪些文件,排除哪些文件 +processResources { +// exclude everything +// 用*.css没效果 +// exclude '**/*.css' +// except this file +// include 'xx.xml' +} + +/*读取plugin.xml中的version*/ +def getPluginInfo(){ + def xmlFile = file("plugin.xml") + if (!xmlFile.exists()) { + return ["id":"none", "version":"1.0.0"] + } + def plugin = new XmlParser().parse(xmlFile) + def version = plugin.version[0].text() + def id = plugin.id[0].text() + return ["id":id,"version":version] +} + +repositories { + mavenLocal() + maven { + url = uri('http://mvn.finedevelop.com/repository/maven-public/') + } +} + +dependencies { + //使用本地jar + implementation fileTree(dir: 'lib', include: ['**/*.jar']) + implementation fileTree(dir: libPath, include: ['**/*.jar']) +} + + diff --git a/encrypt.xml b/encrypt.xml new file mode 100644 index 0000000..1401cd1 --- /dev/null +++ b/encrypt.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/common-kit-10.0-20210309.jar b/lib/common-kit-10.0-20210309.jar new file mode 100644 index 0000000..7027142 Binary files /dev/null and b/lib/common-kit-10.0-20210309.jar differ diff --git a/lib/finekit-10.0-20210309.jar b/lib/finekit-10.0-20210309.jar new file mode 100644 index 0000000..2d3b5b4 Binary files /dev/null and b/lib/finekit-10.0-20210309.jar differ diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..70e8383 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,19 @@ + + com.tptj.demo.hg.db.access.provider.v10 + + yes + 1.0 + 10.0 + tptj + 2019-07-18 + + + com.tptj.demo.hg.db.access.provider + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/BeanConverter.java b/src/main/java/com/tptj/demo/hg/db/access/provider/BeanConverter.java new file mode 100644 index 0000000..ea0b165 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/BeanConverter.java @@ -0,0 +1,31 @@ +package com.tptj.demo.hg.db.access.provider; + +import com.fanruan.api.log.LogKit; +import com.fr.json.JSONObject; +import com.fr.json.revise.EmbedJson; +import com.fr.stable.db.entity.converter.BaseConverter; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +public class BeanConverter extends BaseConverter { + @Override + public String convertToDatabaseColumn(DemoBean bean) { + if( null == bean ){ + return "{}"; + } + return JSONObject.mapFrom(bean).toString(); + } + + @Override + public DemoBean convertToEntityAttribute(String val ) { + try{ + return EmbedJson.MAPPER.readValue(val,DemoBean.class); + }catch(Exception e){ + LogKit.error(e,"{} can not convert to DemoBean for {}",val,e.getMessage()); + } + return new DemoBean(); + } +} diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/ControllerBridge.java b/src/main/java/com/tptj/demo/hg/db/access/provider/ControllerBridge.java new file mode 100644 index 0000000..64135ba --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/ControllerBridge.java @@ -0,0 +1,15 @@ +package com.tptj.demo.hg.db.access.provider; + +import com.fr.decision.fun.impl.AbstractControllerRegisterProvider; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +public class ControllerBridge extends AbstractControllerRegisterProvider { + @Override + public Class[] getControllers() { + return new Class[]{DemoController.class}; + } +} diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/DataBean.java b/src/main/java/com/tptj/demo/hg/db/access/provider/DataBean.java new file mode 100644 index 0000000..c4f7da7 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/DataBean.java @@ -0,0 +1,47 @@ +package com.tptj.demo.hg.db.access.provider; + +import java.util.List; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +public class DataBean { + private String id; + private String key; + private DemoBean bean; + private List attr3; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public DemoBean getBean() { + return bean; + } + + public void setBean(DemoBean bean) { + this.bean = bean; + } + + public List getAttr3() { + return attr3; + } + + public void setAttr3(List attr3) { + this.attr3 = attr3; + } +} diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/DemoBean.java b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoBean.java new file mode 100644 index 0000000..b373aac --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoBean.java @@ -0,0 +1,28 @@ +package com.tptj.demo.hg.db.access.provider; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +public class DemoBean { + + private String attr1 = null; + private String attr2 = null; + + public String getAttr1() { + return attr1; + } + + public void setAttr1(String attr1) { + this.attr1 = attr1; + } + + public String getAttr2() { + return attr2; + } + + public void setAttr2(String attr2) { + this.attr2 = attr2; + } +} diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/DemoController.java b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoController.java new file mode 100644 index 0000000..a5f06c0 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoController.java @@ -0,0 +1,44 @@ +package com.tptj.demo.hg.db.access.provider; + +import com.fr.decision.webservice.Response; +import com.fr.decision.webservice.annotation.LoginStatusChecker; +import com.fr.third.springframework.stereotype.Controller; +import com.fr.third.springframework.web.bind.annotation.*; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +@Controller("DBAccessDemoController") +@LoginStatusChecker(required = false) +@RequestMapping(value = "/db/demo") +public class DemoController { + + @RequestMapping( value = "/dm_entity", method = RequestMethod.POST ) + @ResponseBody + public Response add( @RequestBody DataBean bean )throws Exception{ + DemoDbController.add(bean); + return Response.ok( bean ); + } + + @RequestMapping( value = "/dm_entity/{entity}", method = RequestMethod.DELETE ) + @ResponseBody + public Response delete( @PathVariable("entity") String entity )throws Exception{ + return Response.ok( DemoDbController.delete(entity) ); + } + + @RequestMapping( value = "/dm_entity", method = RequestMethod.PUT ) + @ResponseBody + public Response update( @RequestBody DataBean bean )throws Exception{ + DemoDbController.update(bean); + return Response.ok( bean ); + } + + @RequestMapping( value = "/dm_entity", method = RequestMethod.GET ) + @ResponseBody + public Response like( @RequestParam(value = "key") String key )throws Exception{ + return Response.ok( DemoDbController.like(key) ); + } +} + diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/DemoDao.java b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoDao.java new file mode 100644 index 0000000..692700d --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoDao.java @@ -0,0 +1,63 @@ +package com.tptj.demo.hg.db.access.provider; + +import com.fr.stable.db.dao.BaseDAO; +import com.fr.stable.db.dao.DAOProvider; +import com.fr.stable.db.session.DAOSession; +import com.fr.stable.query.QueryFactory; +import com.fr.stable.query.condition.QueryCondition; +import com.fr.stable.query.restriction.RestrictionFactory; + +import java.util.List; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +public class DemoDao extends BaseDAO { + + public DemoDao(DAOSession session) { + super(session); + } + + @Override + protected Class getEntityClass() { + return DemoEntity.class; + } + + public final static DAOProvider DAO = new DAOProvider() { + @Override + public Class getEntityClass() { + return DemoEntity.class; + } + + @Override + public Class getDAOClass() { + return DemoDao.class; + } + }; + + public List likeByKey( String key )throws Exception{ + QueryCondition condition = QueryFactory.create() + .addRestriction(RestrictionFactory.like(DemoEntity.COLUMN_KEY, key)); + return find(condition); + } + + public void add(DemoEntity entity) throws Exception { + getSession().persist(entity); + } + + public void update(DemoEntity entity) throws Exception { + getSession().merge(entity); + } + + public DemoEntity getById(String id) throws Exception { + return getSession().getById(id, DemoEntity.class); + } + + public void remove(String id) throws Exception { + getSession().remove(QueryFactory.create() + .addRestriction(RestrictionFactory.eq("id", id)), + this.getEntityClass()); + } +} diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/DemoDbController.java b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoDbController.java new file mode 100644 index 0000000..8dd73e1 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoDbController.java @@ -0,0 +1,182 @@ +package com.tptj.demo.hg.db.access.provider; + +import com.fanruan.api.log.LogKit; +import com.fr.db.fun.impl.AbstractDBAccessProvider; +import com.fr.decision.base.util.UUIDUtil; +import com.fr.intelli.record.Focus; +import com.fr.record.analyzer.EnableMetrics; +import com.fr.stable.StringUtils; +import com.fr.stable.db.accessor.DBAccessor; +import com.fr.stable.db.action.DBAction; +import com.fr.stable.db.dao.DAOContext; +import com.fr.stable.db.dao.DAOProvider; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +@EnableMetrics +public class DemoDbController extends AbstractDBAccessProvider { + + private static DBAccessor accessor; + + public static DBAccessor getAccessor() { + return accessor; + } + + @Override + public DAOProvider[] registerDAO() { + return new DAOProvider[]{ + DemoDao.DAO, + LinkDao.DAO + }; + } + + @Override + public void onDBAvailable(DBAccessor accessor) { + DemoDbController.accessor = accessor; + } + + /** + * 业务对象——增 + * @param bean + */ + public static void add( final DataBean bean ){ + try{ + accessor.runDMLAction(new DBAction() { + @Override + public Boolean run(DAOContext context) throws Exception { + context.getDAO(DemoDao.class).add( loadDemoEntity(bean,true) ); + context.getDAO(LinkDao.class).add( loadLinkEntities(bean,true) ); + return true; + } + }); + }catch(Throwable e){ + LogKit.error(e,"Add DataBean Error:{}",e.getMessage()); + } + } + + /** + * 业务对象——删 + * @param id + */ + public static DataBean delete( final String id ){ + try{ + return accessor.runDMLAction(new DBAction() { + @Override + public DataBean run(DAOContext context) throws Exception { + DemoEntity entity = context.getDAO(DemoDao.class).getById(id); + if( null == entity ){ + return null; + } + context.getDAO(DemoDao.class).remove( id ); + List list = context.getDAO(LinkDao.class).removeByKey(entity.getKey()); + return toDataBean(entity,list); + } + }); + }catch(Throwable e){ + LogKit.error(e,"Delete DataBean[{}] Error:{}",id,e.getMessage()); + } + return null; + } + + /** + * 业务对象——改 + * @param bean + */ + public static void update( DataBean bean ){ + try{ + accessor.runDMLAction(new DBAction() { + @Override + public Boolean run(DAOContext context) throws Exception { + context.getDAO(DemoDao.class).update( loadDemoEntity(bean,false) ); + context.getDAO(LinkDao.class).addOrUpdate( loadLinkEntities(bean,false) ); + return true; + } + }); + }catch(Throwable e){ + LogKit.error(e,"Update DataBean Error:{}",e.getMessage()); + } + } + + + + + /** + * 业务对象——查 + * @param key + * @return + */ + @Focus(id = "com.tptj.demo.hg.decision.db.access.provider.v10",text = "DBAccessProvider") + public static List like(String key ){ + try{ + return accessor.runQueryAction(new DBAction>() { + @Override + public List run(DAOContext context) throws Exception { + List result = new ArrayList(); + List list = context.getDAO(DemoDao.class).likeByKey(key); + for( DemoEntity item : list ){ + List links = context.getDAO(LinkDao.class).getByKey(item.getKey()); + result.add( toDataBean(item,links) ); + } + return result; + } + }); + }catch(Throwable e){ + LogKit.error(e,"Get DataBeans{} Error:{}",key,e.getMessage()); + } + return new ArrayList(); + } + + private static DemoEntity loadDemoEntity( DataBean bean, boolean isAdd ){ + DemoEntity entity = new DemoEntity(); + if(isAdd || StringUtils.isEmpty( bean.getId() ) ){ + bean.setId(UUIDUtil.generate()); + } + entity.setId( bean.getId() ); + entity.setKey( bean.getKey() ); + entity.setBean( bean.getBean() ); + return entity; + } + + private static DataBean toDataBean( DemoEntity entity,List list ){ + DataBean bean = new DataBean(); + bean.setId( entity.getId() ); + bean.setKey( entity.getKey() ); + bean.setBean( entity.getBean() ); + bean.setAttr3( toAttrBeans(list) ); + return bean; + } + + private static List toAttrBeans( List links ){ + List list = new ArrayList(); + for( LinkEntity item : links ){ + DemoBean bean = new DemoBean(); + bean.setAttr1(item.getId()); + bean.setAttr2(item.getAttr3()); + list.add( bean ); + } + return list; + } + + private static List loadLinkEntities( DataBean bean, boolean isAdd ){ + List list = new ArrayList(); + List attr3 = bean.getAttr3(); + for( DemoBean item : attr3 ){ + if(isAdd || StringUtils.isEmpty( item.getAttr1() ) ){ + item.setAttr1(UUIDUtil.generate()); + } + list.add( new LinkEntity() + .id( item.getAttr1() ) + .key( bean.getKey() ) + .attr3( item.getAttr2() ) + ); + } + return list; + } + +} diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/DemoEntity.java b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoEntity.java new file mode 100644 index 0000000..e811e0a --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/DemoEntity.java @@ -0,0 +1,45 @@ +package com.tptj.demo.hg.db.access.provider; + +import com.fr.stable.db.entity.BaseEntity; +import com.fr.stable.db.entity.TableAssociation; +import com.fr.third.javax.persistence.Column; +import com.fr.third.javax.persistence.Convert; +import com.fr.third.javax.persistence.Entity; +import com.fr.third.javax.persistence.Table; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +@Entity +@Table(name = "plugin_demo") +@TableAssociation(associated = true) +public class DemoEntity extends BaseEntity { + + public static final String COLUMN_KEY = "key"; + public static final String COLUMN_BEAN = "bean"; + + @Column(name = COLUMN_KEY) + private String key = null; + + @Column(name = COLUMN_BEAN) + @Convert(converter = BeanConverter.class) + private DemoBean bean = null; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public DemoBean getBean() { + return bean; + } + + public void setBean(DemoBean bean) { + this.bean = bean; + } +} diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/LinkDao.java b/src/main/java/com/tptj/demo/hg/db/access/provider/LinkDao.java new file mode 100644 index 0000000..30201fb --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/LinkDao.java @@ -0,0 +1,89 @@ +package com.tptj.demo.hg.db.access.provider; + +import com.fr.stable.db.dao.BaseDAO; +import com.fr.stable.db.dao.DAOProvider; +import com.fr.stable.db.entity.BaseEntity; +import com.fr.stable.db.session.DAOSession; +import com.fr.stable.query.QueryFactory; +import com.fr.stable.query.condition.QueryCondition; +import com.fr.stable.query.restriction.RestrictionFactory; + +import java.util.List; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +public class LinkDao extends BaseDAO { + public LinkDao(DAOSession session) { + super(session); + } + + @Override + protected Class getEntityClass() { + return LinkEntity.class; + } + + public final static DAOProvider DAO = new DAOProvider() { + @Override + public Class getEntityClass() { + return LinkEntity.class; + } + + @Override + public Class getDAOClass() { + return LinkDao.class; + } + }; + + public void add( List list )throws Exception { + for( LinkEntity item : list ){ + add(item); + } + } + + public void addOrUpdate( List list )throws Exception { + for( LinkEntity item : list ){ + addOrUpdate(item); + } + } + + public List removeByKey( String key )throws Exception{ + QueryCondition condition = QueryFactory.create().addRestriction(RestrictionFactory.eq(LinkEntity.COLUMN_KEY, key)); + List list = find(condition); + for( LinkEntity item : list ){ + remove( item.getId() ); + } + return list; + } + + public List getByKey( String key )throws Exception{ + QueryCondition condition = QueryFactory.create().addRestriction(RestrictionFactory.eq(LinkEntity.COLUMN_KEY, key)); + return find(condition); + } + + public void add(LinkEntity entity) throws Exception { + getSession().persist(entity); + } + + public void update(LinkEntity entity) throws Exception { + getSession().merge(entity); + } + + public void addOrUpdate(LinkEntity entity) throws Exception { + if (entity != null && this.getById(entity.getId()) != null) { + update(entity); + } else { + add(entity); + } + } + + public LinkEntity getById(String id) throws Exception { + return this.getSession().getById(id, LinkEntity.class); + } + + public void remove(String id) throws Exception { + this.getSession().remove(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", id)), this.getEntityClass()); + } +} diff --git a/src/main/java/com/tptj/demo/hg/db/access/provider/LinkEntity.java b/src/main/java/com/tptj/demo/hg/db/access/provider/LinkEntity.java new file mode 100644 index 0000000..fbc6771 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/db/access/provider/LinkEntity.java @@ -0,0 +1,61 @@ +package com.tptj.demo.hg.db.access.provider; + +import com.fr.stable.db.entity.BaseEntity; +import com.fr.stable.db.entity.TableAssociation; +import com.fr.third.javax.persistence.Column; +import com.fr.third.javax.persistence.Entity; +import com.fr.third.javax.persistence.Table; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021-04-25 + **/ +@Entity +@Table(name = "plugin_demo_link") +@TableAssociation(associated = true) +public class LinkEntity extends BaseEntity { + + public static final String COLUMN_KEY = "key"; + public static final String COLUMN_ATTR = "attr3"; + + @Column(name = COLUMN_KEY) + private String key = null; + + @Column(name = COLUMN_ATTR) + private String attr3 = null; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getAttr3() { + return attr3; + } + + public void setAttr3(String attr3) { + this.attr3 = attr3; + } + + public LinkEntity key(String key) { + this.setKey(key); + return this; + } + + public LinkEntity attr3(String attr3) { + this.setAttr3(attr3); + return this; + } + + public LinkEntity id(String id) { + this.setId(id); + return this; + } + + +} +