Skip to content

Commit

Permalink
#37 pictureFolder implementation - saving during import
Browse files Browse the repository at this point in the history
  • Loading branch information
spuliaiev-sfdc committed Aug 3, 2018
1 parent ce54620 commit e23de2c
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 29 deletions.
12 changes: 2 additions & 10 deletions core/beans/src/main/java/gallerymine/model/PictureFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ public class PictureFolder {
long filesCount;
long foldersCount;

/** Current folder path with name */
private String path;

/** Current folder path with name, lowercased to support all filesystems */
/** Current folder path with name, in lower case to support all filesystems */
@Indexed(unique = true)
private String pathl;
private String fullPath;

private List<String> notes = new ArrayList<>();

Expand Down Expand Up @@ -70,9 +67,4 @@ public void setName(String name) {
namel = name == null ? null : name.toLowerCase().replaceAll("^[_$/\\\\]*", "");
}

public void setPath(String path) {
this.path = path;
pathl = path == null ? null : path.toLowerCase();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gallerymine.model.mvc;

import com.fasterxml.jackson.annotation.JsonIgnore;
import gallerymine.model.support.InfoStatus;
import lombok.Data;
import org.joda.time.DateTime;
Expand All @@ -17,6 +18,7 @@ public class FileCriteria {
private DateTime fromDate;
private DateTime toDate;
private String path;
private String folderId;
private String fileName;
private Long fileSize;
private DateTime timestamp;
Expand All @@ -30,7 +32,8 @@ public class FileCriteria {
private Set<String> populatedNotBy;
private InfoStatus status;

private PageRequest pager = null;
@JsonIgnore
private PageRequest pager;

public FileCriteria(){
pager = new PageRequest(0,5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.WriteResult;
import gallerymine.model.ImportSource;
import gallerymine.model.PictureFolder;
import gallerymine.model.PictureInformation;
import gallerymine.model.importer.ImportRequest;
import gallerymine.model.mvc.FolderStats;
Expand All @@ -25,6 +26,8 @@
import java.util.ArrayList;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
* Source repository with custom methods
* Created by sergii_puliaiev on 6/19/17.
Expand All @@ -39,6 +42,9 @@ public class ImportSourceRepositoryImpl<Information extends PictureInformation>
@Autowired
private ObjectMapper jacksonObjectMapper;

@Autowired
private PictureFolderRepository pictureFolderRepository;

@Override
public List<PictureInformation> findInfo(String id) {
List<PictureInformation> infos = new ArrayList<>();
Expand Down Expand Up @@ -103,6 +109,20 @@ public Information saveByGrade(Information entity) {
protected List<Criteria> applyCustomCriteria(SourceCriteria searchCriteria) {
List<Criteria> criteria = super.applyCustomCriteria(searchCriteria);

if (searchCriteria.getFolderId() != null) {
if (searchCriteria.getFolderId().isEmpty()) {
PictureFolder rootPicFolder = pictureFolderRepository.findByFullPath("");
if (rootPicFolder != null) {
criteria.add(Criteria.where("folderId").is(rootPicFolder.getId()));
} else {
log.warn("Request is for root gallery picFolder, but it is not found!");
criteria.add(Criteria.where("folderId").is("NOT_FOUND"));
}
} else {
criteria.add(Criteria.where("folderId").is(searchCriteria.getFolderId()));
}
}

if (searchCriteria.getLatitude() != null && searchCriteria.getLongitude() != null &&
searchCriteria.getDistance() != null) {
Distance dist = DistanceFormatter.INSTANCE.convert(searchCriteria.getDistance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public interface PictureFolderRepository extends MongoRepository<PictureFolder,
Collection<PictureFolder> findByName(@Param("name") String name);
Collection<PictureFolder> findByNamel(@Param("namel") String namel);

PictureFolder findByPath(@Param("path") String path);
PictureFolder findByPathl(@Param("pathl") String pathl);
PictureFolder findByFullPath(@Param("fullPath") String path);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package gallerymine.backend.beans.repository;

import gallerymine.model.PictureFolder;
import gallerymine.model.mvc.FileCriteria;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Repository;

@Repository
Expand All @@ -24,4 +27,7 @@ public interface PictureFolderRepositoryCustom {
long incrementFilesCount(String picFolderId);
long incrementFoldersCount(String picFolderId);

PictureFolder getRootFolder();

Page<PictureFolder> fetchCustom(FileCriteria criteria);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package gallerymine.backend.beans.repository;

import gallerymine.backend.data.RetryVersion;
import gallerymine.backend.utils.RegExpHelper;
import gallerymine.model.PictureFolder;
import gallerymine.model.mvc.FileCriteria;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.springframework.data.domain.Sort.Direction.ASC;

/**
* Source repository with custom methods
* Created by sergii_puliaiev on 6/19/17.
* Created by sergii_puliaiev on 8/01/17.
*/
@Repository
public class PictureFolderRepositoryImpl implements PictureFolderRepositoryCustom {
Expand All @@ -26,7 +41,7 @@ public long incrementFilesCount(String picFolderId) {
PictureFolder picFolder = template.findById(picFolderId, PictureFolder.class);
picFolder.setFilesCount(picFolder.getFilesCount()+1);
template.save(picFolder);
log.info(" incFiles for folder new={} path={}", picFolder.getFilesCount(), picFolder.getPath());
log.info(" incFiles for folder new={} path={}", picFolder.getFilesCount(), picFolder.getFullPath());
return picFolder.getFilesCount();
}

Expand All @@ -36,8 +51,83 @@ public long incrementFoldersCount(String picFolderId) {
PictureFolder picFolder = template.findById(picFolderId, PictureFolder.class);
picFolder.setFoldersCount(picFolder.getFoldersCount()+1);
template.save(picFolder);
log.info(" incFolders for folder new={} path={}", picFolder.getFilesCount(), picFolder.getPath());
log.info(" incFolders for folder new={} path={}", picFolder.getFilesCount(), picFolder.getFullPath());
return picFolder.getFoldersCount();
}

protected List<Criteria> applyCustomCriteria(FileCriteria searchCriteria) {
List<Criteria> criteria = new ArrayList<>();

if (isNotBlank(searchCriteria.getFileName())) {
Criteria byFileName;
if (RegExpHelper.isMask(searchCriteria.getFileName())) {
byFileName = Criteria.where("namel").regex(RegExpHelper.convertToRegExp(searchCriteria.getFileName()));
} else {
byFileName = Criteria.where("namel").is(searchCriteria.getFileName());
}
Criteria byOrgFileName;
if (RegExpHelper.isMask(searchCriteria.getFileName())) {
byOrgFileName = Criteria.where("name").regex(RegExpHelper.convertToRegExp(searchCriteria.getFileName()));
} else {
byOrgFileName = Criteria.where("name").is(searchCriteria.getFileName());
}
criteria.add(new Criteria().orOperator(
byFileName,
byOrgFileName
));
}
if (searchCriteria.getPath() != null) {
if (RegExpHelper.isMask(searchCriteria.getPath())) {
criteria.add(Criteria.where("fullPath").regex(RegExpHelper.convertToRegExp(searchCriteria.getPath())));
} else {
criteria.add(Criteria.where("fullPath").is(searchCriteria.getPath()));
}
}

if (searchCriteria.getFolderId() != null) {
if (searchCriteria.getFolderId().isEmpty()) {
PictureFolder rootPicFolder = getRootFolder();
if (rootPicFolder != null) {
criteria.add(Criteria.where("folderId").is(rootPicFolder.getId()));
} else {
log.warn("Request is for root gallery picFolder, but it is not found!");
criteria.add(Criteria.where("folderId").is("NOT_FOUND"));
}
} else {
criteria.add(Criteria.where("folderId").is(searchCriteria.getFolderId()));
}
}

return criteria;
}

@Override
public PictureFolder getRootFolder() {
Criteria criteria = Criteria.where("folderId").is("");
Query query = criteria != null ? Query.query(criteria) : new Query();
return template.findOne(query, PictureFolder.class);
}

@Override
public Page<PictureFolder> fetchCustom(FileCriteria searchCriteria) {
List<Criteria> criteriaList = applyCustomCriteria(searchCriteria);
Criteria criteria = criteriaList.size() == 0 ? null : new Criteria().andOperator(criteriaList.toArray(new Criteria[0]));
Query query = criteria != null ? Query.query(criteria) : new Query();
query.skip(searchCriteria.getOffset())
.limit(searchCriteria.getSize());
if (StringUtils.isNotBlank(searchCriteria.getSortByField())) {
Sort.Direction direction = (searchCriteria.getSortDescending()!=null && searchCriteria.getSortDescending()) ?
Sort.Direction.DESC : ASC;
query.with(new Sort(direction, searchCriteria.getSortByField()));
}

long count = template.count(query, PictureFolder.class);

Iterator<PictureFolder> sources = template.stream(query, PictureFolder.class);

List<PictureFolder> sourcesList = new ArrayList<>();
sources.forEachRemaining( sourcesList::add );

return new PageImpl<PictureFolder>(sourcesList, searchCriteria.getPager(), count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,22 +400,22 @@ public Path indexateFileIfNeeded(Path file) {
public PictureFolder getOrCreatePictureFolder(Path folder) throws ImportFailedException {
try {
if (folder == null) {
PictureFolder picFolder = pictureFolderRepository.findByPathl("");
PictureFolder picFolder = pictureFolderRepository.findByFullPath("");
if (picFolder == null) {
picFolder = new PictureFolder();
picFolder.setName("");
picFolder.setPath("");
picFolder.setFullPath("");
pictureFolderRepository.save(picFolder);
}
return picFolder;
}
String folderRelPath = folder.toString().toLowerCase();
PictureFolder picFolder = pictureFolderRepository.findByPathl(folderRelPath);
PictureFolder picFolder = pictureFolderRepository.findByFullPath(folderRelPath.toLowerCase());

if (picFolder == null) {
picFolder = new PictureFolder();
picFolder.setName(folder.toFile().getName());
picFolder.setPath(folder.toString());
picFolder.setFullPath(folder.toString().toLowerCase());

PictureFolder picFolderParent = getOrCreatePictureFolder(folder.getParent());
picFolder.setParentId(picFolderParent.getId());
Expand Down Expand Up @@ -446,7 +446,7 @@ public PictureInformation settlePicture(PictureInformation source) throws ImageA
galleryImagePath.getParent().toFile().mkdirs();

FileUtils.copyFile(importImage.toFile(), galleryImagePath.toFile(), true);
PictureFolder picFolder = getOrCreatePictureFolder(relativePathWithFileLowered.getParent());
PictureFolder picFolder = getOrCreatePictureFolder(relativePathWithFile.getParent());

Picture picture = uniSourceService.retrySave(source.getId(), Picture.class, pic -> {
if (pic == null) {
Expand Down
8 changes: 7 additions & 1 deletion ui/web/src/main/java/gallerymine/GalleryMineApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableScheduling
Expand All @@ -49,7 +50,12 @@ public class GalleryMineApplication implements CommandLineRunner {
@Autowired
private CustomerRepository repository;

@Bean
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

@Bean
public TaskExecutor taskExecutor() {

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import gallerymine.backend.beans.AppConfig;
import gallerymine.backend.beans.repository.ImportSourceRepository;
import gallerymine.backend.beans.repository.PictureFolderRepository;
import gallerymine.backend.beans.repository.PictureRepository;
import gallerymine.backend.beans.repository.SourceRepository;
import gallerymine.backend.matchers.SourceFilesMatcher;
import gallerymine.backend.services.ImportService;
import gallerymine.model.Picture;
import gallerymine.model.PictureFolder;
import gallerymine.model.PictureInformation;
import gallerymine.model.importer.ActionRequest;
import gallerymine.model.Source;
Expand Down Expand Up @@ -63,6 +65,9 @@ public class SourcesController {
@Autowired
private ImportSourceRepository uniSourceRepository;

@Autowired
private PictureFolderRepository pictureFolderRepository;

@Autowired
private ImportService importService;

Expand Down Expand Up @@ -174,6 +179,18 @@ public Object listPath(@RequestBody SourceCriteria criteria) {
.build();
}

@PostMapping("listFolders")
@ResponseBody
public Object listFolders(@RequestBody SourceCriteria criteria) {
Page<PictureFolder> sourcePaths = pictureFolderRepository.fetchCustom(criteria);

return responseOk()
.results(sourcePaths)
.put("op", "listFolders")
.put("criteria", criteria)
.build();
}

@PostMapping("findDates")
@ResponseBody
public Object listDates(@RequestBody SourceCriteria criteria) {
Expand Down
2 changes: 1 addition & 1 deletion ui/web/src/main/resources/static/js/cmp/TreePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var TreePath = {
dataType: "json",
contentType: "application/json",
"url" : function (node, cb, par2) {
return "/sources/findPath";
return "/sources/listFolders";
},
"data" : function (node, cb, par2) {
var data = node;
Expand Down
2 changes: 1 addition & 1 deletion ui/web/src/main/resources/static/js/cmp/sourceList.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ var SourceList = {
};

if (this.criteriaContributor) {
criteria = this.criteriaContributor(this, criteria);
criteria = this.criteriaContributor(this, criteria, data);
}
if (validValue(data) && validValue(data.fullPath)) {
criteria.path = data.fullPath;
Expand Down
4 changes: 2 additions & 2 deletions ui/web/src/main/resources/static/js/importRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,12 @@ var ImportRequestsTree = {
ImportRequestsTree.sourceList.refreshSources(0);
},

criteriaContributor: function(sourceList, criteria) {
criteriaContributor: function(sourceList, criteria, nodeData) {
criteria.requestId = ImportRequestsTree.getActiveImportId();
criteria.path = null;
return criteria;
},
criteriaContributorMatches: function(sourceList, criteria) {
criteriaContributorMatches: function(sourceList, criteria, nodeData) {
var block = ImportRequestsTree.getSelectedImportSource();
if (block) {
criteria.matchesOfImportId = block.dataobject.id;
Expand Down
Loading

0 comments on commit e23de2c

Please sign in to comment.