Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.96 KB | None | 0 0
  1. package utils;
  2.  
  3. import com.filenet.api.core.Document;
  4. import ru.fns.common.helper.FileHelper;
  5. import ru.fns.common.util.Timer;
  6. import ru.fns.filenet.api.FileNet;
  7. import ru.fns.filenet.api.ObjectStore;
  8. import ru.fns.filenet.api.util.ConnectParams;
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. /**
  16.  * ========== ItCorp v. 1.0 class library ==========
  17.  * <p/>
  18.  * http://www.it.ru/
  19.  * <p/>
  20.  * &copy; Copyright 1990-2013, by ItCorp.
  21.  * <p/>
  22.  * ========== ChangeAttributes.java ==========
  23.  * <p/>
  24.  * $Revision:  $<br/>
  25.  * $Author:  $<br/>
  26.  * $HeadURL:  $<br/>
  27.  * $Id:  $
  28.  * <p/>
  29.  * 30.09.15 22:28: Original version (ASTarasov)<br/>
  30.  */
  31. public class FNS_5763_FindingUnuseFiles {
  32.     public static void main(String[] args) throws IOException {
  33.         if (args.length < 6) throw new IllegalArgumentException("FUCK!");
  34.  
  35.         //\\dpc.tax.nalog.ru\ROOT\GRs\gr216\TRANSPORT_FCOD\CPS\processing\N7700\
  36.         //\\dpc.tax.nalog.ru\ROOT\GRs\gr216\TRANSPORT_FCOD\CPS\INBOX\MANUAL_N7700
  37.  
  38.         final String transportCPSFolder = args[0];
  39.         final String ceUri = args[1];
  40.         final String user = args[2];
  41.         final String pass = args[3];
  42.         final String os = args[4];
  43.         final String type = args[5];
  44.  
  45.         if (!new File(transportCPSFolder).exists()) {
  46.             System.err.println("Failed to execute, cuz: transportCPSFolder is not exists");
  47.             return;
  48.         }
  49.  
  50.         final File processingFolder = new File(transportCPSFolder, "processing");
  51.         final File inboxFolder = new File(transportCPSFolder, "INBOX");
  52.  
  53.         System.out.println("Trying to scan processingFolder: '" + processingFolder.getAbsolutePath() + "'");
  54.         Timer t = Timer.registerStart();
  55.         final File[] cpsFolders = processingFolder.listFiles();
  56.         long totalTime = t.registerStop();
  57.         if (cpsFolders == null) {
  58.             System.err.println("Failed to scan processing folder...");
  59.             return;
  60.         }
  61.         System.out.println("We've found '" + cpsFolders.length + "' cps folders.. It took '" + totalTime + "'ms");
  62.  
  63.         for (final File cpsFolder : cpsFolders) {
  64.             final String cps = cpsFolder.getName();
  65.             final File manualFolder = new File(inboxFolder, "MANUAL_" + cps);
  66.             final List<File> filesToBeMoved = new ArrayList<File>();
  67.             System.out.println("Will be work by '" + cps + "' cps folder: '" + cpsFolder.getAbsolutePath() + "'");
  68.             t = Timer.registerStart();
  69.  
  70.             System.out.println("Trying to scan cpsFolder: '" + cpsFolder.getAbsolutePath() + "'");
  71.             Timer ct = Timer.registerStart();
  72.             final File[] uuidFolders = cpsFolder.listFiles();
  73.             if (uuidFolders == null) {
  74.                 System.err.println("Failed to scan cps folder...");
  75.                 return;
  76.             }
  77.             totalTime = ct.registerStop();
  78.             System.out.println("We've found '" + uuidFolders.length + "' uuid folders.. It took '" + totalTime + "'ms");
  79.  
  80.             ConnectParams params = new ConnectParams(ceUri, user, pass, os, type);
  81.             try {
  82.                 FileNet.connect(params, false, new FileNet.FileNetClosure() {
  83.                     @Override
  84.                     public void closure(FileNet fileNet) throws Exception {
  85.                         fileNet.useObjectStore(os, new FileNet.ObjectStoreClosure() {
  86.                             @Override
  87.                             public void closure(ObjectStore objectStore) throws Exception {
  88.                                 int processed = 0;
  89.                                 for (File uuidFolder : uuidFolders) {
  90.                                     processed++;
  91.                                     if (!uuidFolder.isDirectory()) continue;
  92.                                     File[] files = uuidFolder.listFiles();
  93.                                     if (files == null) continue;
  94.                                     if (files.length == 0) {
  95.                                         System.out.println("One of uuid folder: '" + uuidFolder.getAbsolutePath() + "' is an empty. We'll remove it.");
  96.                                         FileHelper.deleteFileSilent(uuidFolder);
  97.                                         continue;
  98.                                     }
  99.                                     for (File file : files) {
  100.                                         String fileName = file.getName();
  101.                                         if (!fileName.startsWith("16")) continue;
  102.                                         List<Document> documents = objectStore.searchObjects("select id from TechInboxDocument where urlPath LIKE '%" + fileName + "'");
  103.                                         if (documents.isEmpty()) {
  104.                                             //повторная проверка на доступность, на случай если джоб отработал и уже удалил файлик.
  105.                                             if (file.exists()) {
  106.                                                 filesToBeMoved.add(file);
  107.                                                 System.out.println("#" + processed + "/" + uuidFolders.length + "\tWe've found missing file: '" + file.getAbsolutePath() + "'. It will be moved to manual folder");
  108.                                             }
  109.                                         }
  110.                                     }
  111.  
  112.                                 }
  113.                             }
  114.                         });
  115.                     }
  116.                 });
  117.             } catch (Exception e) {
  118.                 e.printStackTrace();
  119.             }
  120.             if (!filesToBeMoved.isEmpty()) {
  121.                 System.out.println("Trying to move '" + filesToBeMoved.size() + "' files to manual folder: '" + manualFolder.getAbsolutePath() + "'");
  122.                 Timer mt = Timer.registerStart();
  123.                 for (File fileToBeMoved : filesToBeMoved) {
  124.                     File parent = fileToBeMoved.getParentFile();
  125.                     File newFilePath = new File(manualFolder, fileToBeMoved.getName());
  126.                     FileHelper.copyFile(fileToBeMoved, newFilePath);
  127.                     FileHelper.deleteFileSilent(fileToBeMoved);
  128.                     System.out.println("File: '" + fileToBeMoved.getAbsolutePath() + "' has moved to '" + newFilePath.getAbsolutePath() + "'");
  129.                     if (parent.list().length == 0) {
  130.                         FileHelper.deleteFileSilent(parent);
  131.                         System.out.println("Removed folder: '" + parent.getAbsolutePath() + "' also");
  132.                     }
  133.                 }
  134.                 totalTime = mt.registerStop();
  135.                 System.out.println("File moving took '" + totalTime + "'ms");
  136.             }
  137.  
  138.             totalTime = t.registerStop();
  139.             System.out.println("CPS folder has been processed. It took '" + totalTime + "'ms");
  140.         }
  141.  
  142.         System.out.println("DONE!");
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement