Difference between revisions of "CSV importer"

From OpenKM Documentation
Jump to: navigation, search
m
Line 47: Line 47:
 
int notFound = 0;
 
int notFound = 0;
 
int noName = 0;
 
int noName = 0;
 +
 
for (Iterator it=data.listIterator(); it.hasNext();) {
 
for (Iterator it=data.listIterator(); it.hasNext();) {
 
         String[] row = (String[]) it.next();
 
         String[] row = (String[]) it.next();
 
         String docName = row[uniqueFileName];
 
         String docName = row[uniqueFileName];
     print(count+">>>> "+docName);
+
     print(count + ">>>> " + docName);
         if (docName!=null && !docName.equals("")) {
+
 
 +
         if (docName != null && !docName.equals("")) {
 
             QueryParams queryParams = new QueryParams();
 
             QueryParams queryParams = new QueryParams();
 
             queryParams.setDomain(QueryParams.DOCUMENT);
 
             queryParams.setDomain(QueryParams.DOCUMENT);
             queryParams.setName(docName+".*");
+
             queryParams.setName(docName + ".*");
 
             Collection results = OKMSearch.getInstance().find(null, queryParams);
 
             Collection results = OKMSearch.getInstance().find(null, queryParams);
             if (results.size()==1) {
+
 
 +
             if (results.size() == 1) {
 
                 QueryResult queryResult = (QueryResult) results.iterator().next();
 
                 QueryResult queryResult = (QueryResult) results.iterator().next();
 
                 if (queryResult.getDocument() != null) {
 
                 if (queryResult.getDocument() != null) {
Line 65: Line 68:
 
                     countNotDocument++;
 
                     countNotDocument++;
 
                 }
 
                 }
             } else if (results.size()>1) {
+
             } else if (results.size() > 1) {
 
                 print("error more than one document found can not decide");
 
                 print("error more than one document found can not decide");
 
                 moreThanOneDocumentFound++;
 
                 moreThanOneDocumentFound++;
Line 76: Line 79:
 
             noName++;
 
             noName++;
 
         }
 
         }
 +
 
         print("</br>");
 
         print("</br>");
 
          
 
          
         //FileLogger.info(FILE_LOG_NAME, "Document name ''{0}'' to ''{1}''",row[0],row[posDocRevNo]);
+
         //FileLogger.info(FILE_LOG_NAME, "Document name ''{0}'' to ''{1}''", row[0], row[posDocRevNo]);
 
         count++;
 
         count++;
 
}
 
}
  
print("Total:"+count+"</br>");
+
print("Total:" + count + "</br>");
print("Found:"+countFound+"</br>");
+
print("Found:" + countFound + "</br>");
print("Error not document:"+countNotDocument+"</br>");
+
print("Error not document:" + countNotDocument + "</br>");
print("Error more then one document found:"+moreThanOneDocumentFound+"</br>");
+
print("Error more then one document found:" + moreThanOneDocumentFound + "</br>");
print("Error not found:"+notFound+"</br>");
+
print("Error not found:" + notFound + "</br>");
print("Error name empty:"+notFound+"</br>");
+
print("Error name empty:" + notFound + "</br>");
 
</source>
 
</source>
  
 
[[Category: Utilities]]
 
[[Category: Utilities]]

Revision as of 15:15, 17 April 2013

These are administrator scripts ( can be used as crontab scripts also ) to import metadata values to OpenKM.


Nota advertencia.png Article under construction

Document exist verification

That script verifies that documents exists in repository:

import java.io.FileReader;
import java.io.Reader;
import java.util.Iterator;
import java.util.List;
import java.util.Collection;

import com.googlecode.jcsv.CSVStrategy;
import com.googlecode.jcsv.reader.CSVReader;
import com.googlecode.jcsv.reader.internal.CSVReaderBuilder;
import com.googlecode.jcsv.reader.internal.DefaultCSVEntryParser;

import com.openkm.dao.bean.QueryParams;
import com.openkm.bean.QueryResult;
import com.openkm.api.OKMSearch;
import com.openkm.util.FileLogger;

String FILE_LOG_NAME = "CSVLOG";
String META_PATH = "/home/openkm/csv/";
String META_FILE_NAME = "metadata.csv";
int uniqueFileName = 0;
int yearColumn = 2;

// Format defintion
char delimiter = ',';
char quoteCharacter = '"';
char commentIndicator = '#';
boolean skipHeader = true;
boolean ignoreEmptyLines = true;
CSVStrategy strategy = new CSVStrategy(delimiter, quoteCharacter, commentIndicator, skipHeader, ignoreEmptyLines);
// File reader
Reader reader = new FileReader(META_PATH + META_FILE_NAME);
// CSV reader		
CSVReader csvParser = new CSVReaderBuilder(reader).strategy(strategy).entryParser(new DefaultCSVEntryParser()).build();
List data = csvParser.readAll();
int count = 1;
int countFound = 0;
int countNotDocument = 0;
int moreThanOneDocumentFound = 0;
int notFound = 0;
int noName = 0;

for (Iterator it=data.listIterator(); it.hasNext();) {
        String[] row = (String[]) it.next();
        String docName = row[uniqueFileName];
    	print(count + ">>>> " + docName);

        if (docName != null && !docName.equals("")) {
            QueryParams queryParams = new QueryParams();
            queryParams.setDomain(QueryParams.DOCUMENT);
            queryParams.setName(docName + ".*");
            Collection results = OKMSearch.getInstance().find(null, queryParams);

            if (results.size() == 1) {
                QueryResult queryResult = (QueryResult) results.iterator().next();
                if (queryResult.getDocument() != null) {
                    print("found");
                    countFound++;
                } else {
                    print("error is not document");
                    countNotDocument++;
                }
            } else if (results.size() > 1) {
                print("error more than one document found can not decide");
                moreThanOneDocumentFound++;
            } else {
                print("not found");
                notFound++;
            }
        } else {
            print("error document has no name");
            noName++;
        }

        print("</br>");
        
        //FileLogger.info(FILE_LOG_NAME, "Document name ''{0}'' to ''{1}''", row[0], row[posDocRevNo]);
        count++;
}

print("Total:" + count + "</br>");
print("Found:" + countFound + "</br>");
print("Error not document:" + countNotDocument + "</br>");
print("Error more then one document found:" + moreThanOneDocumentFound + "</br>");
print("Error not found:" + notFound + "</br>");
print("Error name empty:" + notFound + "</br>");