Search in sources :

Example 21 with Database

use of lotus.domino.Database in project org.openntf.xsp.jakartaee by OpenNTF.

the class DefaultDominoDocumentCollectionManager method getQrpDatabase.

private Database getQrpDatabase(Session session, Database database) throws NotesException {
    String server = database.getServer();
    String filePath = database.getFilePath();
    try {
        // $NON-NLS-1$
        String fileName = md5(server + filePath) + ".nsf";
        Path tempDir = getTempDirectory();
        Path dest = tempDir.resolve(getClass().getPackage().getName());
        Files.createDirectories(dest);
        Path dbPath = dest.resolve(fileName);
        // $NON-NLS-1$
        Database qrp = session.getDatabase("", dbPath.toString());
        if (!qrp.isOpen()) {
            qrp.recycle();
            DbDirectory dbDir = session.getDbDirectory(null);
            // TODO encrypt when the API allows
            qrp = dbDir.createDatabase(dbPath.toString(), true);
            ACL acl = qrp.getACL();
            ACLEntry entry = acl.createACLEntry(session.getEffectiveUserName(), ACL.LEVEL_MANAGER);
            entry.setCanDeleteDocuments(true);
            acl.save();
        }
        return qrp;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : Path(java.nio.file.Path) DbDirectory(lotus.domino.DbDirectory) ACLEntry(lotus.domino.ACLEntry) Database(lotus.domino.Database) ACL(lotus.domino.ACL) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 22 with Database

use of lotus.domino.Database in project org.openntf.xsp.jakartaee by OpenNTF.

the class DefaultDominoDocumentCollectionManager method select.

@Override
public Stream<DocumentEntity> select(DocumentQuery query) {
    try {
        QueryConverterResult queryResult = QueryConverter.select(query);
        long skip = queryResult.getSkip();
        long limit = queryResult.getLimit();
        List<Sort> sorts = query.getSorts();
        Stream<DocumentEntity> result;
        if (sorts != null && !sorts.isEmpty()) {
            Database database = supplier.get();
            Session sessionAsSigner = sessionSupplier.get();
            Database qrpDatabase = getQrpDatabase(sessionAsSigner, database);
            String userName = database.getParent().getEffectiveUserName();
            // $NON-NLS-1$
            String viewName = getClass().getName() + "-" + (String.valueOf(sorts) + skip + limit + userName).hashCode();
            View view = qrpDatabase.getView(viewName);
            try {
                if (view != null) {
                    // Check to see if we need to "expire" it based on the data mod time of the DB
                    DateTime created = view.getCreated();
                    try {
                        long dataMod = NotesSession.getLastDataModificationDateByName(database.getServer(), database.getFilePath());
                        if (dataMod > (created.toJavaDate().getTime() / 1000)) {
                            view.remove();
                            view.recycle();
                            view = null;
                        }
                    } catch (NotesAPIException e) {
                        throw new RuntimeException(e);
                    } finally {
                        recycle(created);
                    }
                }
                if (view != null) {
                    result = EntityConverter.convert(database, view);
                } else {
                    DominoQuery dominoQuery = database.createDominoQuery();
                    QueryResultsProcessor qrp = qrpDatabase.createQueryResultsProcessor();
                    try {
                        qrp.addDominoQuery(dominoQuery, queryResult.getStatement().toString(), null);
                        for (Sort sort : sorts) {
                            int dir = sort.getType() == SortType.DESC ? QueryResultsProcessor.SORT_DESCENDING : QueryResultsProcessor.SORT_ASCENDING;
                            qrp.addColumn(sort.getName(), null, null, dir, false, false);
                        }
                        if (skip == 0 && limit > 0 && limit <= Integer.MAX_VALUE) {
                            qrp.setMaxEntries((int) limit);
                        }
                        view = qrp.executeToView(viewName, 24);
                        try {
                            result = EntityConverter.convert(database, view);
                        } finally {
                            recycle(view);
                        }
                    } finally {
                        recycle(qrp, dominoQuery, qrpDatabase);
                    }
                }
            } finally {
                recycle(view);
            }
        } else {
            Database database = supplier.get();
            DominoQuery dominoQuery = database.createDominoQuery();
            DocumentCollection docs = dominoQuery.execute(queryResult.getStatement().toString());
            try {
                result = EntityConverter.convert(docs);
            } finally {
                recycle(docs, dominoQuery);
            }
        }
        if (skip > 0) {
            result = result.skip(skip);
        }
        if (limit > 0) {
            result = result.limit(limit);
        }
        return result;
    } catch (NotesException e) {
        throw new RuntimeException(e);
    }
}
Also used : QueryResultsProcessor(lotus.domino.QueryResultsProcessor) QueryConverterResult(org.openntf.xsp.nosql.communication.driver.QueryConverter.QueryConverterResult) DocumentCollection(lotus.domino.DocumentCollection) View(lotus.domino.View) DateTime(lotus.domino.DateTime) NotesException(lotus.domino.NotesException) DocumentEntity(jakarta.nosql.document.DocumentEntity) Database(lotus.domino.Database) Sort(jakarta.nosql.Sort) NotesAPIException(com.ibm.designer.domino.napi.NotesAPIException) DominoQuery(lotus.domino.DominoQuery) NotesSession(com.ibm.designer.domino.napi.NotesSession) Session(lotus.domino.Session)

Example 23 with Database

use of lotus.domino.Database in project org.openntf.xsp.jakartaee by OpenNTF.

the class DefaultDominoDocumentCollectionManager method insert.

@Override
public DocumentEntity insert(DocumentEntity entity) {
    try {
        Database database = supplier.get();
        lotus.domino.Document target = database.createDocument();
        Optional<Document> maybeId = entity.find(EntityConverter.ID_FIELD);
        if (maybeId.isPresent()) {
            target.setUniversalID(maybeId.get().get().toString());
        } else {
            // Write the generated UNID into the entity
            entity.add(Document.of(EntityConverter.ID_FIELD, target.getUniversalID()));
        }
        EntityConverter.convert(entity, target);
        target.save();
        return entity;
    } catch (NotesException e) {
        throw new RuntimeException(e);
    }
}
Also used : NotesException(lotus.domino.NotesException) Database(lotus.domino.Database) Document(jakarta.nosql.document.Document)

Example 24 with Database

use of lotus.domino.Database in project org.openntf.xsp.jakartaee by OpenNTF.

the class DefaultDominoDocumentCollectionManager method count.

@Override
public long count(String documentCollection) {
    try {
        Database database = supplier.get();
        DominoQuery dominoQuery = database.createDominoQuery();
        DQLTerm dql = DQL.item(EntityConverter.NAME_FIELD).isEqualTo(documentCollection);
        DocumentCollection result = dominoQuery.execute(dql.toString());
        return result.getCount();
    } catch (NotesException e) {
        throw new RuntimeException(e);
    }
}
Also used : NotesException(lotus.domino.NotesException) Database(lotus.domino.Database) DominoQuery(lotus.domino.DominoQuery) DocumentCollection(lotus.domino.DocumentCollection) DQLTerm(org.openntf.xsp.nosql.communication.driver.DQL.DQLTerm)

Example 25 with Database

use of lotus.domino.Database in project openliberty-domino by OpenNTF.

the class AdminNSFAppDeploymentProvider method deployApp.

@Override
public void deployApp(String serverName, String appName, String contextPath, String fileName, Boolean includeInReverseProxy, InputStream appData) {
    if (StringUtil.isEmpty(serverName)) {
        throw new IllegalArgumentException("serverName cannot be empty");
    }
    if (StringUtil.isEmpty(appName)) {
        throw new IllegalArgumentException("appName cannot be empty");
    }
    try {
        Session session = NotesFactory.createSession();
        try {
            Database database = AdminNSFUtil.getAdminDatabase(session);
            View serversAndApps = database.getView(VIEW_SERVERSANDAPPS);
            serversAndApps.setAutoUpdate(false);
            ViewEntry serverEntry = serversAndApps.getEntryByKey(serverName, true);
            if (serverEntry == null) {
                throw new IllegalArgumentException(MessageFormat.format("Unable to locate server \"{0}\"", serverName));
            }
            ViewNavigator nav = serversAndApps.createViewNavFromChildren(serverEntry);
            ViewEntry appEntry = nav.getFirst();
            while (appEntry != null) {
                Vector<?> columnValues = appEntry.getColumnValues();
                String entryAppName = (String) columnValues.get(0);
                if (appName.equalsIgnoreCase(entryAppName)) {
                    break;
                }
                appEntry.recycle(columnValues);
                ViewEntry tempEntry = appEntry;
                appEntry = nav.getNextSibling(appEntry);
                tempEntry.recycle();
            }
            Document appDoc;
            if (appEntry == null) {
                appDoc = database.createDocument();
                // $NON-NLS-1$
                appDoc.replaceItemValue("Form", FORM_APP);
                appDoc.makeResponse(serverEntry.getDocument());
                appDoc.replaceItemValue(ITEM_APPNAME, appName);
            } else {
                appDoc = appEntry.getDocument();
            }
            String path = contextPath;
            if (StringUtil.isEmpty(path)) {
                // Determine whether to change an existing value
                String existing = appDoc.getItemValueString(ITEM_CONTEXTPATH);
                if (StringUtil.isEmpty(existing)) {
                    // $NON-NLS-1$
                    path = "/" + appName;
                    appDoc.replaceItemValue(ITEM_CONTEXTPATH, path);
                }
            } else {
                appDoc.replaceItemValue(ITEM_CONTEXTPATH, path);
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            appDoc.replaceItemValue(ITEM_REVERSEPROXY, includeInReverseProxy != null && includeInReverseProxy ? "Y" : "N");
            if (appDoc.hasItem(ITEM_FILE)) {
                appDoc.removeItem(ITEM_FILE);
            }
            RichTextItem fileItem = appDoc.createRichTextItem(ITEM_FILE);
            Path tempDir = Files.createTempDirectory(OpenLibertyUtil.getTempDirectory(), getClass().getName());
            try {
                String fname = fileName;
                if (StringUtil.isEmpty(fname)) {
                    // TODO consider a non-WAR default
                    // $NON-NLS-1$
                    fname = appName + ".war";
                }
                Path file = tempDir.resolve(fname);
                Files.copy(appData, file, StandardCopyOption.REPLACE_EXISTING);
                // $NON-NLS-1$
                fileItem.embedObject(EmbeddedObject.EMBED_ATTACHMENT, "", file.toString(), null);
            } finally {
                OpenLibertyUtil.deltree(tempDir);
            }
            appDoc.save();
        } finally {
            session.recycle();
        }
    } catch (NotesException | IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) ViewNavigator(lotus.domino.ViewNavigator) IOException(java.io.IOException) Document(lotus.domino.Document) View(lotus.domino.View) NotesException(lotus.domino.NotesException) ViewEntry(lotus.domino.ViewEntry) RichTextItem(lotus.domino.RichTextItem) Database(lotus.domino.Database) Session(lotus.domino.Session)

Aggregations

Database (lotus.domino.Database)32 Session (lotus.domino.Session)18 Document (lotus.domino.Document)16 NotesException (lotus.domino.NotesException)13 DocumentCollection (lotus.domino.DocumentCollection)5 NoteCollection (lotus.domino.NoteCollection)5 View (lotus.domino.View)4 IOException (java.io.IOException)3 Vector (java.util.Vector)3 DateTime (lotus.domino.DateTime)3 DominoQuery (lotus.domino.DominoQuery)3 Item (lotus.domino.Item)3 ViewEntry (lotus.domino.ViewEntry)3 DominoDocument (com.ibm.xsp.model.domino.wrapped.DominoDocument)2 Document (jakarta.nosql.document.Document)2 URI (java.net.URI)2 Path (java.nio.file.Path)2 Date (java.util.Date)2 ExecutionException (java.util.concurrent.ExecutionException)2 ACL (lotus.domino.ACL)2