Commit 5834281c authored by Andrey Lugovskiy's avatar Andrey Lugovskiy Committed by Matija Obreza

Download repository metadata for an institute

- metadata files info
- Update WiewsController
- download files metadata
parent 1d509486
......@@ -792,7 +792,7 @@
</attributes>
</configuration>
</plugin>
<!-- <plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>1.9</version>
......@@ -815,7 +815,7 @@
<configuration>
<sortArtifactByName>true</sortArtifactByName>
</configuration>
</plugin> -->
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
......
......@@ -55,6 +55,8 @@ import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import javax.servlet.http.HttpServletResponse;
public interface GenesysService {
/**
......@@ -290,4 +292,12 @@ public interface GenesysService {
Page<AccessionDetails> listAccessionsDetails(Collection<Long> accessionIds, Pageable page);
/**
* Method for download files metadata
* @param response response
* @param repositoryPath repository path
* @throws IOException
*/
void downloadFilesMetadata(HttpServletResponse response, String repositoryPath) throws IOException;
}
package org.genesys2.server.service.impl;
import org.genesys.filerepository.model.RepositoryFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* @author Andrey Lugovskoy.
*/
@Service("filesMetadataInfo")
public class FilesMetadataInfo extends MetadataInfo<RepositoryFile> {
@Value("#{'${files.metadata.fields}'.split(',')}") private List<String> filesMetadataHeader;
@Override
public void downloadMetadata(List<RepositoryFile> entities, HttpServletResponse response, char separator, char quoteChar, char escapeChar, String lineEnd) throws IOException {
super.downloadMetadata(filesMetadataHeader, entities, response, separator, quoteChar, escapeChar, lineEnd);
}
}
......@@ -16,37 +16,16 @@
package org.genesys2.server.service.impl;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.persistence.EntityManager;
import com.opencsv.CSVWriter;
import com.opencsv.ResultSetHelper;
import com.opencsv.ResultSetHelperService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang.StringUtils;
import org.genesys.blocks.security.SecurityContextUtil;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.model.elastic.AccessionDetails;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias;
......@@ -127,6 +106,28 @@ import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Service
@Transactional(readOnly = true)
public class GenesysServiceImpl implements GenesysService, DatasetService {
......@@ -200,6 +201,12 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Autowired
private GenesysFilterService filterService;
@Autowired(required = false)
private RepositoryService repositoryService;
@Autowired(required = false)
private FilesMetadataInfo filesMetadataInfo;
private List<String> availableDisplayColumns;
private List<String> defaultDisplayColumns;
......@@ -290,7 +297,8 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
public Page<AccessionDetails> listAccessionsDetails(Collection<Long> accessionIds, Pageable pageable) {
if (!accessionIds.isEmpty()) {
Page<Accession> data = accessionRepository.findById(accessionIds, pageable);
return new PageImpl<AccessionDetails>(getAccessionDetails(data.getContent().stream().map(a -> a.getId()).collect(Collectors.toList())), pageable, data.getTotalElements());
return new PageImpl<AccessionDetails>(getAccessionDetails(data.getContent().stream().map(a -> a.getId()).collect(Collectors.toList())), pageable,
data.getTotalElements());
} else {
return null;
}
......@@ -299,7 +307,8 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Override
public Accession getAccession(AccessionIdentifier3 aid3) throws NonUniqueAccessionException {
try {
Accession accession = accessionRepository.findOne(instituteRepository.findByCode(aid3.getHoldingInstitute()), aid3.getDoi(), aid3.getAccessionNumber(), aid3.getGenus());
Accession accession = accessionRepository
.findOne(instituteRepository.findByCode(aid3.getHoldingInstitute()), aid3.getDoi(), aid3.getAccessionNumber(), aid3.getGenus());
return lazyLoadAccession(accession);
} catch (IncorrectResultSizeDataAccessException e) {
LOG.error("Duplicate accession name instCode={} acceNumb={} genus={}", aid3.getHoldingInstitute(), aid3.getAccessionNumber(), aid3.getGenus());
......@@ -1901,4 +1910,17 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
public List<Object[]> getLastUpdatedStatistics(FaoInstitute faoInstitute) {
return accessionRepository.lastUpdatedStatistics(faoInstitute);
}
/**
* {@inheritDoc}
*
* @param response
*/
@Override
public void downloadFilesMetadata(HttpServletResponse response, String repositoryPath) throws IOException {
List<RepositoryFile> files = repositoryService.getFiles(repositoryPath);
filesMetadataInfo.downloadMetadata(files, response, ' ', '"', '\\', "\r\n");
}
}
package org.genesys2.server.service.impl;
import com.opencsv.CSVWriter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* @author Andrey Lugovskoy.
*/
public abstract class MetadataInfo<T> {
public static final Log LOG = LogFactory.getLog(MetadataInfo.class);
public abstract void downloadMetadata(List<T> entities, HttpServletResponse response, char separator, char quoteChar, char escapeChar, String lineEnd) throws IOException;
public void downloadMetadata(List<String> filesMetadataFields, List<T> entities, HttpServletResponse response, char separator, char quoteChar, char escapeChar, String lineEnd)
throws IOException {
try (CSVWriter writer = new CSVWriter(new BufferedWriter(new OutputStreamWriter(response.getOutputStream())), separator, quoteChar, escapeChar, lineEnd)) {
writer.writeNext(filesMetadataFields.toArray(ArrayUtils.EMPTY_STRING_ARRAY));
entities.stream().forEach(element -> {
List<Object> values = new ArrayList<>();
Class<?> aClass = element.getClass();
filesMetadataFields.stream().forEach(fieldName -> {
Field field;
Object value = " ";
try {
field = aClass.getField(fieldName);
value = field.get(element);
} catch (NoSuchFieldException e) {
try {
field = aClass.getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
value = field.get(element);
value = value == null ? " " : value;
} catch (NoSuchFieldException | IllegalAccessException e1) {
LOG.error("Error getting value from field in " + element.getClass().getName());
}
} catch (IllegalAccessException e) {
LOG.error("Error getting value from field in " + element.getClass().getName());
}
values.add(value);
});
String[] stockArr = new String[values.size()];
stockArr = values.toArray(stockArr);
writer.writeNext(stockArr);
});
writer.flush();
}
}
}
......@@ -16,22 +16,6 @@
package org.genesys2.server.servlet.controller;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
......@@ -82,6 +66,21 @@ import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.UriUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Controller
@Scope("request")
@RequestMapping("/wiews")
......@@ -506,8 +505,6 @@ public class WiewsController extends BaseController {
public String uploadFile(@RequestParam MultipartFile file, @PathVariable String wiewsCode, @RequestParam String repositoryPath,
RedirectAttributes redirectAttributes) throws IOException {
//check user permissions
instituteService.getInstituteForEdit(wiewsCode);
final String mimeType = file.getContentType();
try {
......@@ -610,7 +607,6 @@ public class WiewsController extends BaseController {
model.addAttribute("thumbnailFormat", "200x200");
model.addAttribute("imageGallery", imageGallery);
model.addAttribute("wiewsCode", wiewsCode);
model.addAttribute("gallerySubPath", gallerySubPath);
return MANAGE_FILES_JSP_PATH + "/gallery/details";
}
......@@ -644,7 +640,7 @@ public class WiewsController extends BaseController {
}
redirectAttributes.addFlashAttribute("successMessage", "repository.gallery.successfully-updated");
return "redirect:/wiews/" + wiewsCode +"/files/gallery";
return "redirect:/wiews/" + wiewsCode + "/files/gallery";
}
@PostMapping(value = "{wiewsCode}/files/gallery/delete")
......@@ -656,7 +652,18 @@ public class WiewsController extends BaseController {
imageGalleryService.removeGallery(imageGallery);
redirectAttributes.addFlashAttribute("successMessage", "repository.gallery.removed");
return "redirect:/wiews/" + wiewsCode +"/files/gallery";
return "redirect:/wiews/" + wiewsCode + "/files/gallery";
}
@RequestMapping(value = "{wiewsCode}/files/download/metadata")
public void downloadMetadata(@PathVariable("wiewsCode") String wiewsCode, HttpServletResponse response) throws IOException {
final String repositoryPath = "/wiews/" + wiewsCode;
response.setHeader("Content-Disposition", "attachment; filename=" + wiewsCode +"_files_metadata.csv ");
genesysService.downloadFilesMetadata(response, repositoryPath);
}
@InitBinder
......
......@@ -63,6 +63,7 @@ public class ApplicationConfig {
final List<Resource> locations = new ArrayList<>();
locations.add(new ClassPathResource("application.properties"));
locations.add(new ClassPathResource("files-metadata.properties"));
locations.add(new ClassPathResource("genesys.properties"));
final String extraConfigFile = System.getenv("config.file");
......
......@@ -43,14 +43,6 @@ base.cookie-http-only=true
# robots.txt
robots.allow=false
db.url=jdbc:mysql://localhost/genesys?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
db.driverClassName=com.mysql.jdbc.Driver
db.username=root
db.password=
db.showSql=false
db.hbm2ddl=false
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
google.api.key=AIzaSyBEvPuc8j5ps5GDQ3tdnTJaffKhfOdxFVc
google.url.shortener=https://www.googleapis.com/urlshortener/v1/url
news.menu.abbreviate.length=22
......
......@@ -866,6 +866,7 @@ repository.file.dateSubmitted=Date of submission
repository.file.lastModifiedDate=Last modified date
file.upload-file=Upload file
file.download-metadata=Download metadata
file.upload-file.help=Select file for upload from local computer
file.manage-files=Manage files
......
......@@ -858,4 +858,6 @@ welcome.search-genesys=البحث في جينيسيس
welcome.networks=الشبكات
geo.country=الدولة:
file.download-metadata=تنزيل البيانات الوصفية
file.download-metadata=تنزيل البيانات الوصفية
article.is.template=هو القالب:
......@@ -858,4 +858,6 @@ welcome.search-genesys=Genesys durchsuchen
welcome.networks=Netzwerke
geo.country=Land:
file.download-metadata=Metadaten herunterladen
file.download-metadata=Metadaten herunterladen
article.is.template=Ist Vorlage:
......@@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
file.download-metadata=Download metadata
......@@ -858,4 +858,6 @@ welcome.search-genesys=Buscar en Genesys
welcome.networks=Redes
geo.country=País:
file.download-metadata=Descargar metadatos
file.download-metadata=Descargar metadatos
article.is.template=Es plantilla:
......@@ -858,4 +858,6 @@ welcome.search-genesys=جستجو در Genesys
welcome.networks=شبکه‌ها
geo.country=کشور:
file.download-metadata=دانلود ابرداده
file.download-metadata=دانلود ابرداده
article.is.template=الگو است:
......@@ -858,4 +858,6 @@ welcome.search-genesys=Recherche sur Genesys
welcome.networks=Réseaux
geo.country=Pays :
file.download-metadata=Télécharger les métadonnées
file.download-metadata=Télécharger les métadonnées
article.is.template=Est-ce que le modèle:
......@@ -858,4 +858,6 @@ welcome.search-genesys=Pesquisar no Genesys
welcome.networks=Redes
geo.country=País:
file.download-metadata=Baixar metadados
file.download-metadata=Baixar metadados
article.is.template=É modelo:
......@@ -858,4 +858,6 @@ welcome.search-genesys=Поиск по Genesys
welcome.networks=Сети
geo.country=Страна:
file.download-metadata=Загрузить метаданные
file.download-metadata=Загрузить метаданные
article.is.template=Шаблон:
......@@ -858,4 +858,6 @@ welcome.search-genesys=搜索Genesys
welcome.networks=网站
geo.country=国家/地区:
file.download-metadata=下載元數據
file.download-metadata=下載元數據
article.is.template=是模板
files.metadata.fields=originalFilename,extension,title,subject,description,creator,created,license,contentType
......@@ -82,6 +82,13 @@
</tbody>
</table>
<form style="padding-bottom: 5px;" action="<c:url value="/wiews/${wiewsCode}/files/download/metadata"><c:param name="${_csrf.parameterName}" value="${_csrf.token}" /></c:url>"
method="post" enctype="multipart/form-data" class="">
<input type="hidden" name="wiewsCode" value="${wiewsCode}"/>
<input type="hidden" name="repositoryPath" value="${currentPath}"/>
<button type="submit" class="btn btn-primary"><spring:message code="file.download-metadata"/></button>
</form>
<form action="<c:url value="/wiews/${wiewsCode}/upload-file"><c:param name="${_csrf.parameterName}" value="${_csrf.token}" /></c:url>"
method="post" enctype="multipart/form-data" class="">
......
......@@ -282,6 +282,11 @@ public abstract class GenesysServicesTest extends BaseSpringTest {
return new GenesysServiceImpl();
}
@Bean(name = "filesMetadataInfo")
public FilesMetadataInfo filesMetadataInfo(){
return new FilesMetadataInfo();
}
@Bean
public CropService cropService() {
return new CropServiceImpl();
......
......@@ -36,7 +36,7 @@ public class PropertyPlacholderInitializer implements ApplicationContextInitiali
postProcessor.setLocations(new Resource[] { new ClassPathResource("application.properties"), new ClassPathResource("spring/spring.properties"),
// Include genesys.properties
new ClassPathResource("genesys.properties") });
new ClassPathResource("genesys.properties"), new ClassPathResource("files-metadata.properties") });
applicationContext.addBeanFactoryPostProcessor(postProcessor);
}
}
files.metadata.fields=originalFilename,extension,title,subject,description,creator.created,license,contentType
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment