import java.util.Map;
import java.util.Optional;
import java.util.Properties;
+import java.util.Set;
import java.util.function.Consumer;
+import java.util.stream.Collectors;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.simantics.databoard.util.Base64;
+import org.simantics.db.Resource;
import org.simantics.fileimport.dropins.FileImportDropins;
-import org.simantics.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
return null;
}
String result = "Import failed";
- Optional<IGenericFileImport> serviceOp = findServiceForFileExtension(file);
- if (serviceOp.isPresent()) {
- IGenericFileImport service = serviceOp.get();
+ IGenericFileImport service = findServiceForFileExtension(file);
+ if (service != null) {
try {
Optional<String> resource = service.perform(file);
saveResourceForPath(file, resource);
* @param callback Optional callback to catch Throwables thrown during the deletion process
*/
public static void removeResourceForFile(Path file, Optional<Consumer<Throwable>> callback) {
- Optional<IGenericFileImport> serviceOp = findServiceForFileExtension(file);
- serviceOp.ifPresent(service -> {
- try {
- Optional<String> resource = getResourceForPath(file);
- if (!resource.isPresent())
- return;
- service.remove(resource.get());
- removeResourceForPath(file);
- } catch (Throwable t) {
- if (callback.isPresent()) {
- callback.get().accept(t);
- } else {
- t.printStackTrace();
- }
+ try {
+ Optional<String> resource = getResourceForPath(file);
+ if (!resource.isPresent())
+ return;
+ IGenericFileImport service = findServiceForFileExtension(file);
+ if (service == null) {
+ LOGGER.warn("Could not find service for importing file " + file);
+ if (callback.isPresent())
+ callback.get().accept(new Exception("Could not find IGenericFileImport service for file " + file));
}
- });
+ service.remove(resource.get());
+ removeResourceForPath(file);
+ } catch (Throwable t) {
+ if (callback.isPresent()) {
+ callback.get().accept(t);
+ } else {
+ LOGGER.error("Could not remove resource for file " + file.toAbsolutePath(), t);
+ }
+ }
}
public static void removeFileForResource(long id, Optional<Consumer<Throwable>> callback) {
try {
fileOp = findPathForId(id);
} catch (IOException e) {
- e.printStackTrace();
+ LOGGER.error("Could not remove file for resource id " + id, e);
return;
}
if (!fileOp.isPresent())
return;
Path file = fileOp.get();
- Optional<IGenericFileImport> serviceOp = findServiceForFileExtension(file);
- serviceOp.ifPresent(service -> {
+
+ try {
+ Optional<String> resource = getResourceForPath(file);
+ if (!resource.isPresent())
+ return;
+ IGenericFileImport service = findServiceForFileExtension(file);
+ if (service == null) {
+ LOGGER.warn("Could not find service for importing file " + file);
+ if (callback.isPresent())
+ callback.get().accept(new Exception("Could not find IGenericFileImport service for file " + file));
+ }
+ service.remove(resource.get());
+ removeResourceForPath(file);
try {
- Optional<String> resource = getResourceForPath(file);
- if (!resource.isPresent())
- return;
- service.remove(resource.get());
- removeResourceForPath(file);
- try {
- Files.delete(file);
- } catch (IOException e) {
- Files.delete(file);
- }
- } catch (Throwable t) {
- if (callback.isPresent()) {
- callback.get().accept(t);
- } else {
- t.printStackTrace();
- }
+ Files.delete(file);
+ } catch (IOException e) {
+ Files.delete(file);
}
- });
+ } catch (Throwable t) {
+ if (callback.isPresent()) {
+ callback.get().accept(t);
+ } else {
+ LOGGER.error("Could not remove file for resource " + id, t);
+ }
+ }
}
private static Optional<Path> findPathForId(long id) throws IOException {
* Method for finding a File Import service for the given file based on the file extension
*
* @param file Path file for which the import service is looked for
- * @return Optiona IGenerigFileImport service which is able to handle the import of this type of file
+ * @return Optional IGenerigFileImport service which is able to handle the import of this type of file
*/
- public static Optional<IGenericFileImport> findServiceForFileExtension(Path file) {
+ public static IGenericFileImport findServiceForFileExtension(Path file) {
String extension = "";
int i = file.getFileName().toString().lastIndexOf('.');
extension = FOLDER;
}
}
+ return findServiceForExtension(extension);
+ }
+
+ public static List<String> filterSupportedExtensions(String filter) {
+ return getFileImportServices().stream().filter(s -> s.allowedExtensionsWithFilters().keySet().contains(filter)).map(s -> s.allowedExtensionsWithFilters().keySet()).flatMap(Set::stream).collect(Collectors.toList());
+ }
+ public static IGenericFileImport findServiceForExtension(String extension) {
+ List<IGenericFileImport> services = findServicesForExtension(extension);
+ IGenericFileImport service = null;
+ if (services.size() == 1) {
+ service = services.get(0);
+ } else {
+ for (IGenericFileImport servicee : services) {
+ service = servicee;
+ if (isPerfectMatch(servicee.allowedExtensionsWithFilters().keySet(), extension))
+ break;
+ }
+ }
+ return service;
+ }
+
+ public static List<IGenericFileImport> findServicesForExtension(String extension) {
+ List<IGenericFileImport> result = new ArrayList<>();
List<IGenericFileImport> services = getFileImportServices();
for (IGenericFileImport service : services) {
for (Map.Entry<String, String> entry : service.allowedExtensionsWithFilters().entrySet()) {
possibleExtensions = possibleExtensions.substring(1);
if (possibleExtensions.equals(extension) || possibleExtensions.isEmpty()) {
if (extension.equals(FOLDER) && possibleExtensions.equals(FOLDER)) {
- return Optional.of(service);
+ result.add(service);
} else if (!extension.isEmpty() && !extension.equals(FOLDER)){
- return Optional.of(service);
+ result.add(service);
}
}
}
}
- return Optional.empty();
+ return result;
}
/**
}
return map;
} catch (IOException e) {
- e.printStackTrace();
+ LOGGER.error("Could not get current paths and resources!", e);
return Collections.emptyMap();
}
}
props.store(stream, null);
}
} catch (IOException e) {
- e.printStackTrace();
+ LOGGER.error("Could not save resource for path " + file.toAbsolutePath() + " and resource " + resource.get(), e);
}
});
}
return Optional.empty();
return Optional.of(value);
}
+
+ public static String importGenericFileWithExtension(String path, String extension) throws Exception {
+ IGenericFileImport service = findServiceForExtension(extension);
+ Optional<String> result = service.perform(Paths.get(path));
+ return result.get();
+ }
+
+ public static Resource importGenericFileWithExtensionAndParent(Resource parent, String path, String extension) throws Exception {
+ IGenericFileImport service = findServiceForExtension(extension);
+ Optional<Resource> result = service.perform(parent, Paths.get(path));
+ return result.get();
+ }
+
+ private static boolean isPerfectMatch(Set<String> candidates, String extension) {
+ for (String ext : candidates) {
+ if (ext.startsWith("."))
+ ext = ext.substring(1);
+ if (ext.equals(extension))
+ return true;
+ }
+ return false;
+ }
}
import org.simantics.db.Resource;
import org.simantics.db.WriteGraph;
import org.simantics.db.common.request.DelayedWriteRequest;
-import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.exception.BindingException;
import org.simantics.db.exception.DatabaseException;
import org.simantics.spreadsheet.resource.SpreadsheetResource;
import org.simantics.spreadsheet.util.SpreadsheetUtils;
import org.simantics.utils.DataContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class ExcelImport {
-
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ExcelImport.class);
+
private static final double POINT_TO_PIXEL_RATIO = 1.33;
- public static void importBook(Resource container, File file) {
+ public static void importBook(Resource container, File file) {
+ importBook(container, file);
+ }
+
+ public static Resource importBookR(Resource container, File file) {
try {
btreeContainer.set(result);
bookContainer.set(book);
} catch (Exception e) {
- e.printStackTrace();
+ LOGGER.error("Could not import book " + file.getAbsolutePath(), e);
btreeContainer.add(Collections.emptyList());
}
}
}
});
- Simantics.getSession().sync(new WriteRequest() {
+ return Simantics.getSession().sync(new WriteResultRequest<Resource>() {
@Override
- public void perform(WriteGraph graph) throws DatabaseException {
+ public Resource perform(WriteGraph graph) throws DatabaseException {
Resource delayedBook = bookContainer.get();
XSupport support = graph.getService(XSupport.class);
Resource book = support.convertDelayedResourceToResource(delayedBook);
SpreadsheetGraphUtils.constructAndInitializeRunVariable(graph, book);
+ return book;
}
});
} catch (Exception e) {
- e.printStackTrace();
+ LOGGER.error("Could not import book " + file.getAbsolutePath(), e);
+ return null;
}
-
}
private static Resource assignStyles(WriteGraph graph, SpreadsheetResource SR, Cell cell, Resource book, Map<Integer, Resource> existingStyles, Map<Integer, SpreadsheetStyle> existingStyles2, String styleName) throws DatabaseException {