From e34fedee69a6f7e3ef2188a70d8be60f7d179c10 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Mon, 3 Apr 2017 10:19:37 +0300 Subject: [PATCH] Move graph file document codes around a bit refs #7118 Change-Id: I8a1036a8717b74f3e57b5ea2519de27fb9dc2eab --- .../META-INF/MANIFEST.MF | 3 +- .../org.simantics.document.ontology/graph.tg | Bin 11099 -> 11244 bytes .../graph/Document.pgraph | 5 + .../simantics/document/DocumentResource.java | 6 + .../document/ui/actions/AddUrlDocument.java | 1 + .../ui/actions/AddUrlDocumentWithDetail.java | 1 + .../ui/actions/ExportDocumentFolder.java | 2 +- .../ui/actions/ImportDocumentFolder.java | 2 +- .../ui/actions/ImportDocumentWithDetail.java | 3 +- .../ui/wizard/FileDocumentImportWizard.java | 2 +- .../META-INF/MANIFEST.MF | 4 +- .../src/org/simantics/document/Activator.java | 3 + .../simantics/document/AddDocumentAction.java | 60 +++ .../document/DocumentVersionUtils.java | 178 +++++++ .../simantics/document/FileDocumentUtil.java | 474 ++++++++++++++++++ .../simantics/document/ImportDocument.java | 112 +++++ 16 files changed, 850 insertions(+), 6 deletions(-) create mode 100644 bundles/org.simantics.document/src/org/simantics/document/AddDocumentAction.java create mode 100644 bundles/org.simantics.document/src/org/simantics/document/DocumentVersionUtils.java create mode 100644 bundles/org.simantics.document/src/org/simantics/document/FileDocumentUtil.java create mode 100644 bundles/org.simantics.document/src/org/simantics/document/ImportDocument.java diff --git a/bundles/org.simantics.document.ontology/META-INF/MANIFEST.MF b/bundles/org.simantics.document.ontology/META-INF/MANIFEST.MF index 5397d24db..517ce7f3b 100644 --- a/bundles/org.simantics.document.ontology/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.document.ontology/META-INF/MANIFEST.MF @@ -9,6 +9,7 @@ Require-Bundle: org.simantics.layer0, org.simantics.scenegraph.ontology;bundle-version="1.0.0", org.simantics.viewpoint.ontology;bundle-version="1.0.0", org.simantics.selectionview.ontology;bundle-version="1.1.0", - org.simantics.graphfile.ontology;bundle-version="0.1.0" + org.simantics.graphfile.ontology;bundle-version="0.1.0", + org.simantics.action.ontology;bundle-version="1.1.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Export-Package: org.simantics.document diff --git a/bundles/org.simantics.document.ontology/graph.tg b/bundles/org.simantics.document.ontology/graph.tg index aadabaa9a92d5782509edce10315771b1e9dbcf2..94f3d05181d30bad5872617c811080b9a5861860 100644 GIT binary patch literal 11244 zcmai)d3;<|6~~)&l1yfjv`I?^p%6qw)D}uX1PV=?G;Jg_HE9cO;OopwI&|`0c=OT_ z1SzX1h#)B7g1Dj99Tf#di?Z*62*{!W;>M!j|N8r#chAh}^~1+}J|}a(XS?Sv@4h#m zc`Xx3Ftv5goH?QMU$qz-1$d*Da4bpHDL0Q2P+PXccPXGTAw1}IG8^!nnrF6C#R|yB8Aa9Nk-SRa>3329wEY)slC)FdWPEG~tqq!Oh=(T0IR*dL}YHcsPItnKQ@dQQa2 z?7B)Bmm5@u5L?CtR-e!o^KV)y(9@qU26kAI{jF zaEuu8#&wx!=9k@akA-Pki6*#3?xtm#DfcLGEt=g3hkL0!%6RpQ*B%?HK}GhBx5#T~ z?$1-ZAzQG4Of%llJV4EW$+=Q;c5n$z=~q#^a)H&Zu0&;&5B+K}=4o`h_iE8BwC?6= z`ZBFo=Qh>C1dZ#2TGxdEE+p_Z!rioL0lFYh(R7U|b`=#X^D~W}@26&!aaln{e!iJW z&2}FxD~#)fnXC3{Dn3&z3+fi^)W-17qVdc&^x$^z@2EJ-B;Fw&dF;?Qsid>8K2FUJ z<8rpRa2wf9;Pe;`XB*EesL0P#H@9t}J6{YOX|`hrgHnpE4$Bj?oNsbmDXwj+>Sxra zskpLd2$z16dfVtSaqh0~GVK`=m(z5aUg1xYE;rpAFOjXN2-gg350Q^I4hzL$zGgI6 z#DTOd?2ML72FXMv8m&xh3EIW zt;X$0Dq4!AAX*&`SH{z@4E_t1M;fnk=C%89KPXDxH2!xobF?*eHSVlfTJC|bQJ-tA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MyogX6(E@Sfzx;Tm zNZwreRMO;dH2I_)Hsh6qn*NZq{-8v^1o}_NVT-&IQETz>bc>HAEf%B2`3?AGf6kLw z?c0le(~Zwc1l8}vrczuHX38iPDGO(7U$3=@;~>0{XI{_Kv#?O7cgVf|h>Rtvl#Aue zYA>6rM|*Ka=}&9k^}{d4xTN%_E{E5|ydu8Hh~UDL6z>iWDpAyr4NuN;n5 zsi~(__TnlwC*SteIGLUr5BY_1R8(fibxn~!if_5_B~d1hBV>Gm^sS6_CsFP$ZQ%UhY%L@oOA zr)Fzt!n+}OL(!d=nR&Tb!`fup(Pf&G*L=7O)N>4Z^V*L%ni2=D5y6%OldDWT{{``! BXtDqR diff --git a/bundles/org.simantics.document.ontology/graph/Document.pgraph b/bundles/org.simantics.document.ontology/graph/Document.pgraph index e006a5fd8..b35002d40 100644 --- a/bundles/org.simantics.document.ontology/graph/Document.pgraph +++ b/bundles/org.simantics.document.ontology/graph/Document.pgraph @@ -3,6 +3,7 @@ L0X = SG = SEL = GF = +ACT = DOC = : L0.Ontology @L0.new @@ -158,4 +159,8 @@ DOC.PlainTextDocument + * + */ +public abstract class AddDocumentAction implements ActionFactory { + + private Resource relation; + + public AddDocumentAction(ReadGraph graph, String relationUri) throws DatabaseException { + relation = graph.getResource(relationUri); + } + + + protected Resource getRelation() { + return relation; + } + + protected void linkDocument(WriteGraph graph, Resource target, Resource newDocument) throws DatabaseException{ + DocumentResource doc = DocumentResource.getInstance(graph); + + if (graph.isInstanceOf(target, doc.Document)) { + // adding a new revision + DocumentVersionUtils.createNewVersion(graph, target, newDocument, relation); + + + } else { + graph.claim(target, relation, newDocument); + FileDocumentUtil.createUniqueName(graph, newDocument); + } + } + +} diff --git a/bundles/org.simantics.document/src/org/simantics/document/DocumentVersionUtils.java b/bundles/org.simantics.document/src/org/simantics/document/DocumentVersionUtils.java new file mode 100644 index 000000000..819a06af3 --- /dev/null +++ b/bundles/org.simantics.document/src/org/simantics/document/DocumentVersionUtils.java @@ -0,0 +1,178 @@ +package org.simantics.document; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.document.DocumentResource; +import org.simantics.layer0.Layer0; +/** + * Util class for managing document versions + * + * Document version history mode is searched with relations: + * 1. l0.PartOf + * 2. l0.IsOwnedBy + * 3. l0.IsDependencyOf + * + * If library is not found, history mode is assumed to be flat. + * + * In order to have working tree history, the library must contain proper configuration for relations: + * doc.HasLibraryRelation + * doc.HasVersionType + * + * @author Marko Luukkainen + * + */ +public class DocumentVersionUtils { + + public enum VersionMode{FLAT,TREE}; + + + /** + * Adds a new document version. + * + * Expects that newDocument is not part of document version chain, and oldDocument does not have newer version. + * + * @param graph + * @param oldDocument + * @param newDocument + * @param relation + * @throws DatabaseException + */ + public static void createNewVersion(WriteGraph graph, Resource oldDocument, Resource newDocument, Resource relation) throws DatabaseException{ + DocumentResource doc = DocumentResource.getInstance(graph); + if (graph.hasStatement(oldDocument, doc.HasNewerVersion)) + throw new DatabaseException("Document " + NameUtils.getSafeName(graph, oldDocument) +" has already new version"); + + Resource inverse = graph.getInverse(relation); + Resource lib = graph.getSingleObject(oldDocument, inverse); + String modeString = graph.getPossibleRelatedValue(lib, doc.HasVersionType); + VersionMode mode = VersionMode.FLAT; + if (modeString != null) + mode = VersionMode.valueOf(modeString); + + graph.claim(oldDocument, doc.HasNewerVersion, newDocument); + graph.claim(lib, relation, newDocument); + + if (mode == VersionMode.TREE) { + graph.deny(lib,relation,oldDocument); + graph.claim(newDocument,relation,oldDocument); + } + FileDocumentUtil.createUniqueName(graph, newDocument); + } + + /** + * Sets document version relationship between two documents. + * + * If the documents are already part of the same version chain this method does nothing. + * + * If documents are already set to some version chain with given relation, this method replaces the exiting links. + * + * @param graph + * @param document1 + * @param document2 + * @param versionRel + * @throws DatabaseException + */ + public static void setVersion(WriteGraph graph, Resource document1, Resource document2, Resource versionRel) throws DatabaseException { + DocumentResource doc = DocumentResource.getInstance(graph); + // document type must match + if (!graph.getSingleType(document2, doc.Document).equals(graph.getSingleType(document1, doc.Document))) + return; + if (!versionRel.equals(doc.HasNewerVersion) && !(versionRel.equals(doc.HasOlderVersion))) + throw new IllegalArgumentException("Unknow version relation + " + graph.getPossibleURI(versionRel)); + + Resource versionRelInv = graph.getInverse(versionRel); + // toSet must not be part of the document's version history + Resource r = document1; + while (r != null) { + if (r.equals(document2)) + return; + r = graph.getPossibleObject(r, versionRel); + } + + r = document2; + while (r != null) { + if (r.equals(document1)) + return; + r = graph.getPossibleObject(r, versionRel); + } + // At the moment document revision history is linear (no branching). + Resource document1Ver = graph.getPossibleObject(document1, versionRel); + if (document1Ver != null) + unsetVersion(graph, document1, document1Ver, versionRel); + Resource document2Ver = graph.getPossibleObject(document2, versionRelInv); + if (document2Ver != null) + unsetVersion(graph, document2, document2Ver, versionRelInv); + + graph.claim(document1, versionRel, document2); + + Resource lib = getLib(graph, document1); + if (lib != null) { + Resource relation = graph.getPossibleObject(lib, doc.HasLibraryRelation); + String type= graph.getPossibleRelatedValue(lib, doc.HasVersionType); + if ("TREE".equals(type) && relation != null) { + if (versionRel.equals(doc.HasOlderVersion)) { + graph.deny(document2, graph.getInverse(relation)); + graph.claim(document1,relation,document2); + } else { + graph.deny(document1, graph.getInverse(relation)); + graph.claim(document2,relation,document1); + } + } + + } + } + + /** + * Unlinks document version relationship between two documents. + * @param graph + * @param document1 + * @param document2 + * @param versionRel + * @throws DatabaseException + */ + public static void unsetVersion(WriteGraph graph, Resource document1, Resource document2, Resource versionRel) throws DatabaseException { + DocumentResource doc = DocumentResource.getInstance(graph); + if (!versionRel.equals(doc.HasNewerVersion) && !(versionRel.equals(doc.HasOlderVersion))) + throw new IllegalArgumentException("Unknow version relation + " + graph.getPossibleURI(versionRel)); + + graph.deny(document1, versionRel,document2); + Resource lib = getLib(graph, document1); + if (lib != null) { + Resource relation = graph.getPossibleObject(lib, doc.HasLibraryRelation); + String type= graph.getPossibleRelatedValue(lib, doc.HasVersionType); + if ("TREE".equals(type) && relation != null) { + if (versionRel.equals(doc.HasOlderVersion)) { + graph.deny(document1, relation); + graph.claim(lib,relation,document2); + FileDocumentUtil.createUniqueName(graph, document2); + } else { + graph.deny(document1, graph.getInverse(relation)); + graph.claim(lib,relation,document1); + FileDocumentUtil.createUniqueName(graph, document1); + } + } + + } + } + + private static Resource getLib(ReadGraph graph, Resource document) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + DocumentResource doc = DocumentResource.getInstance(graph); + Resource r = document; + while (true) { + Resource lib = graph.getPossibleObject(r, l0.PartOf); + if (lib == null) + lib = graph.getPossibleObject(r, l0.IsOwnedBy); + if (lib == null) + lib = graph.getPossibleObject(r, l0.IsDependencyOf); + if (lib == null) + return null; + if (!graph.isInstanceOf(lib, doc.Document)) + return lib; + r = lib; + } + } +} diff --git a/bundles/org.simantics.document/src/org/simantics/document/FileDocumentUtil.java b/bundles/org.simantics.document/src/org/simantics/document/FileDocumentUtil.java new file mode 100644 index 000000000..cab7e9a2c --- /dev/null +++ b/bundles/org.simantics.document/src/org/simantics/document/FileDocumentUtil.java @@ -0,0 +1,474 @@ +package org.simantics.document; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.graphfile.ontology.GraphFileResource; +import org.simantics.graphfile.util.GraphFileUtil; +import org.simantics.layer0.Layer0; +import org.simantics.utils.ui.ExceptionUtils; + +/** + * + * @author Marko Luukkainen + * + */ +public class FileDocumentUtil { + + /** + * Imports file, sets its L0.hasName, and and adds it to a library + * + * Note: if library relation is L0.ConsistsOf, L0.HasName is set to next available unique name. + * + * @param fileName + * @param lib + * @param rel + * @throws DatabaseException + */ + public static Resource importFile(final String fileName, final Resource lib, final Resource rel) throws DatabaseException { + return Simantics.getSession().syncRequest(new WriteResultRequest() { + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + return importFile(graph, fileName,lib,rel); + } + }); + + + } + + public static void importFileAsync(final String fileName, final Resource lib, final Resource rel) { + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + importFile(graph, fileName,lib,rel); + + } + },new org.simantics.utils.datastructures.Callback() { + + @Override + public void run(DatabaseException parameter) { + if (parameter != null) + ExceptionUtils.logAndShowError("Cannot import file " + fileName, parameter); + + } + }); + + + } + + /** + * Imports file, sets its L0.HasName, and and adds it to a library + * + * Note: if library relation is L0.ConsistsOf, L0.HasName is set to next available unique name. + * + * @param graph + * @param fileName + * @param lib + * @param rel + * @throws DatabaseException + */ + public static Resource importFile(WriteGraph graph, String fileName, Resource lib, Resource rel) throws DatabaseException{ + Layer0 l0 = Layer0.getInstance(graph); + Resource fileResource = importFile(graph, fileName); + graph.claim(lib, rel, fileResource); + File file = new File(fileName); + String name = file.getName(); + graph.claimLiteral(fileResource, l0.HasName, name); + setUniqueName(graph, fileResource, lib, rel); + return fileResource; + } + + public static Resource importFileWithName(WriteGraph graph, String fileName) throws DatabaseException{ + Layer0 l0 = Layer0.getInstance(graph); + Resource fileResource = importFile(graph, fileName); + File file = new File(fileName); + String name = file.getName(); + graph.claimLiteral(fileResource, l0.HasName, name); + return fileResource; + } + + /** + * Imports folder of documents recursively (including all sub folders). + * @param graph + * @param folderName Name of imported folder + * @param lib Library, where imported folder is attached. + * @param folderType Type of folders + * @param relation Relation used to create file/folder hierarchy + * @return the imported folder + * @throws DatabaseException + */ + public static Resource importFolderWithName(WriteGraph graph, String folderName, Resource lib, Resource folderType, Resource relation, IProgressMonitor monitor) throws Exception{ + Resource folderRes = importFolderWithName(graph, folderName, folderType, relation,monitor); + graph.claim(lib, relation, folderRes); + FileDocumentUtil.createUniqueName(graph, folderRes); + return folderRes; + } + + /** + * Imports folder of documents recursively (including all sub folders). + * @param graph + * @param folderName Name of imported folder + * @param folderType Type of folders + * @param relation Relation used to create file/folder hierarchy + * @param monitor ProgessMonitor or null + * @return the imported folder + * @throws DatabaseException + */ + public static Resource importFolderWithName(WriteGraph graph, String folderName, Resource folderType, Resource relation, IProgressMonitor monitor) throws Exception{ + Layer0 l0 = Layer0.getInstance(graph); + File folder = new File(folderName); + Resource rootFolderRes = graph.newResource(); + graph.claim(rootFolderRes, l0.InstanceOf, folderType); + graph.claimLiteral(rootFolderRes, l0.HasName, folder.getName()); + importFolder(graph, folder, rootFolderRes, relation, monitor); + return rootFolderRes; + } + + /** + * Imports folder of documents recursively (including all sub folders). + * @param graph + * @param folder Imported folder + * @param folderResource Resource folder matching file system folder + * @param relation Relation used to create file/folder hierarchy + * @throws DatabaseException + */ + public static void importFolder(WriteGraph graph, File folder, Resource folderResource, Resource relation, IProgressMonitor monitor) throws Exception{ + if (monitor != null) { + int count = _countFiles(folder); + monitor.beginTask("Import files", count); + } + _importFolder(graph, folder, folderResource, relation, monitor); + if (monitor != null) + monitor.done(); + } + + private static void _importFolder(WriteGraph graph, File folder, Resource folderResource, Resource relation, IProgressMonitor monitor) throws Exception{ + Layer0 l0 = Layer0.getInstance(graph); + File files[] = folder.listFiles(); + for (File f : files) { + if (f.isDirectory()) { + Resource newFolderRes = graph.newResource(); + graph.claim(newFolderRes, l0.InstanceOf, graph.getSingleType(folderResource)); + graph.claim(folderResource, relation, newFolderRes); + graph.claimLiteral(newFolderRes, l0.HasName, f.getName()); + _importFolder(graph, f, newFolderRes, relation,monitor); + } else { + Resource fileRes = null; + if (isUrl(f)) { + } else { + fileRes = importURL(graph, f); + fileRes = importFileWithName(graph, f.getAbsolutePath()); + } + graph.claim(folderResource, relation, fileRes); + if (monitor != null) + monitor.worked(1); + } + } + } + + private static int _countFiles(File folder) { + + int count = 0; + File files[] = folder.listFiles(); + for (File f : files) { + if (f.isDirectory()) { + count += _countFiles(f); + } else { + count++; + } + } + return count; + } + + + public static void createUniqueName(WriteGraph graph, Resource document) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + Resource lib = graph.getPossibleObject(document, l0.PartOf); + if (lib == null) + return; + setUniqueName(graph, document, lib, l0.ConsistsOf); + } + + public static void setUniqueName(WriteGraph graph, Resource res, Resource lib, Resource rel) throws DatabaseException{ + Layer0 l0 = Layer0.getInstance(graph); + Set names = new HashSet(); + for (Resource r : graph.getObjects(lib, rel)) { + if (r.equals(res)) + continue; + names.add((String)graph.getRelatedValue(r, l0.HasName)); + } + String name = graph.getRelatedValue(res, l0.HasName); + if (!names.contains(name)) + return; + int i = 1; + while (true) { + String proposal = name +" (" + i +")"; + if (!names.contains(proposal)) { + graph.claimLiteral(res, l0.HasName, proposal); + return; + } + i++; + } + + } + + /** + * Imports a file + * + * @param graph + * @param fileName + * @return + * @throws DatabaseException + */ + public static Resource importFile(WriteGraph graph, String fileName) throws DatabaseException{ + Layer0 l0 = Layer0.getInstance(graph); + DocumentResource doc = DocumentResource.getInstance(graph); + + Resource fileResource = graph.newResource(); + graph.claim(fileResource, l0.InstanceOf, doc.FileDocument); + try { + GraphFileUtil.toGraph(graph,fileName, fileResource); + + } catch (IOException e) { + throw new DatabaseException(e); + } + return fileResource; + + } + + /** + * Exports graph folder recursively to file system. + * @param graph + * @param folderResource + * @param folder + * @param relation + * @throws DatabaseException + */ + public static void exportDocumentFolder(final Resource folderResource, final File folder, final Resource relation, boolean useResourceNames, final IProgressMonitor monitor) throws Exception{ + Simantics.getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + try { + exportDocumentFolder(graph, folderResource, folder, relation, useResourceNames, monitor); + } catch (Exception e) { + throw new DatabaseException(e); + } + + } + }); + } + + + /** + * Exports graph folder recursively to file system. + * @param graph + * @param folderResource + * @param folder + * @param relation + * @throws DatabaseException + */ + public static void exportDocumentFolder(ReadGraph graph, Resource folderResource, File folder, Resource relation, boolean useResourceNames, IProgressMonitor monitor) throws Exception{ + Layer0 l0 = Layer0.getInstance(graph); + DocumentResource doc = DocumentResource.getInstance(graph); + GraphFileResource gf = GraphFileResource.getInstance(graph); + Set names = new HashSet(); + Collection folderType = graph.getPrincipalTypes(folderResource); + for (Resource r : graph.getObjects(folderResource, relation)) { + if (graph.isInstanceOf(r, doc.Document)) { + String name = null; + boolean canExport = false; + if (graph.isInstanceOf(r, doc.FileDocument)) { + name = graph.getRelatedValue(r, useResourceNames ? gf.HasResourceName : l0.HasName); + canExport = true; + } else if (graph.isInstanceOf(r, doc.UrlDocument)) { + name = graph.getRelatedValue(r, l0.HasName) +".url"; + canExport = true; + } + if (canExport) { + name = resolveName(folder, name, names, true); + File file = new File(folder.getAbsolutePath()+"/"+name); + if (graph.isInstanceOf(r, doc.FileDocument)) { + GraphFileUtil.writeDataToFile(graph,r, file); + } else if (graph.isInstanceOf(r, doc.UrlDocument)) { + String url = graph.getRelatedValue(r, doc.HasUrl); + String n = graph.getRelatedValue(r, l0.HasName); + exportUrl(file, n, url); + } + if (monitor != null) + monitor.worked(1); + } + + } else { + Collection type = graph.getPrincipalTypes(r); + if (type.size() == folderType.size() && folderType.containsAll(type)) { + String name = graph.getRelatedValue(r, l0.HasName); + name = resolveName(folder, name, names, false); + File subFolder = new File(folder.getAbsolutePath()+"/"+name); + if (!subFolder.exists()) { + if (!subFolder.mkdir()) { + // TODO : error. + continue; + } + } + exportDocumentFolder(graph, r, subFolder, relation, useResourceNames, monitor); + } + } + } + } + + /** + * Print URL to a file (Windows specific format?) + * @param toFile + * @param url + * @throws DatabaseException + */ + private static void exportUrl(File toFile, String name, String url) throws Exception{ + PrintStream os = new PrintStream(toFile,"UTF-8"); + os.println("[InternetShortcut]"); + os.println("URL="+url); + os.println("name="+name); + os.flush(); + os.close(); + } + + public static Resource importURL(WriteGraph graph, File file) throws Exception{ + String s = null; + String url = null; + String name = null; + BufferedReader is = null; + try { + is = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); + while ((s = is.readLine()) != null) { + if (s.startsWith("URL=")) { + url = s.substring(4); + } else if (s.startsWith("name=")) { + name = s.substring(5); + } + } + } finally { + if (is != null) + is.close(); + } + + if (url == null) + return null; + + Layer0 l0 = Layer0.getInstance(graph); + DocumentResource doc = DocumentResource.getInstance(graph); + + Resource fileResource = graph.newResource(); + graph.claim(fileResource, l0.InstanceOf, doc.UrlDocument); + if (name == null) { + name = file.getName(); + name = unescape(name); + name = name.substring(0,name.length()-4); + } + graph.claimLiteral(fileResource, l0.HasName, name); + graph.claimLiteral(fileResource, doc.HasUrl, url); + return fileResource; + } + + private static boolean isUrl(File file) throws Exception{ + return (file.getAbsolutePath().endsWith(".url")); + } + + private static final char ESCAPE = '%'; + + private static String escape(String s) { + + int len = s.length(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + char ch = s.charAt(i); + if (ch < ' ' || ch >= 0x7F || ch == '/' || ch == '\\' || ch == ':' || ch == ESCAPE) { + sb.append(ESCAPE); + if (ch < 0x10) { + sb.append('0'); + } + sb.append(Integer.toHexString(ch)); + } else { + sb.append(ch); + } + } + return sb.toString(); + } + + private static String unescape(String s) { + int len = s.length(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + char ch = s.charAt(i); + if (ch == ESCAPE) { + String num = "0x"; + num += s.charAt(++i); + num += s.charAt(++i); + ch = (char)Integer.decode(num).intValue(); + } + sb.append(ch); + } + return sb.toString(); + + } + + private static String resolveName(File parentFolder, String proposal, Set used, boolean file) { + String current = escape(proposal); + int i = 0; + if (file) { + while (true) { + i++; + if (used.contains(current)) { + current = createFileName(proposal, i); + } else { + File subFile = new File(parentFolder.getAbsolutePath()+"/"+current); + if (!subFile.exists()) + break; + if (subFile.exists() && subFile.isFile() && subFile.canWrite()) { + break; + } + } + } + } else { + while (true) { + i++; + if (used.contains(current)) { + current = proposal+i; + } else { + File subFolder = new File(parentFolder.getAbsolutePath()+"/"+current); + if (!subFolder.exists()) + break; + if (subFolder.exists() && subFolder.isDirectory()) { + break; + } + } + } + } + used.add(current); + return current; + } + + private static String createFileName(String original, int i) { + int extIndex = original.lastIndexOf("."); + if (extIndex == -1) + return original+i; + return original.substring(0,extIndex) + i + original.substring(extIndex); + } + +} diff --git a/bundles/org.simantics.document/src/org/simantics/document/ImportDocument.java b/bundles/org.simantics.document/src/org/simantics/document/ImportDocument.java new file mode 100644 index 000000000..c8d3ee3cd --- /dev/null +++ b/bundles/org.simantics.document/src/org/simantics/document/ImportDocument.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.document; + +import java.io.File; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; +import org.simantics.DatabaseJob; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; + +/** + * Action for importing files as documents. + * + * @author Marko Luukkainen + * + */ +public class ImportDocument extends AddDocumentAction { + + + public ImportDocument(ReadGraph graph, String relationUri) throws DatabaseException { + super(graph, relationUri); + } + + @Override + public Runnable create(Object target) { + if(!(target instanceof Resource)) + return null; + final Resource resource = (Resource)target; + return new Runnable() { + + @Override + public void run() { + FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN | SWT.MULTI); + // TODO : is there any way to read file/executable bindings from OS? + // if is, use those extensions to filter this list. + // note: in windows using "reg query ..." to read bindings form registry would work. + // Note : If the above mentioned filtering is implemented it should be made optional / configurable. + dialog.setFilterExtensions(new String[]{"*.*"}); + if (dialog.open() == null) return; + + String filterPath = dialog.getFilterPath(); + String[] filenames = dialog.getFileNames(); + + ImportJob job = new ImportJob(filenames.length > 1 ? "Import files" : "Import file", resource, filterPath, filenames); + job.setUser(true); + job.schedule(); + } + }; + } + + private class ImportJob extends DatabaseJob { + + public ImportJob(String name, Resource resource, String path, String[] filenames) { + super(name); + this.resource = resource; + this.path = path; + this.filenames = filenames; + } + + Resource resource; + String path; + String[] filenames; + + @Override + protected IStatus run(final IProgressMonitor monitor) { + monitor.beginTask("Importing...", filenames.length); + try { + Simantics.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + try { + graph.markUndoPoint(); + for (String filename : filenames) { + File f = new File(path, filename); + Resource newDoc = FileDocumentUtil.importFileWithName(graph, f.getAbsolutePath()); + linkDocument(graph, resource, newDoc); + monitor.worked(1); + } + } catch (Exception e) { + throw new DatabaseException(e); + } + } + }); + return new Status(IStatus.OK, Activator.PLUGIN_ID, "Import succesful."); + } catch (DatabaseException e) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed.", e); + } finally { + monitor.done(); + } + } + } + +} -- 2.47.1