From e27c43f254793a834f4713637494b088bdb34680 Mon Sep 17 00:00:00 2001 From: 杜洪波 <1074825718@qq.com> Date: 星期五, 30 八月 2024 18:31:50 +0800 Subject: [PATCH] 文档检索代码优化 --- src/main/java/com/product/lucene/service/LuceneService.java | 644 +++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 386 insertions(+), 258 deletions(-) diff --git a/src/main/java/com/product/lucene/service/LuceneService.java b/src/main/java/com/product/lucene/service/LuceneService.java index 1c7017e..1a82ed6 100644 --- a/src/main/java/com/product/lucene/service/LuceneService.java +++ b/src/main/java/com/product/lucene/service/LuceneService.java @@ -2,10 +2,13 @@ import com.alibaba.fastjson.JSONObject; import com.product.common.lang.StringUtils; +import com.product.core.config.CoreConst; import com.product.core.dao.BaseDao; +import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.permission.PermissionService; -import com.product.core.websocket.config.CmnConst; +import com.product.core.spring.context.SpringMVCContextHolder; +import com.product.lucene.config.CmnConst; import com.product.lucene.util.FileUtils; import com.product.util.BaseUtil; @@ -14,6 +17,7 @@ import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; +import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.*; import org.apache.lucene.search.highlight.Fragmenter; @@ -26,294 +30,417 @@ import java.io.File; import java.io.IOException; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * 鏂囦欢妫�绱� */ @Service("luceneService") -public class LuceneService{ - +public class LuceneService { + @Autowired BaseDao baseDao; + + @Autowired + private IndexWriter indexWriter; + + @Autowired + PermissionService permissionService; + + @Autowired + private SearcherManager searcherManager; - @Autowired - private IndexWriter indexWriter; - - @Autowired - PermissionService permissionService; - - @Autowired - private SearcherManager searcherManager; - /** - * 鍒涘缓绱㈠紩 - * @param fse - * @throws IOException - */ - public void createdIndex(FieldSetEntity fse) { - String function_uuid=fse.getString("function_uuid"); //鑾峰彇鍔熻兘 - File file=(File)fse.getObject("file"); //鑾峰彇闄勪欢 + /** + * 鍒涘缓绱㈠紩 + * + * @param fse + * @throws IOException + */ + public void createdIndex(FieldSetEntity fse) { + String function_uuid = fse.getString(CoreConst.FUNCTION_UUID); // 鑾峰彇鍔熻兘 + File file = (File) fse.getObject("file"); // 鑾峰彇闄勪欢 + + // 鑾峰彇鍏ㄦ枃妫�绱㈤厤缃� + FieldSetEntity fseConfig = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH, "function_uuid=?", new Object[] { function_uuid }, false); + if (fseConfig == null) + return; + String table_uuid = fseConfig.getString(CmnConst.TABLE_NAME); + + // 鑾峰彇缂撳瓨琛ㄩ厤缃� + FieldSetEntity fseTable = baseDao.getFieldSetEntity(CoreConst.PRODUCT_SYS_DATAMODEL_TABLE, table_uuid, false); + if (fseTable == null) + return; + + // 鑾峰彇闄勪欢uuids + String attachments = fse.getString(CmnConst.ATTACHMENT_UUID); + if (StringUtils.isEmpty(attachments)) + return; + + // 閬嶅巻鑾峰彇闄勪欢淇℃伅 + String[] attachment = attachments.split(","); + List<Document> docs = new ArrayList<>(); + for (int i = 0; i < attachment.length; i++) { + // 鑾峰彇闄勪欢淇℃伅 + FieldSetEntity fseAttachment = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, attachment[i], false); + if (fseAttachment == null || file == null) + return; + // 鐢熸垚绱㈠紩淇℃伅 + Document doc = new Document(); + doc.add(new StringField(CoreConst.UUID, attachment[i], Field.Store.YES)); // 闄勪欢UUID + doc.add(new StoredField(CoreConst.FUNCTION_UUID, function_uuid)); // 鍔熻兘UUID + doc.add(new StoredField(CmnConst.UPLOAD_USER, fseAttachment.getString(CoreConst.CREATED_BY))); // 涓婁紶浜� + doc.add(new LongPoint(CmnConst.UPLOAD_DATE, fseAttachment.getDate(CoreConst.CREATED_UTC_DATETIME).getTime())); // 涓婁紶鏃堕棿 + doc.add(new TextField(CmnConst.FILE_NAME, fseAttachment.getString(CmnConst.FILE_NAME), Field.Store.YES)); // 鏂囦欢鍚� + doc.add(new TextField(CmnConst.FILE_CONTENT, FileUtils.FileToString(file), Field.Store.YES)); // 鏂囦欢鍐呭 + docs.add(doc); + } + // 鍐欏叆绱㈠紩 + try { + indexWriter.addDocuments(docs); + indexWriter.commit(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + file.delete(); + } + } + + /** + * 鏂囨。妫�绱� + * + * @param fse + * @return + * @throws Exception + * @throws ParseException + */ + public JSONObject searchProduct(FieldSetEntity fse) throws Exception { + // 鍒锋柊IndexSearcher瀹炰緥 + searcherManager.maybeRefresh(); + // 鍒涘缓IndexSearcher瀹炰緥 + IndexSearcher indexSearcher = null; + // 鏂囨。淇℃伅姹囨�� + List<JSONObject> listJsonFile = new ArrayList<>(); + Long totalCount = 0L; + try { + // 鑾峰彇瀹夊叏鐨処ndexSearcher瀹炰緥 + indexSearcher = searcherManager.acquire(); + + // 鑾峰彇鏉′欢鏌ヨ缁撴灉 + Query finalQuery = searchFilterParse(fse); + + // 鑾峰彇楂樹寒鍖归厤鍣� + Highlighter highlighter = highlighterParse(finalQuery); + + // 鍒嗛〉鏌ヨ鏂囨。 + TopDocs topDocs = searchByPage(fse.getInteger(CoreConst.CPAGE), fse.getInteger(CoreConst.PAGESIZE), + indexSearcher, finalQuery); + // 鑾峰彇妫�绱㈠懡涓」锛屾�诲懡涓暟 + ScoreDoc[] hits = topDocs.scoreDocs; + totalCount = topDocs.totalHits.value; + for (int i = 0; i < hits.length; i++) { + // 鑾峰彇妫�绱㈠懡涓殑鏂囨。淇℃伅 + Document doc = indexSearcher.doc(hits[i].doc); + + JSONObject jsonFile = new JSONObject(); + jsonFile.put(CmnConst.FILE_UUID, doc.get(CoreConst.UUID)); + jsonFile.put(CoreConst.FUNCTION_UUID, doc.get(CoreConst.FUNCTION_UUID)); + + // 鏂囦欢鍚嶅鍔犻珮浜樉绀� + String fileName = doc.get(CmnConst.FILE_NAME); + String file_name = highlighter.getBestFragment(new SmartChineseAnalyzer(), CmnConst.FILE_NAME, + fileName); + if (StringUtils.isEmpty(file_name)) { + file_name = fileName; + } + // 鏂囦欢鍐呭澧炲姞楂樹寒鏄剧ず + String fileContent = doc.get(CmnConst.FILE_CONTENT); + String file_content = highlighter.getBestFragment(new SmartChineseAnalyzer(), CmnConst.FILE_CONTENT, + fileContent); + if (StringUtils.isEmpty(file_content)) { + file_content = fileContent; + } + + jsonFile.put(CmnConst.FILE_NAME, file_name); + jsonFile.put(CmnConst.FILE_CONTENT, file_content); + listJsonFile.add(jsonFile); + } + } finally { + // 鍏抽棴瀹炰緥璧勬簮 + searcherManager.release(indexSearcher); + } + // 鍒涘缓杩斿洖淇℃伅瀵硅薄 + JSONObject jsonReturn = new JSONObject(); + jsonReturn.put(CmnConst.TOTALCOUNT, totalCount); + if (listJsonFile.size() > 0) { + // 鑾峰彇鏂囨。灞曠ず淇℃伅 + getFileInfo(listJsonFile); + jsonReturn.put(CmnConst.DATA, listJsonFile); + } + return jsonReturn; + } + + /** + * 瑙f瀽鏌ヨ鏉′欢 + * @param fse + * @return + * @throws ParseException + */ + public Query searchFilterParse(FieldSetEntity fse) throws ParseException { + // 涓婁紶浜� + String uploadUser = fse.getString("upload_user"); + String startDate = fse.getString("start_date"); + String endDate = fse.getString("end_date"); + String searchKey = fse.getString(CmnConst.SEARCH_KEY); + + // 鍒涘缓鏌ヨ鏉′欢 + BooleanQuery.Builder boolQuery = new BooleanQuery.Builder(); - //鑾峰彇鍏ㄦ枃妫�绱㈤厤缃� - FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter("product_sys_document_search", "function_uuid=?", new Object[] {function_uuid}, false); - if (fseConfig!=null) { - String table_uuid=fseConfig.getString("table_name"); - - //鑾峰彇缂撳瓨琛ㄩ厤缃� - FieldSetEntity fseTable=baseDao.getFieldSetEntity("product_sys_datamodel_table", table_uuid, false); - if (fseTable!=null) { - - //鑾峰彇闄勪欢uuids - String attachments=fse.getString("attachment_uuid"); - if (!StringUtils.isEmpty(attachments)) { - - //閬嶅巻鑾峰彇闄勪欢淇℃伅 - String [] attachment=attachments.split(","); - List<Document> docs=new ArrayList<>(); - for (int i = 0; i < attachment.length; i++) { - - //鑾峰彇闄勪欢淇℃伅 - FieldSetEntity fseAttachment=baseDao.getFieldSetEntity("product_sys_attachments", attachment[i], false); - if (fseAttachment!=null) { - - if (file!=null) { - //鐢熸垚绱㈠紩淇℃伅 - Document doc=new Document(); - doc.add(new StringField("uuid", attachment[i], Field.Store.YES)); //闄勪欢UUID - doc.add(new TextField("function_uuid", function_uuid, Field.Store.YES)); //鍔熻兘UUID - doc.add(new TextField("file_name", fseAttachment.getString("file_name"), Field.Store.YES)); //鏂囦欢鍚� - doc.add(new TextField("file_content", FileUtils.FileToString(file), Field.Store.YES)); //鏂囦欢鍐呭唴瀹� - docs.add(doc); - } - } - } - //鍐欏叆绱㈠紩 - try { - indexWriter.addDocuments(docs); - indexWriter.commit(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + // 涓婁紶浜鸿繃婊� + if (!BaseUtil.strIsNull(uploadUser)) { + Query uploaderQuery = new TermQuery(new Term("upload_user", uploadUser)); + boolQuery.add(uploaderQuery, BooleanClause.Occur.MUST); + } + // 涓婁紶鏃堕棿杩囨护 + if (!BaseUtil.strIsNull(startDate) || !BaseUtil.strIsNull(endDate)) { + Long longStartDate = Long.MIN_VALUE; + Long longEndData = Long.MAX_VALUE; + if (!BaseUtil.strIsNull(startDate)) { + longStartDate = fse.getDate("start_date").getTime(); + } + if (!BaseUtil.strIsNull(endDate)) { + longEndData = fse.getDate("end_date").getTime(); + } + Query timeRangeQuery = LongPoint.newRangeQuery("upload_date", longStartDate, longEndData); + boolQuery.add(timeRangeQuery, BooleanClause.Occur.MUST); + } + + // 鍏抽敭璇嶈繃婊� + if (!BaseUtil.strIsNull(searchKey)) { + QueryParser queryParser = new MultiFieldQueryParser( + new String[] { CmnConst.FILE_NAME, CmnConst.FILE_CONTENT }, new SmartChineseAnalyzer()); + Query query = queryParser.parse(searchKey); + boolQuery.add(query, BooleanClause.Occur.MUST); + } + return boolQuery.build(); + } + + /** + * 鏌ヨ鏂囦欢楂樹寒鍖归厤鍣� + * @param finalQuery + * @return + */ + public Highlighter highlighterParse(Query finalQuery) { + // 楂樹寒鏍煎紡 + SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(CmnConst.HIGHLIGHT_BEFORE, + CmnConst.HIGHLIGHT_AFTER); + // 楂樹寒鍖归厤鍣� + Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(finalQuery)); + Fragmenter fragmenter = new SimpleFragmenter(100); + highlighter.setTextFragmenter(fragmenter); + return highlighter; + } + + /** + * 鑾峰彇鏂囨。灞曠ず淇℃伅 + * @param listJsonFile + */ + public void getFileInfo(List<JSONObject> listJsonFile) { + StringBuilder sql = new StringBuilder(); + for (int i = 0; i < listJsonFile.size(); i++) { + JSONObject jsonFile = listJsonFile.get(i); + if (i > 0) { + sql.append("UNION ALL \n"); + } + sql.append("SELECT \n"); + sql.append(" A.uuid,A.attachment_data_table,A.attachment_data_field,DATE_FORMAT(A.created_utc_datetime,'%Y-%m-%d') AS upload_date, \n"); + sql.append(" B.org_fields,B.user_fields,B.title_field,B.time_field, \n"); + sql.append(" C.uuid AS function_uuid,C.function_name,D.user_name AS upload_user "); + sql.append("FROM product_sys_attachments A \n"); + sql.append("LEFT JOIN product_sys_document_search B ON B.table_name = A.attachment_data_table \n"); + sql.append("LEFT JOIN product_sys_functions C ON C.uuid = B.function_uuid \n"); + sql.append("LEFT JOIN product_sys_users D ON D.user_id = A.created_by \n"); + sql.append("WHERE A.uuid = '").append(jsonFile.get("file_uuid")).append("' \n"); + } + // 鑾峰彇鏂囨。闄勪欢銆侀厤缃俊鎭� + DataTableEntity dtConfig = baseDao.listTable(sql.toString(), new Object[] {}); + if (!BaseUtil.dataTableIsEmpty(dtConfig)) { + StringBuilder sqlService = new StringBuilder(); + for (int i = 0; i < dtConfig.getRows(); i++) { + FieldSetEntity fseConfig = dtConfig.getFieldSetEntity(i); + if (i > 0) { + sqlService.append("UNION ALL \n"); + } + sqlService.append("SELECT \n"); + sqlService.append(" A.uuid, \n"); + sqlService.append(" B.uuid AS service_uuid, \n"); + sqlService.append(" A.upload_user, A.upload_date, A.function_uuid, A.function_name, \n"); + sqlService.append(" ").append(fseConfig.getString(CmnConst.TITLE_FIELD)).append(" AS service_title, \n"); + sqlService.append(" ").append(fseConfig.getString(CmnConst.TIME_FIELD)).append(" AS service_time \n"); + sqlService.append("FROM ( \n"); + sqlService.append(" SELECT \n"); + sqlService.append(" '").append(fseConfig.getString(CoreConst.UUID)).append("' AS uuid, \n"); + sqlService.append(" '").append(fseConfig.getString(CmnConst.UPLOAD_USER)).append("' AS upload_user, \n"); + sqlService.append(" '").append(fseConfig.getString(CmnConst.UPLOAD_DATE)).append("' AS upload_date, \n"); + sqlService.append(" '").append(fseConfig.getString(CoreConst.FUNCTION_UUID)).append("' AS function_uuid, \n"); + sqlService.append(" '").append(fseConfig.getString(CoreConst.FUNCTION_NAME)).append("' AS function_name \n"); + sqlService.append(") A \n"); + sqlService.append("LEFT JOIN ").append(fseConfig.getString(CmnConst.ATTACHMENT_DATA_TABLE)).append(" B \n"); + sqlService.append("ON B.").append(fseConfig.getString(CmnConst.ATTACHMENT_DATA_FIELD)).append(" LIKE '%").append(fseConfig.getUUID()).append("%' \n"); + } + // 鑾峰彇鏂囨。涓氬姟鏁版嵁淇℃伅 + DataTableEntity dtService = baseDao.listTable(sqlService.toString(), new Object[] {}); + if (!BaseUtil.dataTableIsEmpty(dtService)) { + Map<String, FieldSetEntity> map = dtService.getData().stream() + .collect(Collectors.toMap( + fseService -> fseService.getUUID(), + fseService -> fseService + )); + for (int i = 0; i < listJsonFile.size(); i++) { + JSONObject jsonFile = listJsonFile.get(i); + FieldSetEntity fseService = map.get(jsonFile.get(CmnConst.FILE_UUID)); + if (fseService != null) { + jsonFile.put(CoreConst.FUNCTION_NAME, fseService.getString(CoreConst.FUNCTION_NAME)); + jsonFile.put(CoreConst.FUNCTION_UUID, fseService.getString(CoreConst.FUNCTION_UUID)); + jsonFile.put(CmnConst.UPLOAD_USER, fseService.getString(CmnConst.UPLOAD_USER)); + jsonFile.put(CmnConst.UPLOAD_DATE, fseService.getString(CmnConst.UPLOAD_DATE)); + jsonFile.put(CmnConst.SERVICE_UUID, fseService.getString(CmnConst.SERVICE_UUID)); + jsonFile.put(CmnConst.SERVICE_TITLE, fseService.getString(CmnConst.SERVICE_TITLE)); + jsonFile.put(CmnConst.SERVICE_TIME, fseService.getString(CmnConst.SERVICE_TIME)); } } } } - file.delete(); - } - - -/* - public void createdIndex(FieldSetEntity fse) throws IOException { - String function_uuid=fse.getString("function_uuid"); - String service_uuid=fse.getString("service_uuid"); - - //鑾峰彇鍏ㄦ枃妫�绱㈤厤缃� - FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter("product_sys_document_search", "function_uuid=?", new Object[] {function_uuid}, false); - if (fseConfig!=null) { - String table_uuid=fseConfig.getString("table_name"); - String attachment_field=fseConfig.getString("attachment_file"); - - //鑾峰彇缂撳瓨琛ㄩ厤缃� - FieldSetEntity fseTable=baseDao.getFieldSetEntity("product_sys_datamodel_table", table_uuid, false); - if (fseTable!=null) { - - //鑾峰彇鍘熸暟鎹� - FieldSetEntity fseService=baseDao.getFieldSetEntityBySQL("SELECT * FROM "+fseTable.getString("table_name")+" WHERE uuid=?", new Object[] {service_uuid}, false); - if (fseService!=null) { - - //鑾峰彇闄勪欢uuids - String attachments=fseService.getString(attachment_field); - if (!StringUtils.isEmpty(attachments)) { - - //閬嶅巻鑾峰彇闄勪欢淇℃伅 - String [] attachment=attachments.split(","); - List<Document> docs=new ArrayList<>(); - for (int i = 0; i < attachment.length; i++) { - - //鑾峰彇闄勪欢淇℃伅 - FieldSetEntity fseAttachment=baseDao.getFieldSetEntity("product_sys_attachments", attachment[i], false); - if (fseAttachment!=null) { - //鑾峰彇闄勪欢 - File file=fileManagerService.getFile(attachment[i]); - if (file!=null) { - //鐢熸垚绱㈠紩淇℃伅 - Document doc=new Document(); - doc.add(new StringField("uuid", attachment[i], Field.Store.YES)); //闄勪欢UUID - doc.add(new TextField("service_uuid", service_uuid, Field.Store.YES)); //涓氬姟UUID - doc.add(new TextField("function_uuid", function_uuid, Field.Store.YES)); //鍏卞棷閭d釜UUID - doc.add(new TextField("file_name", fseAttachment.getString("file_name"), Field.Store.YES)); //鏂囦欢鍚� - doc.add(new TextField("file_content", FileUtils.FileToString(file), Field.Store.YES)); //鏂囦欢鍐呭唴瀹� - docs.add(doc); - } - file.delete(); - } - } - //鍐欏叆绱㈠紩 - indexWriter.addDocuments(docs); - indexWriter.commit(); - } - } + } + + public JSONObject permission(String uuid, String function_uuid) { + JSONObject json = new JSONObject(); + StringBuilder searchConfigSql = new StringBuilder(); + searchConfigSql.append("SELECT \n"); + searchConfigSql.append(" A.uuid,A.function_uuid,A.function_button_uuid,A.table_name, \n"); + searchConfigSql.append(" A.attachment_file,A.org_fields,A.user_fields,title_field,time_field, \n"); + searchConfigSql.append(" B.function_name,C.button_name \n"); + searchConfigSql.append("FROM product_sys_document_search A \n"); + searchConfigSql.append("LEFT JOIN product_sys_functions B ON A.function_uuid = B.uuid \n"); + searchConfigSql.append("LEFT JOIN product_sys_function_buttons C ON A.function_button_uuid = C.uuid \n"); + searchConfigSql.append("WHERE A.function_uuid=?"); + + // 鑾峰彇鏂囨。妫�绱㈤厤缃�(鍔熻兘锛岃烦杞寜閽�) + FieldSetEntity fseConfig = baseDao.getFieldSetEntityBySQL(searchConfigSql.toString(), new Object[] { function_uuid }, false); + if (fseConfig != null) { + + // 鑾峰彇璺宠浆鎸夐挳 骞� 鍒ゆ柇璺宠浆鏉冮檺 + String buttonName = fseConfig.getString(CoreConst.BUTTON_NAME); + if (BaseUtil.strIsNull(buttonName)) { + json.put(CmnConst.IS_PERSSION, 1); + json.put("permission_msg", "鏁版嵁閰嶇疆-璇ユ枃妗i厤缃笉鏀寔璺宠浆"); + return json; + } else { + json.put(CmnConst.IS_PERSSION, 0); + json.put(CmnConst.SKIPBUTTON, fseConfig.getString(CoreConst.BUTTON_NAME)); } - } - } -*/ - - /** - * 鏂囨。妫�绱� - * @param fse - * @return - * @throws Exception - * @throws ParseException - */ - public JSONObject searchProduct(FieldSetEntity fse) throws Exception, ParseException { - searcherManager.maybeRefresh(); - IndexSearcher indexSearcher = searcherManager.acquire(); - //鏂囨。淇℃伅姹囨�� - List<JSONObject> jsonTotal=new ArrayList<>(); - JSONObject jsonReturn=new JSONObject(); - - // 妯$硦鍖归厤,鍖归厤璇� - String search_key=fse.getString("search_key"); - if (!BaseUtil.strIsNull(search_key)) { - //鏌ヨ瑙f瀽鍣� - QueryParser parser = new MultiFieldQueryParser(new String [] {"file_name","file_content"}, new SmartChineseAnalyzer()); - Query query = parser.parse(search_key); - - //楂樹寒鏍煎紡 - SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>"); - //楂樹寒鍖归厤鍣� - Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query)); - Fragmenter fragmenter = new SimpleFragmenter(100); - highlighter.setTextFragmenter(fragmenter); - - //鍒嗛〉鏌ヨ鏂囨。 - TopDocs topDocs = searchByPage(fse.getInteger("cpage"), fse.getInteger("pagesize"), indexSearcher, query); - - - - ScoreDoc[] hits = topDocs.scoreDocs; - for (int i = 0; i < hits.length; i++) { - Document doc = indexSearcher.doc(hits[i].doc); - JSONObject json=new JSONObject(); - if (!StringUtils.isEmpty(doc.get("function_uuid"))) { - getConfig(json, doc.get("function_uuid"), doc.get("uuid")); - } - json.put("function_uuid", doc.get("function_uuid")); - - // 鍐呭澧炲姞楂樹寒鏄剧ず - String file_name=highlighter.getBestFragment(new SmartChineseAnalyzer(), "file_name",doc.get("file_name")); - if (StringUtils.isEmpty(file_name)) { - file_name=doc.get("file_name"); - } - String file_content=highlighter.getBestFragment(new SmartChineseAnalyzer(), "file_content",doc.get("file_content")); - if (StringUtils.isEmpty(file_content)) { - file_content=doc.get("file_content"); - } - json.put("file_name", file_name); - json.put("file_content", file_content); - jsonTotal.add(json); - } - - jsonReturn.put("totalCount", topDocs.totalHits.value); - } + // 鑾峰彇鏉冮檺杩囨护瀛楁 + String org_fields = fseConfig.getString(CmnConst.ORG_FIELDS); + String user_fields = fseConfig.getString(CmnConst.USER_FIELDS); - jsonReturn.put("data", jsonTotal); - return jsonReturn; - } - - /** - * 鑾峰彇鏂囨。妫�绱㈤厤缃俊鎭紙鏉冮檺锛屽姛鑳藉悕绉帮紝璺宠浆鎸夐挳锛� - * @param json - * @param function_uuid - * @param service_uuid - */ - public void getConfig(JSONObject json, String function_uuid, String uuid) { - - FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter("product_sys_document_search", "function_uuid=?", new Object[] {function_uuid}, false); - if (fseConfig!=null) { - - FieldSetEntity fseFunction=baseDao.getFieldSetEntity("product_sys_functions", function_uuid, false); - - //鑾峰彇鏉冮檺杩囨护瀛楁 - String org_fields=fseConfig.getString("org_fileds"); - String user_fields=fseConfig.getString("user_fileds"); - - //鑾峰彇闄勪欢淇℃伅 - FieldSetEntity fseAttachment=baseDao.getFieldSetEntity("product_sys_attachments", uuid, false); - baseDao.loadPromptData(fseAttachment); - if (fseAttachment!=null) { - StringBuilder filter=new StringBuilder(); - filter.append(fseAttachment.getString("attachment_data_field")); + // 鑾峰彇闄勪欢淇℃伅锛岀粨鍚堟暟鎹潈闄愬瓧娈碉紝鐢熸垚鍘熸暟鎹殑杩囨护SQL + FieldSetEntity fseAttachment = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); + if (fseAttachment != null) { + StringBuilder filter = new StringBuilder(); + filter.append(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_FIELD)); filter.append(" =? "); if (StringUtils.isEmpty(org_fields)) { if (!StringUtils.isEmpty(user_fields)) { filter.append(" AND "); - filter.append(permissionService.getDataFilter(fseAttachment.getString("attachment_data_table"), user_fields)); + filter.append(permissionService + .getDataFilter(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), user_fields)); } - }else { + } else { if (StringUtils.isEmpty(user_fields)) { filter.append(" AND "); filter.append(permissionService.getDataFilter(org_fields)); - }else { + } else { filter.append(" AND "); - filter.append(permissionService.getDataFilter(fseAttachment.getString("attachment_data_table"), user_fields, org_fields)); + filter.append(permissionService.getDataFilter( + fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), user_fields, org_fields)); } } - - - //鑾峰彇鍘熸暟鎹� - FieldSetEntity fseService=baseDao.getFieldSetEntityByFilter(fseAttachment.getString("attachment_data_table"), filter.toString(), new Object[] {uuid}, false); - if (fseService==null) {//鏉冮檺杩囨护鏃犲師鏁版嵁 - FieldSetEntity fseService2=baseDao.getFieldSetEntityByFilter(fseAttachment.getString("attachment_data_table"), fseAttachment.getString("attachment_data_field")+"=?", new Object[] {uuid}, false); - if (fseService2==null) { -// json.put("date_time", fseService2.getString(fseConfig.getString("time_field"))); - json.put("title", "鏃犵浉鍏充笟鍔℃暟鎹�"); - json.put("function_name", fseFunction.getString("function_name")); - json.put("is_perssion", 1); - }else { - json.put("service_uuid", fseService2.getString("uuid")); - json.put("date_time", fseService2.getString(fseConfig.getString("time_field"))); - json.put("title", fseService2.getString(fseConfig.getString("title_field"))); - json.put("function_name", fseFunction.getString("function_name")); - json.put("is_perssion", 0); + + // 鑾峰彇鍘熸暟鎹� + FieldSetEntity fseService = baseDao.getFieldSetEntityByFilter( + fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), filter.toString(), + new Object[] { uuid }, false); + if (fseService == null) { + // 鑾峰彇鏃犳潈闄愯繃婊ょ殑鍘熸暟鎹� + FieldSetEntity fseService2 = baseDao.getFieldSetEntityByFilter( + fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), + fseAttachment.getString(CmnConst.ATTACHMENT_DATA_FIELD) + "=?", new Object[] { uuid }, + false); + if (fseService2 == null) { + json.put("permission_msg", "鏁版嵁寮傚父-涓氬姟鏁版嵁涓嶅瓨鍦�"); + json.put(CmnConst.IS_PERSSION, 2); + } else { + json.put("permission_msg", "鏁版嵁绛栫暐-姝ゆ枃妗f偍娌℃湁鏌ョ湅鏉冮檺"); + json.put(CmnConst.IS_PERSSION, 3); } - - }else { - json.put("service_uuid", fseService.getString("uuid")); - json.put("date_time", fseService.getString(fseConfig.getString("time_field"))); - json.put("title", fseService.getString(fseConfig.getString("title_field"))); - json.put("function_name", fseFunction.getString("function_name")); + + } else { + + // 鍒ゆ柇鏄惁涓烘枃妗g鐞嗗姛鑳斤紝骞跺鐞嗗崟浣嶆枃妗e拰涓汉鏂囨。鏉冮檺 + if ("e4fa2c88-88a4-4ef4-9020-ebbe0440b4cf".equals(function_uuid)) { + if (!SpringMVCContextHolder.getCurrentUserId() + .equals(fseService.getString(CoreConst.CREATED_BY))) { + json.put("permission_msg", "鏂囨。鏉冮檺绛栫暐-姝ゆ枃妗f偍娌℃湁鏌ョ湅鏉冮檺"); + json.put(CmnConst.IS_PERSSION, 4); + } + } else if ("01513ff6-c758-4384-b861-e58dfe146fdd".equals(function_uuid)) { + // 鑾峰彇鍗曚綅鏂囨。鎵�灞炵洰褰曟潈闄� + FieldSetEntity fseDirectRight = baseDao.getFieldSetByFilter( + CmnConst.PRODUCT_OA_DIRECTORY_RIGHTS, "directory_uuid=?", + new Object[] { fseService.getString(CmnConst.DIRECTORY_UUID) }, false); + if (fseDirectRight == null) { + json.put("permission_msg", "鏂囨。鏉冮檺绛栫暐-姝ゆ枃妗f偍娌℃湁鏌ョ湅鏉冮檺"); + json.put(CmnConst.IS_PERSSION, 4); + } else { + boolean succ = BaseUtil.multipleTypeConcat(fseDirectRight.getString(CmnConst.STORAGE_UUID), + SpringMVCContextHolder.getCurrentUserId()); + if (!succ) { + json.put("permission_msg", "鏂囨。鏉冮檺绛栫暐-姝ゆ枃妗f偍娌℃湁鏌ョ湅鏉冮檺"); + json.put(CmnConst.IS_PERSSION, 4); + } + } + } } - json.put("created_by", fseAttachment.getString(CmnConst.CREATED_BY)); } } - } - - - /** - * 鏂囦欢妫�绱㈡煡璇㈠垎椤� - * @param cpage 褰撳墠椤� - * @param pagesize 姣忛〉澶у皬 - * @param searcher 鏌ヨ鍣� - * @param query 鏌ヨ瀵硅薄 - * @return - * @throws IOException - */ - private TopDocs searchByPage(int cpage,int pagesize, IndexSearcher searcher, Query query) throws IOException { + return json; + } + + /** + * 鏂囦欢妫�绱㈡煡璇㈠垎椤� + * + * @param cpage 褰撳墠椤� + * @param pagesize 姣忛〉澶у皬 + * @param searcher 鏌ヨ鍣� + * @param query 鏌ヨ瀵硅薄 + * @return + * @throws IOException + */ + private TopDocs searchByPage(int cpage, int pagesize, IndexSearcher searcher, Query query) throws IOException { TopDocs result = null; - if(query == null){ + if (query == null) { System.out.println(" Query is null return null "); return null; } ScoreDoc before = null; - if(cpage != 1){ - TopDocs docsBefore = searcher.search(query, (cpage-1)*pagesize); + if (cpage != 1) { + TopDocs docsBefore = searcher.search(query, (cpage - 1) * pagesize); ScoreDoc[] scoreDocs = docsBefore.scoreDocs; - if(scoreDocs.length > 0){ + if (scoreDocs.length > 0) { before = scoreDocs[scoreDocs.length - 1]; } } @@ -321,13 +448,14 @@ return result; } - /** - * 鏍规嵁闄勪欢uuid鍒犻櫎绱㈠紩 - * @param uuid - * @throws IOException - */ - public void deleteIndexByUUID(String uuid) throws IOException { - indexWriter.deleteDocuments(new Term("uuid",uuid)); - indexWriter.commit(); - } + /** + * 鏍规嵁闄勪欢uuid鍒犻櫎绱㈠紩 + * + * @param uuid + * @throws IOException + */ + public void deleteIndexByUUID(String uuid) throws IOException { + indexWriter.deleteDocuments(new Term("uuid", uuid)); + indexWriter.commit(); + } } -- Gitblit v1.9.2