| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
package com.buckosoft.PicMan.business; |
| 9 | |
|
| 10 | |
import java.awt.Dimension; |
| 11 | |
import java.io.File; |
| 12 | |
import java.util.ArrayList; |
| 13 | |
import java.util.Date; |
| 14 | |
import java.util.HashMap; |
| 15 | |
import java.util.Iterator; |
| 16 | |
import java.util.LinkedList; |
| 17 | |
import java.util.List; |
| 18 | |
|
| 19 | |
import org.apache.commons.logging.Log; |
| 20 | |
import org.apache.commons.logging.LogFactory; |
| 21 | |
|
| 22 | |
import com.buckosoft.PicMan.business.util.MD5SumUpdater; |
| 23 | |
import com.buckosoft.PicMan.domain.Pic; |
| 24 | |
import com.buckosoft.PicMan.domain.Root; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public class NewPicScannerImpl implements NewPicScanner { |
| 33 | |
|
| 34 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 35 | |
private static final boolean DEBUG = false; |
| 36 | |
|
| 37 | |
private LinkedList<String> picExtensions; |
| 38 | |
private Root currentRoot; |
| 39 | |
private ArrayList<File> newPics; |
| 40 | |
private PicManFacade pmf; |
| 41 | 0 | private String processErrorMessage = ""; |
| 42 | |
private HashMap<String, Date> existingPicsMap; |
| 43 | |
|
| 44 | 0 | public NewPicScannerImpl(){ |
| 45 | 0 | newPics = new ArrayList<File>(); |
| 46 | 0 | } |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | public NewPicScannerImpl(PicManFacade pmf) { |
| 52 | 0 | newPics = new ArrayList<File>(); |
| 53 | 0 | this.pmf = pmf; |
| 54 | 0 | setPicExtensions(pmf.getDB().getPicExtensions()); |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 0 | public void setPicExtensions(List<String> list) {picExtensions = new LinkedList<String>(list); } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | 0 | public ArrayList<File> getNewPics() { return(newPics); } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public int getNewPicCount() throws Exception { |
| 70 | |
if (DEBUG) |
| 71 | |
logger.info("getNewPicCount()"); |
| 72 | |
|
| 73 | 0 | int newCount = 0; |
| 74 | 0 | newPics.clear(); |
| 75 | |
Iterator<Root> it; |
| 76 | |
|
| 77 | 0 | existingPicsMap = pmf.getDB().getPicsMap(); |
| 78 | |
if (DEBUG) |
| 79 | |
logger.info("existingPicsMap= " + existingPicsMap.size()); |
| 80 | 0 | List<Root> roots = pmf.getDB().getRoots(); |
| 81 | 0 | it = roots.iterator(); |
| 82 | 0 | while(it.hasNext()) { |
| 83 | 0 | currentRoot = (Root)it.next(); |
| 84 | 0 | if (currentRoot.isActive() == false) |
| 85 | 0 | continue; |
| 86 | 0 | File f = new File(currentRoot.getPath()); |
| 87 | 0 | newCount = processFile(f, newCount); |
| 88 | 0 | } |
| 89 | 0 | existingPicsMap = null; |
| 90 | 0 | return(newCount); |
| 91 | |
} |
| 92 | |
|
| 93 | |
private int processFile(File f, int newCount) throws Exception |
| 94 | |
{ |
| 95 | 0 | if (f.isFile()) { |
| 96 | |
|
| 97 | |
|
| 98 | 0 | String s = f.getName(); |
| 99 | 0 | Iterator<String> it = picExtensions.iterator(); |
| 100 | 0 | boolean match = false; |
| 101 | 0 | while (it.hasNext()) { |
| 102 | 0 | if (s.endsWith((String)it.next())) { |
| 103 | 0 | match = true; |
| 104 | 0 | break; |
| 105 | |
} |
| 106 | |
} |
| 107 | 0 | if (match) { |
| 108 | 0 | if (picExists(f)) |
| 109 | 0 | return(newCount); |
| 110 | 0 | newPics.add(f); |
| 111 | 0 | newCount++; |
| 112 | |
} |
| 113 | 0 | return(newCount); |
| 114 | |
} |
| 115 | 0 | if (f.isDirectory()) { |
| 116 | 0 | File[] files = f.listFiles(); |
| 117 | 0 | for (int i=0; i<files.length; i++) |
| 118 | 0 | newCount = processFile(files[i], newCount); |
| 119 | 0 | return(newCount); |
| 120 | |
} |
| 121 | 0 | throw new Exception("file '" + f.getName() + "' is neither file nor directory"); |
| 122 | |
} |
| 123 | |
|
| 124 | |
private boolean picExists(File f) { |
| 125 | 0 | String s = f.getName(); |
| 126 | 0 | if (s.endsWith(".jpg")) { |
| 127 | 0 | s = s.substring(0,s.length()-4); |
| 128 | |
|
| 129 | 0 | if (existingPicsMap.containsKey(s)) |
| 130 | 0 | return(true); |
| 131 | |
} |
| 132 | 0 | return(false); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
public boolean processNewPics() { |
| 140 | 0 | processErrorMessage = "Broken!"; |
| 141 | 0 | Iterator<File> it = newPics.iterator(); |
| 142 | 0 | boolean success = true; |
| 143 | 0 | List<Root> roots = this.pmf.getDB().getRoots(); |
| 144 | |
File f; |
| 145 | |
String s; |
| 146 | |
Pic pic; |
| 147 | 0 | while (it.hasNext()) { |
| 148 | 0 | f = (File)it.next(); |
| 149 | 0 | pic = new Pic(); |
| 150 | 0 | s = f.getName(); |
| 151 | 0 | if (s.endsWith(".jpg")) |
| 152 | 0 | s = s.substring(0, s.length()-4); |
| 153 | 0 | pic.setName(s); |
| 154 | 0 | String path = f.getParent().replace('\\', '/'); |
| 155 | 0 | Iterator<Root> riter = roots.iterator(); |
| 156 | 0 | while (riter.hasNext()) { |
| 157 | 0 | Root root = riter.next(); |
| 158 | 0 | if (!root.isActive()) |
| 159 | 0 | continue; |
| 160 | 0 | if (path.startsWith(root.getPath())) { |
| 161 | 0 | pic.setRid(root.getRid()); |
| 162 | 0 | path = path.substring(root.getPath().length()+1); |
| 163 | 0 | break; |
| 164 | |
} |
| 165 | 0 | } |
| 166 | 0 | if (pic.getRid() == -1) { |
| 167 | 0 | throw new RuntimeException("Can't match root for '" + pic.getName() + "' - '" + path + "'"); |
| 168 | |
} |
| 169 | |
|
| 170 | 0 | pic.setLocation(path); |
| 171 | 0 | pic.getDate().setTime(f.lastModified()); |
| 172 | |
|
| 173 | 0 | Dimension d = pmf.determinePicSize(pic); |
| 174 | 0 | if (d.height == -1) { |
| 175 | 0 | logger.warn("Can't determine size for pic: " + pic.getName()); |
| 176 | 0 | d.height = 0; |
| 177 | 0 | d.width = 0; |
| 178 | |
} |
| 179 | 0 | pic.setHeight(d.height); |
| 180 | 0 | pic.setWidth(d.width); |
| 181 | |
|
| 182 | |
|
| 183 | 0 | MD5SumUpdater.calculateMD5Sum(pmf, pic); |
| 184 | 0 | pmf.getDB().addPic(pic); |
| 185 | 0 | pmf.getDB().addVirgin(pic.getName()); |
| 186 | 0 | } |
| 187 | 0 | return(success); |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
public String getProcessNewPicsErrorMessage() { |
| 194 | 0 | return(processErrorMessage); |
| 195 | |
} |
| 196 | |
} |