/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.portfolio.handler;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.PathUtils;
import org.olat.fileresource.types.FileResource;
import org.olat.fileresource.types.ResourceEvaluation;
import org.olat.repository.RepositoryEntryImportExport;
import org.olat.repository.RepositoryEntryImportExport.RepositoryEntryImport;
/**
*
* Description:<br>
* Olat cannot import something else than files
* <P>
* Initial Date: 12 oct. 2010 <br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*/
public class BinderTemplateResource extends FileResource {
private static final OLog log = Tracing.createLoggerFor(BinderTemplateResource.class);
public static final String TYPE_NAME = "BinderTemplate";
public static final String BINDER_XML = "binder.xml";
/**
* @param f
* @return True if is of type.
*/
public static boolean validate(File f) {
if(f.isDirectory()) {
//unzip directory
return new File(f, BINDER_XML).exists();
}
return f.getName().toLowerCase().endsWith(BINDER_XML);
}
public static ResourceEvaluation evaluate(File file, String filename) {
ResourceEvaluation eval = new ResourceEvaluation();
try {
BinderFileFilter visitor = new BinderFileFilter();
Path fPath = PathUtils.visit(file, filename, visitor);
if(visitor.isValid()) {
eval.setValid(true);
Path repoXml = fPath.resolve(RepositoryEntryImportExport.PROPERTIES_FILE);
if(Files.exists(repoXml)) {
RepositoryEntryImport re = RepositoryEntryImportExport.getConfiguration(repoXml);
if(re != null) {
eval.setDisplayname(re.getDisplayname());
eval.setDescription(re.getDescription());
}
}
} else {
eval.setValid(false);
}
} catch (IOException | IllegalArgumentException e) {
log.error("", e);
eval.setValid(false);
}
return eval;
}
private static class BinderFileFilter extends SimpleFileVisitor<Path> {
private boolean binderFile;
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
String filename = file.getFileName().toString();
if(BINDER_XML.equals(filename)) {
binderFile = true;
}
return binderFile ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE;
}
public boolean isValid() {
return binderFile;
}
}
}