|
|
@@ -1,7 +1,19 @@
|
|
|
package com.uas.search.console.dcn;
|
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
|
+import com.uas.search.console.core.util.ContextUtils;
|
|
|
+import com.uas.search.console.dao.BrandSimpleInfoDao;
|
|
|
+import com.uas.search.console.dao.ComponentSimpleInfoDao;
|
|
|
+import com.uas.search.console.dao.KindSimpleInfoDao;
|
|
|
+import com.uas.search.console.model.BrandSimpleInfo;
|
|
|
+import com.uas.search.console.model.ComponentSimpleInfo;
|
|
|
+import com.uas.search.console.model.KindSimpleInfo;
|
|
|
+import com.uas.search.console.service.IndexService;
|
|
|
+import com.uas.search.console.util.SearchConstants;
|
|
|
+import com.uas.search.service.SearchService;
|
|
|
+
|
|
|
import oracle.jdbc.dcn.DatabaseChangeEvent;
|
|
|
import oracle.jdbc.dcn.DatabaseChangeListener;
|
|
|
import oracle.jdbc.dcn.QueryChangeDescription;
|
|
|
@@ -18,8 +30,26 @@ import oracle.sql.ROWID;
|
|
|
*/
|
|
|
public class DCNListener implements DatabaseChangeListener {
|
|
|
|
|
|
+ private IndexService indexService;
|
|
|
+
|
|
|
+ private SearchService searchService;
|
|
|
+
|
|
|
+ private KindSimpleInfoDao kindDao;
|
|
|
+
|
|
|
+ private BrandSimpleInfoDao brandDao;
|
|
|
+
|
|
|
+ private ComponentSimpleInfoDao componentDao;
|
|
|
+
|
|
|
private Logger logger = Logger.getLogger(DCNListener.class);
|
|
|
|
|
|
+ public DCNListener() {
|
|
|
+ indexService = ContextUtils.getBean(IndexService.class);
|
|
|
+ searchService = ContextUtils.getBean(SearchService.class);
|
|
|
+ kindDao = ContextUtils.getBean(KindSimpleInfoDao.class);
|
|
|
+ brandDao = ContextUtils.getBean(BrandSimpleInfoDao.class);
|
|
|
+ componentDao = ContextUtils.getBean(ComponentSimpleInfoDao.class);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void onDatabaseChangeNotification(DatabaseChangeEvent event) {
|
|
|
logger.info(event);
|
|
|
@@ -38,14 +68,61 @@ public class DCNListener implements DatabaseChangeListener {
|
|
|
logger.info("rowOperation " + rowOperation);
|
|
|
ROWID rowid = rowChangeDescription.getRowid();
|
|
|
logger.info(rowid);
|
|
|
- updateIndex(tableName, rowOperation.name(), rowid.stringValue());
|
|
|
+ updateIndex(tableName, rowOperation, rowid.stringValue());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void updateIndex(String tableName, String rowOperation, String rowid) {
|
|
|
-
|
|
|
+ private void updateIndex(String tableName, RowOperation rowOperation, String rowid) {
|
|
|
+ if (rowOperation.equals(RowOperation.DELETE)) {
|
|
|
+ indexService.delete(getObject(tableName, rowid));
|
|
|
+ } else if (rowOperation.equals(RowOperation.INSERT)) {
|
|
|
+ indexService.save(getObject(tableName, rowid));
|
|
|
+ } else if (rowOperation.equals(RowOperation.UPDATE)) {
|
|
|
+ indexService.update(getObject(tableName, rowid));
|
|
|
+ } else {
|
|
|
+ logger.error("DCN error: tableName " + tableName + " " + rowOperation + " rowid " + rowid);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ private Object getObject(String tableName, String rowid) {
|
|
|
+ if (tableName.equals(SearchConstants.KIND_TABLE_NAME)) {
|
|
|
+ KindSimpleInfo kind = kindDao.findByRowid(rowid);
|
|
|
+ if (kind == null) {
|
|
|
+ kind = new KindSimpleInfo();
|
|
|
+ kind.setRowid(rowid);
|
|
|
+ }
|
|
|
+ return kind;
|
|
|
+ } else if (tableName.equals(SearchConstants.BRAND_TABLE_NAME)) {
|
|
|
+ BrandSimpleInfo brand = brandDao.findByRowid(rowid);
|
|
|
+ if (brand == null) {
|
|
|
+ brand = new BrandSimpleInfo();
|
|
|
+ brand.setRowid(rowid);
|
|
|
+ }
|
|
|
+ return brand;
|
|
|
+ } else if (tableName.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
|
|
|
+ ComponentSimpleInfo component = componentDao.findByRowid(rowid);
|
|
|
+ if (component == null) {
|
|
|
+ component = new ComponentSimpleInfo();
|
|
|
+ component.setRowid(rowid);
|
|
|
+ }
|
|
|
+ return component;
|
|
|
+ } else if (tableName.equals(SearchConstants.PROPERTYVALUE_TABLE_NAME)) {
|
|
|
+ // 从本地获取component的rowid
|
|
|
+ String componentRowid = searchService.getComponentRowidByPropertyValueRowid(rowid);
|
|
|
+ if (StringUtils.isEmpty(componentRowid)) {
|
|
|
+ logger.error("未找到属性值rowid " + rowid + " 所对应的器件id");
|
|
|
+ }
|
|
|
+ ComponentSimpleInfo component = componentDao.findByRowid(componentRowid);
|
|
|
+ if (component == null) {
|
|
|
+ component = new ComponentSimpleInfo();
|
|
|
+ component.setRowid(rowid);
|
|
|
+ }
|
|
|
+ return component;
|
|
|
+ } else {
|
|
|
+ logger.error("DCN error: tableName " + tableName + " rowid " + rowid);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|