@Transactional(propagation=Propagation.REQUIRED)
private void scanDirectories(final List<ScanContext> scanContexts) {
long changedCount = 0l;
boolean systemChanged = false;
final SystemDomain system = this.systemDao.getSystem(cfg.getUSN());
final Set<Object> itemsToPurge = new HashSet<Object>();
final Map<String, ScanContext> idScanContextMap = new HashMap<String, ScanContext>();
final Set<String> foundFileIds = new HashSet<String>();
final DidlChangeMap touchedDidlMap = new DidlChangeMap();
for (final ScanContext sc : scanContexts) {
idScanContextMap.put(sc.getScanFolderId(), sc);
}
// collect all Files, no Folders!
for (final ScanContext sc : scanContexts) {
logger.info("scanner [scan] "+sc.getScanFolder());
List<String> knownFileIds = this.fsDao.getAllFileIds(sc.getScanFolderId());
collectNewFiles(sc.getScanFolder(), sc.getFiles(), knownFileIds, foundFileIds);
logger.info("scanner [found files] " + sc.getFiles().size() + " " +sc.getScanFolder());
changedCount += sc.getFiles().size();
knownFileIds.removeAll(foundFileIds);
for (String removed : knownFileIds) {
sc.addDeletedMediaFile(removed);
changedCount += 1;
}
}
if (changedCount > 0) {
final List<String> allDidlIds= this.didlDao.getAllDidlIds();
for (String id : allDidlIds) {
touchedDidlMap.addDidl(id, null);
}
for (final ScanFolderDomain sfd : system.getScanFolder()) {
if (idScanContextMap.containsKey(sfd.getId())) {
final ScanContext sc = idScanContextMap.get(sfd.getId());
logger.info("scanner [filter] "+sc.getScanFolder());
for (final ScanFile f : sc.getFiles()) {
final FileDomain fd = new FileDomain(f.getId(), null, f.getFile());
if (this.didl.createDidl(fd, f, touchedDidlMap, sfd)) {
sc.getMediaFiles().add(fd);
}
}
logger.info("scanner [found new files] " + sc.getMediaFiles().size() + " " +sc.getScanFolder());
}
}
}
// update folders
for (final ScanFolderDomain sfd : system.getScanFolder()) {
if (idScanContextMap.containsKey(sfd.getId())) {
final ScanContext sc = idScanContextMap.get(sfd.getId());
boolean changedFiles = false;
for (final FileDomain fd : sc.getMediaFiles()) {
changedFiles |= sfd.addFile(fd);
logger.info("scanner [add file] " + fd.getName() + "\t\t(" + fd.getPath() + ")");
}
if (changedFiles || !sc.getDeletedMediaFiles().isEmpty()) {
changedFiles |= updateDidl(sfd.getDidlRoot(), sfd, touchedDidlMap,sc.getDeletedMediaFiles(), itemsToPurge);
}
// remove scanfolder mark
sfd.setScanState(ScanFolderState.IDLE);
sfd.setLastScan(new Date());
systemChanged |= changedFiles;
}
}
// something changed increase updateId
if (systemChanged) {
logger.info("scanner [collect new stats]");
system.increaseUpdateId();
system.setLastDataChange(new Date());
if (this.didl.foundUnsupportedFiles()) {
logger.info("scanner [dlna-unsupported] " + this.didl.getMissingClassTypeExtensions()+ ", " + this.didl.getMissingProtocolInfoExtensions());
}
}