forked from fanruan/design
Browse Source
* commit '77cfbde529d6d8a6dbca58b294184a12c176b404': 代码质量 代码质量 9.0同步到10.0 REPORT-5856 去除无效action REPORT-5856 插件管理插件新分类 REPORT-6544 移除无用的打印信息 插件搜索直接打开 REPORT-6544 使用Lucene,重新实现RecentSearchManagermaster
superman
7 years ago
18 changed files with 593 additions and 570 deletions
@ -1,303 +0,0 @@
|
||||
package com.fr.design.mainframe.alphafine.search.manager; |
||||
|
||||
import com.fr.base.FRContext; |
||||
import com.fr.base.Utils; |
||||
import com.fr.design.mainframe.alphafine.AlphaFineConstants; |
||||
import com.fr.design.mainframe.alphafine.AlphaFineHelper; |
||||
import com.fr.design.mainframe.alphafine.CellType; |
||||
import com.fr.design.mainframe.alphafine.cell.CellModelHelper; |
||||
import com.fr.design.mainframe.alphafine.cell.model.ActionModel; |
||||
import com.fr.design.mainframe.alphafine.cell.model.AlphaCellModel; |
||||
import com.fr.design.mainframe.alphafine.cell.model.MoreModel; |
||||
import com.fr.design.mainframe.alphafine.model.SearchResult; |
||||
import com.fr.design.mainframe.toolbar.UpdateActionManager; |
||||
import com.fr.file.XMLFileManager; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.general.FRLogger; |
||||
import com.fr.general.IOUtils; |
||||
import com.fr.general.Inter; |
||||
import com.fr.json.JSONException; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.stable.ProductConstants; |
||||
import com.fr.stable.StableUtils; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.project.ProjectConstants; |
||||
import com.fr.stable.xml.XMLPrintWriter; |
||||
import com.fr.stable.xml.XMLReadable; |
||||
import com.fr.stable.xml.XMLTools; |
||||
import com.fr.stable.xml.XMLableReader; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
import java.io.StringReader; |
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Created by XiaXiang on 2017/5/15. |
||||
*/ |
||||
public class RecentSearchManager extends XMLFileManager implements AlphaFineSearchProcessor { |
||||
|
||||
private static final String XML_TAG = "AlphaFineRecent"; |
||||
private static final int MAX_SIZE = 3; |
||||
private static RecentSearchManager recentSearchManager = null; |
||||
private static File recentFile = null; |
||||
private SearchResult modelList; |
||||
private SearchResult recentModelList; |
||||
private Map<String, SearchResult> recentKVModelMap = new HashMap<>(); |
||||
|
||||
public synchronized static RecentSearchManager getRecentSearchManger() { |
||||
if (recentSearchManager == null) { |
||||
recentSearchManager = new RecentSearchManager(); |
||||
try { |
||||
XMLTools.readFileXML(recentSearchManager, recentSearchManager.getRecentEnvFile()); |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
return recentSearchManager; |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void readXML(XMLableReader reader) { |
||||
if (reader.isAttr()) { |
||||
reader.readXMLObject(new XMLReadable() { |
||||
public void readXML(XMLableReader reader) { |
||||
if (reader.isChildNode()) { |
||||
String nodeName = reader.getTagName(); |
||||
if (nodeName.equals("RecentModelList")) { |
||||
String key = reader.getAttrAsString("searchKey", StringUtils.EMPTY); |
||||
final SearchResult list = new SearchResult(); |
||||
reader.readXMLObject(new XMLReadable() { |
||||
@Override |
||||
public void readXML(XMLableReader reader) { |
||||
readCellModel(reader, list); |
||||
} |
||||
} |
||||
); |
||||
recentKVModelMap.put(key, list); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
} |
||||
|
||||
private void readCellModel(XMLableReader reader, List<AlphaCellModel> list) { |
||||
if (reader.isChildNode()) { |
||||
String nodeName = reader.getTagName(); |
||||
if (nodeName.equals("model")) { |
||||
String modelValue = reader.getAttrAsString("cellModel", StringUtils.EMPTY); |
||||
addModelToList(list, modelValue); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
private void addModelToList(List<AlphaCellModel> list, String modelValue) { |
||||
try { |
||||
AlphaCellModel model = CellModelHelper.getModelFromJson(new JSONObject(modelValue)); |
||||
if (model != null) { |
||||
list.add(model); |
||||
} |
||||
} catch (JSONException e) { |
||||
FRLogger.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void writeXML(XMLPrintWriter writer) { |
||||
writer.startTAG(XML_TAG); |
||||
if (!recentKVModelMap.isEmpty()) { |
||||
for (String key : recentKVModelMap.keySet()) { |
||||
writer.startTAG("RecentModelList"); |
||||
writer.attr("searchKey", key); |
||||
for (AlphaCellModel model : recentKVModelMap.get(key)) { |
||||
try { |
||||
String modelValue = model.ModelToJson().toString(); |
||||
writer.startTAG("model"); |
||||
writer.attr("cellModel", modelValue); |
||||
writer.end(); |
||||
} catch (JSONException e) { |
||||
FRLogger.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
writer.end(); |
||||
} |
||||
} |
||||
writer.end(); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public String fileName() { |
||||
return "AlphaFine_Recent.xml"; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取xml |
||||
* |
||||
* @return |
||||
*/ |
||||
private File getRecentFile() { |
||||
if (recentFile == null) { |
||||
recentFile = new File(ProductConstants.getEnvHome() + File.separator + fileName()); |
||||
} |
||||
return recentFile; |
||||
} |
||||
|
||||
private File getRecentEnvFile() { |
||||
File envFile = getRecentFile(); |
||||
if (!envFile.exists()) { |
||||
createRecentFile(envFile); |
||||
} |
||||
return envFile; |
||||
} |
||||
|
||||
/** |
||||
* 创建XML |
||||
* |
||||
* @param envFile |
||||
*/ |
||||
private void createRecentFile(File envFile) { |
||||
try { |
||||
FileWriter fileWriter = new FileWriter(envFile); |
||||
StringReader stringReader = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><AlphaFineRecent></AlphaFineRecent>"); |
||||
Utils.copyCharTo(stringReader, fileWriter); |
||||
stringReader.close(); |
||||
fileWriter.close(); |
||||
} catch (IOException e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 保存XML到设计器安装目录 |
||||
*/ |
||||
public void saveXMLFile() { |
||||
File xmlFile = this.getRecentEnvFile(); |
||||
if (xmlFile == null) { |
||||
return; |
||||
} |
||||
if (!xmlFile.getParentFile().exists()) {//建立目录.
|
||||
StableUtils.mkdirs(xmlFile.getParentFile()); |
||||
} |
||||
|
||||
String tempName = xmlFile.getName() + ProjectConstants.TEMP_SUFFIX; |
||||
File tempFile = new File(xmlFile.getParentFile(), tempName); |
||||
|
||||
writeTempFile(tempFile); |
||||
IOUtils.renameTo(tempFile, xmlFile); |
||||
} |
||||
|
||||
private void writeTempFile(File tempFile) { |
||||
try { |
||||
OutputStream fout = new FileOutputStream(tempFile); |
||||
XMLTools.writeOutputStreamXML(this, fout); |
||||
fout.flush(); |
||||
fout.close(); |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
|
||||
public List<AlphaCellModel> getRecentModelList() { |
||||
return recentModelList; |
||||
} |
||||
|
||||
/** |
||||
* 根据搜索字段获取对应的model列表 |
||||
* |
||||
* @param searchText |
||||
* @return |
||||
*/ |
||||
private synchronized SearchResult getRecentModelList(String searchText) { |
||||
recentModelList = new SearchResult(); |
||||
for (String key : recentKVModelMap.keySet()) { |
||||
AlphaFineHelper.checkCancel(); |
||||
if (ComparatorUtils.equals(key, searchText)) { |
||||
recentModelList = recentKVModelMap.get(searchText); |
||||
SearchResult resultModelList = recentModelList; |
||||
Iterator<AlphaCellModel> modelIterator = resultModelList.iterator(); |
||||
SearchResult searchResult = new SearchResult(); |
||||
while (modelIterator.hasNext()) { |
||||
AlphaCellModel model = modelIterator.next(); |
||||
if (model.getType() == CellType.ACTION && !UpdateActionManager.getUpdateActionManager().isEnable(((ActionModel) model).getAction())) { |
||||
continue; |
||||
} else { |
||||
searchResult.add(model); |
||||
} |
||||
|
||||
} |
||||
Collections.sort(searchResult); |
||||
int size = searchResult.size(); |
||||
if (size > MAX_SIZE) { |
||||
SearchResult result = new SearchResult(); |
||||
result.addAll(searchResult.subList(0, MAX_SIZE)); |
||||
return result; |
||||
} |
||||
return searchResult; |
||||
} |
||||
} |
||||
return recentModelList; |
||||
} |
||||
|
||||
/** |
||||
* 将搜索结果加入到当前MAP中 |
||||
* |
||||
* @param searchKey |
||||
* @param cellModel |
||||
*/ |
||||
public void addRecentModel(String searchKey, AlphaCellModel cellModel) { |
||||
if (recentKVModelMap.keySet().contains(searchKey)) { |
||||
List<AlphaCellModel> cellModels = recentKVModelMap.get(searchKey); |
||||
int index = cellModels.indexOf(cellModel); |
||||
if (index >= 0) { |
||||
cellModels.get(index).addSearchCount(); |
||||
} else { |
||||
cellModels.add(cellModel); |
||||
} |
||||
//trimToSize(cellModels);
|
||||
} else { |
||||
SearchResult list = new SearchResult(); |
||||
list.add(cellModel); |
||||
recentKVModelMap.put(searchKey, list); |
||||
} |
||||
} |
||||
|
||||
|
||||
private synchronized void trimToSize(List<AlphaCellModel> cellModels) { |
||||
if (cellModels.size() > AlphaFineConstants.MAX_FILE_SIZE) { |
||||
cellModels.remove(0); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public synchronized SearchResult getLessSearchResult(String searchText) { |
||||
this.modelList = new SearchResult(); |
||||
recentModelList = getRecentModelList(searchText); |
||||
if (recentModelList != null && recentModelList.size() > 0) { |
||||
modelList.add(new MoreModel(Inter.getLocText("FR-Designer_AlphaFine_Latest"))); |
||||
} |
||||
modelList.addAll(recentModelList); |
||||
return modelList; |
||||
} |
||||
|
||||
@Override |
||||
public SearchResult getMoreSearchResult() { |
||||
return new SearchResult(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,205 @@
|
||||
package com.fr.design.mainframe.alphafine.search.manager.impl; |
||||
|
||||
import com.fr.design.actions.UpdateAction; |
||||
import com.fr.design.mainframe.alphafine.CellType; |
||||
import com.fr.design.mainframe.alphafine.cell.CellModelHelper; |
||||
import com.fr.design.mainframe.alphafine.cell.model.ActionModel; |
||||
import com.fr.design.mainframe.alphafine.cell.model.AlphaCellModel; |
||||
import com.fr.design.mainframe.alphafine.cell.model.MoreModel; |
||||
import com.fr.design.mainframe.alphafine.model.SearchResult; |
||||
import com.fr.design.mainframe.alphafine.search.manager.fun.AlphaFineSearchProvider; |
||||
import com.fr.design.mainframe.toolbar.UpdateActionManager; |
||||
import com.fr.general.Inter; |
||||
import com.fr.json.JSONException; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FRLoggerFactory; |
||||
import com.fr.stable.ProductConstants; |
||||
import com.fr.third.org.apache.lucene.analysis.Analyzer; |
||||
import com.fr.third.org.apache.lucene.analysis.standard.StandardAnalyzer; |
||||
import com.fr.third.org.apache.lucene.document.Document; |
||||
import com.fr.third.org.apache.lucene.document.Field; |
||||
import com.fr.third.org.apache.lucene.document.IntField; |
||||
import com.fr.third.org.apache.lucene.document.StringField; |
||||
import com.fr.third.org.apache.lucene.index.DirectoryReader; |
||||
import com.fr.third.org.apache.lucene.index.IndexReader; |
||||
import com.fr.third.org.apache.lucene.index.IndexWriter; |
||||
import com.fr.third.org.apache.lucene.index.IndexWriterConfig; |
||||
import com.fr.third.org.apache.lucene.index.Term; |
||||
import com.fr.third.org.apache.lucene.search.IndexSearcher; |
||||
import com.fr.third.org.apache.lucene.search.Query; |
||||
import com.fr.third.org.apache.lucene.search.ScoreDoc; |
||||
import com.fr.third.org.apache.lucene.search.Sort; |
||||
import com.fr.third.org.apache.lucene.search.SortField; |
||||
import com.fr.third.org.apache.lucene.search.TermQuery; |
||||
import com.fr.third.org.apache.lucene.search.TopFieldDocs; |
||||
import com.fr.third.org.apache.lucene.store.Directory; |
||||
import com.fr.third.org.apache.lucene.store.FSDirectory; |
||||
import com.fr.third.org.apache.lucene.util.Version; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Created by XiaXiang on 2018/1/22. |
||||
*/ |
||||
public class RecentSearchManager implements AlphaFineSearchProvider { |
||||
private static final int MAX_SIZE = 3; |
||||
private static RecentSearchManager recentSearchManager = null; |
||||
IndexReader indexReader = null; |
||||
IndexSearcher indexSearcher = null; |
||||
//索引存储路径
|
||||
private String path = ProductConstants.getEnvHome() + File.separator + "searchIndex"; |
||||
//分词器,暂时先用这个
|
||||
private Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); |
||||
// 存储目录
|
||||
private Directory directory = null; |
||||
private IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer); |
||||
private IndexWriter indexWriter = null; |
||||
private SearchResult recentModelList; |
||||
private SearchResult modelList; |
||||
|
||||
public synchronized static RecentSearchManager getInstance() { |
||||
if (recentSearchManager == null) { |
||||
recentSearchManager = new RecentSearchManager(); |
||||
} |
||||
return recentSearchManager; |
||||
} |
||||
|
||||
@Override |
||||
public SearchResult getLessSearchResult(String searchText) { |
||||
this.modelList = new SearchResult(); |
||||
recentModelList = getRecentModelList(searchText); |
||||
if (recentModelList != null && recentModelList.size() > 0) { |
||||
modelList.add(new MoreModel(Inter.getLocText("FR-Designer_AlphaFine_Latest"))); |
||||
} |
||||
modelList.addAll(recentModelList); |
||||
return modelList; |
||||
} |
||||
|
||||
@Override |
||||
public SearchResult getMoreSearchResult(String searchText) { |
||||
return new SearchResult(); |
||||
} |
||||
|
||||
public synchronized SearchResult getRecentModelList(String searchText) { |
||||
return searchBySort(searchText); |
||||
} |
||||
|
||||
public List<AlphaCellModel> getRecentModelList() { |
||||
return recentModelList; |
||||
} |
||||
|
||||
/** |
||||
* 初始化indexWriter |
||||
*/ |
||||
private void initWriter() { |
||||
if (indexWriter == null) { |
||||
try { |
||||
directory = FSDirectory.open(new File(path)); |
||||
} catch (IOException e) { |
||||
FRLoggerFactory.getLogger().error("cannot open directory " + path); |
||||
} |
||||
try { |
||||
indexWriter = new IndexWriter(directory, config); |
||||
} catch (IOException e) { |
||||
FRLoggerFactory.getLogger().error("not privilege to write to" + path); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 初始化indexReader |
||||
*/ |
||||
private void initReader() { |
||||
if (indexReader == null) { |
||||
try { |
||||
directory = FSDirectory.open(new File(path)); |
||||
indexReader = DirectoryReader.open(directory); |
||||
} catch (IOException e) { |
||||
FRLoggerFactory.getLogger().error("not privilege to read " + path); |
||||
} |
||||
indexSearcher = new IndexSearcher(indexReader); |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 添加模型 |
||||
* @param searchKey |
||||
* @param cellModel |
||||
* @param searchCount |
||||
*/ |
||||
public void addModel(String searchKey, AlphaCellModel cellModel, int searchCount) { |
||||
try { |
||||
initWriter(); |
||||
Document doc = new Document(); |
||||
doc.add(new StringField("searchKey", searchKey, Field.Store.YES)); |
||||
doc.add(new StringField("cellModel", cellModel.ModelToJson().toString(), Field.Store.YES)); |
||||
doc.add(new IntField("searchCount", searchCount, Field.Store.YES)); |
||||
writeDoc(doc); |
||||
} catch (JSONException e) { |
||||
FRLoggerFactory.getLogger().error("add document error: " + e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 写文档,建立索引 |
||||
* @param doc |
||||
*/ |
||||
private void writeDoc(Document doc) { |
||||
try { |
||||
indexWriter.addDocument(doc); |
||||
indexWriter.commit(); |
||||
indexWriter.close(); |
||||
} catch (IOException e) { |
||||
FRLoggerFactory.getLogger().error("write document error: " + e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 按序搜索 |
||||
* @param key |
||||
* @return |
||||
*/ |
||||
private synchronized SearchResult searchBySort(String key) { |
||||
recentModelList = new SearchResult(); |
||||
try { |
||||
initReader(); |
||||
IndexSearcher searcher = new IndexSearcher(indexReader); |
||||
//构建排序字段
|
||||
SortField[] sortField = new SortField[1]; |
||||
sortField[0] = new SortField("searchCount", SortField.Type.INT, true); |
||||
Sort sortKey = new Sort(sortField); |
||||
String searchField = "searchKey"; |
||||
Term term = new Term(searchField, key); |
||||
Query query = new TermQuery(term); |
||||
TopFieldDocs docs = searcher.search(query, MAX_SIZE, sortKey); |
||||
ScoreDoc[] scores = docs.scoreDocs; |
||||
this.recentModelList = new SearchResult(); |
||||
//遍历结果
|
||||
for (ScoreDoc scoreDoc : scores) { |
||||
Document document = searcher.doc(scoreDoc.doc); |
||||
AlphaCellModel model = CellModelHelper.getModelFromJson(new JSONObject(document.get("cellModel"))); |
||||
if (model.getType() == CellType.ACTION) { |
||||
UpdateAction action = UpdateActionManager.getUpdateActionManager().getActionByName(model.getName()); |
||||
if (action != null) { |
||||
((ActionModel) model).setAction(action); |
||||
recentModelList.add(model); |
||||
} |
||||
} else { |
||||
recentModelList.add(model); |
||||
} |
||||
|
||||
|
||||
} |
||||
} catch (Exception e) { |
||||
FRLoggerFactory.getLogger().error("local search error: " + e.getMessage()); |
||||
return recentModelList; |
||||
} |
||||
return recentModelList; |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue