framework.retrieval.engine.index.all.file.impl.DefaultRFileIndexAll.java Source code

Java tutorial

Introduction

Here is the source code for framework.retrieval.engine.index.all.file.impl.DefaultRFileIndexAll.java

Source

/**
 * Copyright 2010 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package framework.retrieval.engine.index.all.file.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.lucene.index.IndexWriter;

import framework.base.snoic.base.util.StringClass;
import framework.base.snoic.base.util.UtilTool;
import framework.base.snoic.base.util.file.FileHelper;
import framework.retrieval.engine.RetrievalType;
import framework.retrieval.engine.common.RetrievalUtil;
import framework.retrieval.engine.facade.IRDocOperatorFacade;
import framework.retrieval.engine.facade.IRQueryFacade;
import framework.retrieval.engine.index.all.RetrievalIndexAllException;
import framework.retrieval.engine.index.all.file.IIndexAllFileInterceptor;
import framework.retrieval.engine.index.all.file.IRFileIndexAll;
import framework.retrieval.engine.index.doc.file.FileIndexDocument;
import framework.retrieval.engine.index.doc.file.RFileIndexAllItem;
import framework.retrieval.engine.index.doc.internal.RDocItem;

/**
 * ?
 * @author 
 *
 */
public class DefaultRFileIndexAll implements IRFileIndexAll {
    private Log log = RetrievalUtil.getLog(this.getClass());

    private static FileHelper FileHelper = new FileHelper();
    private IRQueryFacade queryFacade = null;
    private IRDocOperatorFacade docOperatorFacade = null;
    private RFileIndexAllItem fileIndexAllItem = null;
    private long indexCount = 0;

    /**
     * ??
     * @return
     */
    public RFileIndexAllItem getFileIndexAllItem() {
        return fileIndexAllItem;
    }

    /**
     * ?
     * @param fileIndexAllItem
     */
    public void setFileIndexAllItem(RFileIndexAllItem fileIndexAllItem) {
        if (fileIndexAllItem.getIndexPathType() == null) {
            throw new RetrievalIndexAllException(
                    "RFileIndexAllItem  indexPathType  ?????");
        }
        if (fileIndexAllItem.getIndexOperatorType() == null) {
            throw new RetrievalIndexAllException(
                    "RFileIndexAllItem  RetrievalType.RIndexOperatorType  ?????");
        }
        this.fileIndexAllItem = fileIndexAllItem;
        this.queryFacade = fileIndexAllItem.getQueryFacade();
        this.docOperatorFacade = fileIndexAllItem.getDocOperatorFacade();
    }

    /**
     * 
     */
    public long indexAll() {

        List<FileIndexDocument> allFileIndexDocumentList = new ArrayList<FileIndexDocument>();

        indexAll(fileIndexAllItem.getFileBasePath(), allFileIndexDocumentList);

        createFileIndex(allFileIndexDocumentList);

        return indexCount;

    }

    /**
     * ?
     * @param filePath
     * @param allFileIndexDocumentList
     */
    private void indexAll(String filePath, List<FileIndexDocument> allFileIndexDocumentList) {

        if (!FileHelper.isDir(filePath)) {

            if (allFileIndexDocumentList != null && allFileIndexDocumentList.size() > 0) {
                createFileIndex(allFileIndexDocumentList);
            }

            return;
        }

        String[] fileList = new File(filePath).list();

        if (fileList != null && fileList.length > 0) {
            int length = fileList.length;

            for (int i = 0; i < length; i++) {
                String fileName = StringClass.getFormatPath(filePath + "/" + fileList[i]);

                if (FileHelper.isDir(fileName)) {
                    if (fileIndexAllItem.isIncludeSubDir()) {
                        indexAll(fileName, allFileIndexDocumentList);
                    }
                } else if (FileHelper.isFile(fileName)) {

                    String fileType = RetrievalUtil.getFileType(fileName);

                    if (fileType.equals("")) {
                        continue;
                    }

                    if (fileIndexAllItem.getFileTypes() != null && fileIndexAllItem.getFileTypes().size() > 0
                            && !fileIndexAllItem.getFileTypes().contains(fileType)) {
                        continue;
                    }

                    File file = new File(fileName);

                    FileIndexDocument fileIndexDocument = new FileIndexDocument(
                            fileIndexAllItem.isFullContentFlag(), fileIndexAllItem.getCharsetName());

                    String absolutePath = StringClass.getFormatPath(file.getAbsolutePath());
                    String basePath = StringClass.getFormatPath(fileIndexAllItem.getFileBasePath());
                    String relativizePath = StringClass.getReplaceString(absolutePath, basePath, "");
                    relativizePath = StringClass.getFormatPath("/" + relativizePath);

                    fileIndexDocument.setFile(file);
                    fileIndexDocument.setFileBasePath(basePath);
                    fileIndexDocument.setFileId(relativizePath);

                    allFileIndexDocumentList.add(fileIndexDocument);

                }

                if (allFileIndexDocumentList.size() >= fileIndexAllItem.getPageSize()) {
                    createFileIndex(allFileIndexDocumentList);
                }

            }
        }

        if (allFileIndexDocumentList.size() >= fileIndexAllItem.getPageSize()) {
            createFileIndex(allFileIndexDocumentList);
        }

    }

    /**
     * ?
     * @param list
     */
    private void createFileIndex(List<FileIndexDocument> list) {
        int length = list.size();

        indexCount += length;

        boolean debugLogFlag = false;
        if (log.isDebugEnabled()) {
            debugLogFlag = true;
        }

        List<String> deleteFileDocumentIdList = new ArrayList<String>();

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            FileIndexDocument fileIndexDocument = list.get(i);

            fileIndexDocument.setIndexInfoType(fileIndexAllItem.getIndexInfoType());
            fileIndexDocument.setIndexPathType(fileIndexAllItem.getIndexPathType());

            //?
            IIndexAllFileInterceptor indexAllFileInterceptor = fileIndexAllItem.getIndexAllFileInterceptor();

            if (indexAllFileInterceptor != null) {
                Map<String, Object> addonFileDocuments = indexAllFileInterceptor.interceptor(fileIndexDocument);
                Map<String, Object> addonFileDocumentsType = indexAllFileInterceptor.getFieldsType();
                if (addonFileDocuments != null && addonFileDocuments.size() > 0) {
                    Object[][] objects = UtilTool.getMapKeyValue(addonFileDocuments);
                    int objectlength = objects.length;
                    for (int j = 0; j < objectlength; j++) {
                        String fieldName = String.valueOf(objects[j][0]);
                        String fieldValue = String.valueOf(objects[j][1]);
                        String type = "";

                        if (addonFileDocumentsType != null) {
                            type = String.valueOf(addonFileDocumentsType.get(fieldName));
                        }

                        RDocItem docItem = new RDocItem();
                        docItem.setName(fieldName);
                        docItem.setContent(fieldValue);

                        if (!type.equals("")) {
                            if (type.equalsIgnoreCase(String.valueOf(RetrievalType.RDocItemType.KEYWORD))) {
                                fileIndexDocument.addKeyWord(docItem);
                            } else if (type.equalsIgnoreCase(String.valueOf(RetrievalType.RDocItemType.DATE))) {
                                fileIndexDocument.addDateProperty(docItem);
                            } else if (type.equalsIgnoreCase(String.valueOf(RetrievalType.RDocItemType.NUMBER))) {
                                fileIndexDocument.addNumberProperty(docItem);
                            } else if (type.equalsIgnoreCase(String.valueOf(RetrievalType.RDocItemType.PROPERTY))) {
                                fileIndexDocument.addContent(docItem);
                            } else {
                                fileIndexDocument.addContent(docItem);
                            }
                        } else {
                            fileIndexDocument.addContent(docItem);
                        }
                    }
                }
            }

            //???
            if (fileIndexAllItem.getIndexOperatorType() == RetrievalType.RIndexOperatorType.UPDATE) {

                if (debugLogFlag) {
                    stringBuilder.append("?[" + fileIndexDocument.getIndexPathType() + "]:"
                            + fileIndexDocument.getFile().getAbsolutePath() + "\n");
                }

                String existsFileDocumentId = StringClass.getString(queryFacade.queryFileDocumentIndexIdByFileId(
                        fileIndexAllItem.getIndexPathType(), fileIndexDocument.getFileId()));
                if (existsFileDocumentId.equals("")) {
                    list.add(fileIndexDocument);
                } else {
                    fileIndexDocument.setId(existsFileDocumentId);
                    list.add(fileIndexDocument);

                    deleteFileDocumentIdList.add(existsFileDocumentId);
                }
            } else {
                if (debugLogFlag) {
                    stringBuilder.append("?[" + fileIndexDocument.getIndexPathType() + "]:"
                            + fileIndexDocument.getFile().getAbsolutePath() + "\n");
                }
            }
        }

        log.debug(stringBuilder);

        if (deleteFileDocumentIdList != null && deleteFileDocumentIdList.size() > 0) {
            docOperatorFacade.delete(fileIndexAllItem.getIndexPathType(), deleteFileDocumentIdList);
        }

        Iterator<FileIndexDocument> it = list.iterator();
        List<FileIndexDocument> theList = new ArrayList<FileIndexDocument>();
        while (it.hasNext()) {
            theList.add(it.next());
            it.remove();
            if (theList.size() >= fileIndexAllItem.getPageSize()) {
                docOperatorFacade.createFileIndexs(theList, fileIndexAllItem.getMaxIndexFileSize());
                theList.clear();
            }
        }
        if (theList.size() > 0) {
            docOperatorFacade.createFileIndexs(theList, fileIndexAllItem.getMaxIndexFileSize());
            theList.clear();
        }
        theList = null;
        list.clear();
    }

    @Override
    public long indexAll(IndexWriter indexWriter) {
        return 0;
    }
}