Search in sources :

Example 11 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class NodeIdRepresentation method serializeAT.

/**
 * This method serializes requested resource with an access type.
 *
 * @param resource
 *          The requested resource
 * @param nodeId
 *          The node id of the requested resource.
 * @param revision
 *          The revision of the requested resource.
 * @param doNodeId
 *          Specifies whether the node id's have to be shown in the result.
 * @param output
 *          The output stream to be written.
 * @param wrapResult
 *          Specifies whether the result has to be wrapped with a result
 *          element.
 * @param accessType
 *          The {@link IDAccessType} which indicates the access to a special
 *          node.
 */
private void serializeAT(final String resource, final long nodeId, final Integer revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult, final IDAccessType accessType) {
    if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
        Session session = null;
        Database database = null;
        NodeReadTrx rtx = null;
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            if (revision == null) {
                rtx = session.beginNodeReadTrx();
            } else {
                rtx = session.beginNodeReadTrx(revision);
            }
            if (rtx.moveTo(nodeId).hasMoved()) {
                switch(accessType) {
                    case FIRSTCHILD:
                        if (!rtx.moveToFirstChild().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    case LASTCHILD:
                        if (rtx.moveToFirstChild().hasMoved()) {
                            long last = rtx.getNodeKey();
                            while (rtx.moveToRightSibling().hasMoved()) {
                                last = rtx.getNodeKey();
                            }
                            rtx.moveTo(last);
                        } else {
                            throw new JaxRxException(404, NOTFOUND);
                        }
                        break;
                    case RIGHTSIBLING:
                        if (!rtx.moveToRightSibling().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    case LEFTSIBLING:
                        if (!rtx.moveToLeftSibling().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    // nothing to do;
                    default:
                }
                if (wrapResult) {
                    output.write(BEGINRESULT);
                    final XMLSerializerProperties props = new XMLSerializerProperties();
                    final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), output, props);
                    if (doNodeId) {
                        builder.emitRESTful().emitIDs();
                    }
                    final XMLSerializer serializer = builder.build();
                    serializer.call();
                    output.write(ENDRESULT);
                } else {
                    final XMLSerializerProperties props = new XMLSerializerProperties();
                    final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), output, props);
                    if (doNodeId) {
                        builder.emitRESTful().emitIDs();
                    }
                    final XMLSerializer serializer = builder.build();
                    serializer.call();
                }
            } else {
                throw new JaxRxException(404, NOTFOUND);
            }
        } catch (final SirixException ttExcep) {
            throw new JaxRxException(ttExcep);
        } catch (final IOException ioExcep) {
            throw new JaxRxException(ioExcep);
        } catch (final Exception globExcep) {
            if (globExcep instanceof JaxRxException) {
                // types
                throw (JaxRxException) globExcep;
            } else {
                throw new JaxRxException(globExcep);
            }
        } finally {
            try {
                WorkerHelper.closeRTX(rtx, session, database);
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    } else {
        throw new JaxRxException(404, "Resource does not exist");
    }
}
Also used : XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) IOException(java.io.IOException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) JaxRxException(org.jaxrx.core.JaxRxException) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerProperties(org.sirix.service.xml.serialize.XMLSerializerProperties) NodeReadTrx(org.sirix.api.NodeReadTrx) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 12 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class NodeIdRepresentation method modifyResource.

/**
 * This method is responsible to modify the XML resource, which is addressed
 * through a unique node id.
 *
 * @param resourceName
 *          The name of the database, where the node id belongs to.
 * @param nodeId
 *          The node id.
 * @param newValue
 *          The new value of the node that has to be replaced.
 * @throws JaxRxException
 *           The exception occurred.
 */
public void modifyResource(final String resourceName, final long nodeId, final InputStream newValue) throws JaxRxException {
    synchronized (resourceName) {
        Session session = null;
        Database database = null;
        NodeWriteTrx wtx = null;
        boolean abort = false;
        if (WorkerHelper.checkExistingResource(mStoragePath, resourceName)) {
            try {
                database = Databases.openDatabase(mStoragePath);
                // Creating a new session
                session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
                // Creating a write transaction
                wtx = session.beginNodeWriteTrx();
                if (wtx.moveTo(nodeId).hasMoved()) {
                    final long parentKey = wtx.getParentKey();
                    wtx.remove();
                    wtx.moveTo(parentKey);
                    WorkerHelper.shredInputStream(wtx, newValue, Insert.ASFIRSTCHILD);
                } else {
                    // workerHelper.closeWTX(abort, wtx, session, database);
                    throw new JaxRxException(404, NOTFOUND);
                }
            } catch (final SirixException exc) {
                abort = true;
                throw new JaxRxException(exc);
            } finally {
                try {
                    WorkerHelper.closeWTX(abort, wtx, session, database);
                } catch (final SirixException exce) {
                    throw new JaxRxException(exce);
                }
            }
        } else {
            throw new JaxRxException(404, "Requested resource not found");
        }
    }
}
Also used : Database(org.sirix.api.Database) NodeWriteTrx(org.sirix.api.NodeWriteTrx) SirixException(org.sirix.exception.SirixException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 13 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class SunburstControl method mousePressed.

/**
 * Implements processing mousePressed.
 *
 * @param pEvent
 *          The {@link MouseEvent}.
 *
 * @see processing.core.PApplet#mousePressed
 */
@Override
public void mousePressed(final MouseEvent pEvent) {
    mSunburstGUI.getControlP5().controlWindow.mouseEvent(pEvent);
    mSunburstGUI.zoomMouseEvent(pEvent);
    mSunburstGUI.setShowGUI(mSunburstGUI.getControlP5().getGroup("menu").isOpen());
    if (!mSunburstGUI.isShowGUI() && mSunburstGUI.mDone) {
        boolean doMouseOver = true;
        if (mSunburstGUI.mRevisions != null && mSunburstGUI.mRevisions.isOpen()) {
            doMouseOver = false;
        }
        if (doMouseOver) {
            // Mouse rollover.
            if (!mSunburstGUI.mParent.keyPressed) {
                if (mSunburstGUI.mHitTestIndex != -1) {
                    synchronized (mSunburstGUI) {
                        mHitTestIndex = mSunburstGUI.mHitTestIndex;
                    }
                    mSunburstGUI.pushImg();
                    // Bug in processing's mousbotton, thus used SwingUtilities.
                    if (SwingUtilities.isLeftMouseButton(pEvent) && !mSunburstGUI.mCtrl.isOpen()) {
                        final SunburstContainer container = new SunburstContainer(mSunburstGUI, mModel);
                        if (mSunburstGUI.mUsePruning) {
                            if (mSunburstGUI.mUseDiffView == ViewType.DIFF && mSunburstGUI.mUseDiffView.getValue()) {
                                container.setPruning(Pruning.DIFF);
                            } else {
                                container.setPruning(Pruning.DEPTH);
                            }
                        } else {
                            container.setPruning(Pruning.NO);
                        }
                        container.setMoveDetection(mSunburstGUI.mUseMoveDetection);
                        final SunburstView view = (SunburstView) ((Embedded) mSunburstGUI.mParent).getView();
                        final ViewNotifier notifier = view.getNotifier();
                        final SunburstItem item = mModel.getItem(mHitTestIndex);
                        notifier.getGUI().getReadDB().setKey(item.getKey());
                        if (mSunburstGUI.mUseDiffView == ViewType.DIFF) {
                            LOGWRAPPER.debug("old rev: " + container.getOldRevision());
                            mModel.update(container.setAll(mSunburstGUI.mSelectedRev, item.getDepth(), mSunburstGUI.getModificationWeight()).setNewStartKey(item.getKey()).setDiff(item.getDiff()));
                        } else {
                            mModel.update(container.setNewStartKey(item.getKey()));
                        }
                        refreshed(container, mHitTestIndex);
                    } else if (SwingUtilities.isRightMouseButton(pEvent)) {
                        if (mSunburstGUI.mUseDiffView == ViewType.NODIFF) {
                            try {
                                ((SunburstModel) mModel).popupMenu(pEvent, mSunburstGUI.mCtrl, mHitTestIndex);
                            } catch (final SirixException e) {
                                LOGWRAPPER.error(e.getMessage(), e);
                                JOptionPane.showMessageDialog(mSunburstGUI.mParent, this, "Failed to commit change: " + e.getMessage(), JOptionPane.ERROR_MESSAGE);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ViewNotifier(org.sirix.gui.view.ViewNotifier) SirixException(org.sirix.exception.SirixException)

Example 14 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class SunburstControl method keyReleased.

/**
 * Is getting called from processings keyRealeased-method and implements it.
 *
 * @see processing.core.PApplet#keyReleased()
 */
@Override
public void keyReleased() {
    if (!mSunburstGUI.mXPathField.isActive() && !mSunburstGUI.mCtrl.isOpen()) {
        switch(mSunburstGUI.mParent.key) {
            case 'r':
            case 'R':
                mSunburstGUI.mParent.frameRate(35);
                mSunburstGUI.mRad = 0;
                mSunburstGUI.resetZoom();
                mSunburstGUI.mZoomPanReset = true;
                break;
            case 's':
            case 'S':
                // Save PNG.
                mSunburstGUI.mParent.saveFrame(ViewUtilities.SAVEPATH + ViewUtilities.timestamp() + "_##.png");
                break;
            case 'p':
            case 'P':
                // Save PDF.
                mSunburstGUI.mParent.frameRate(60);
                mSunburstGUI.setSavePDF(true);
                PApplet.println("\n" + "saving to pdf – starting");
                mSunburstGUI.mParent.beginRecord(PConstants.PDF, ViewUtilities.SAVEPATH + ViewUtilities.timestamp() + ".pdf");
                mSunburstGUI.mParent.textMode(PConstants.SHAPE);
                break;
            case 'q':
                // mSunburstGUI.setViewKind(EView.NODIFF);
                ((Embedded) mSunburstGUI.mParent).refreshUpdate();
                break;
            case '\b':
                // Backspace.
                mModel.undo();
                mSunburstGUI.undo();
                final SunburstView view = (SunburstView) ((Embedded) mSunburstGUI.mParent).getView();
                final ViewNotifier notifier = view.getNotifier();
                notifier.getGUI().getReadDB().setKey(mModel.getItem(0).getKey());
                notifyChanges(mModel.subList(0, mModel.getItemsSize()));
                break;
            case '1':
                mSunburstGUI.setMappingMode(1);
                break;
            case '2':
                mSunburstGUI.setMappingMode(2);
                break;
            case '3':
                mSunburstGUI.setMappingMode(3);
                break;
            case 'o':
            case 'O':
                if (mSunburstGUI.mUseDiffView == ViewType.NODIFF) {
                    mSunburstGUI.mUseDiffView.setValue(true);
                    mSunburstGUI.mRevisions = mSunburstGUI.getControlP5().addDropdownList("Compare revision", mSunburstGUI.mParent.width - 250, 100, 100, 120);
                    assert mSunburstGUI.mDb != null;
                    try {
                        for (long i = mSunburstGUI.mDb.getRevisionNumber() + 1, newestRev = mSunburstGUI.mDb.getSession().beginNodeReadTrx().getRevisionNumber(); i <= newestRev; i++) {
                            mSunburstGUI.mRevisions.addItem("Revision " + i, (int) i);
                        }
                    } catch (final SirixException e) {
                        LOGWRAPPER.error(e.getMessage(), e);
                        JOptionPane.showMessageDialog(mSunburstGUI.mParent, this, "Failed to open read transaction: " + e.getMessage(), JOptionPane.ERROR_MESSAGE);
                    }
                }
                break;
            default:
        }
        switch(mSunburstGUI.mParent.key) {
            case '1':
            case '2':
            case '3':
                mSunburstGUI.update(EResetZoomer.YES);
                break;
            case 'm':
            case 'M':
                mSunburstGUI.setShowGUI(mSunburstGUI.getControlP5().getGroup("menu").isOpen());
                mSunburstGUI.setShowGUI(!mSunburstGUI.isShowGUI());
                break;
            default:
        }
        final ControllerGroup<?> menu = mSunburstGUI.getControlP5().getGroup("menu");
        if (mSunburstGUI.isShowGUI()) {
            menu.open();
            mSunburstGUI.getApplet().frameRate(5);
        } else {
            menu.close();
            mSunburstGUI.getApplet().frameRate(40);
        }
        if (mSunburstGUI.mParent.keyCode == PConstants.RIGHT) {
            mSunburstGUI.mRad += 5;
            mSunburstGUI.mRadChanged = true;
        } else if (mSunburstGUI.mParent.keyCode == PConstants.LEFT) {
            mSunburstGUI.mRad -= 5;
            mSunburstGUI.mRadChanged = true;
        }
    }
}
Also used : ViewNotifier(org.sirix.gui.view.ViewNotifier) SirixException(org.sirix.exception.SirixException) Embedded(org.sirix.gui.view.sunburst.SunburstView.Embedded)

Example 15 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class XMLUpdateShredder method main.

/**
 * Main method.
 *
 * @param args input and output files
 */
public static void main(final String[] args) {
    if (args.length != 2) {
        throw new IllegalArgumentException("Usage: XMLShredder input.xml output.tnk");
    }
    LOGWRAPPER.info("Shredding '" + args[0] + "' to '" + args[1] + "' ... ");
    final long time = System.currentTimeMillis();
    final Path target = Paths.get(args[1]);
    try {
        final DatabaseConfiguration config = new DatabaseConfiguration(target);
        Databases.createDatabase(config);
        final Database db = Databases.openDatabase(target);
        db.createResource(new ResourceConfiguration.Builder("shredded", config).build());
        final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
        final XdmNodeWriteTrx wtx = resMgr.beginNodeWriteTrx();
        final XMLEventReader reader = XMLShredder.createFileReader(Paths.get(args[0]));
        final XMLUpdateShredder shredder = new XMLUpdateShredder(wtx, reader, Insert.ASFIRSTCHILD, new File(args[0]), ShredderCommit.COMMIT);
        shredder.call();
        wtx.close();
        resMgr.close();
    } catch (final SirixException | XMLStreamException | IOException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
    LOGWRAPPER.info(" done [" + (System.currentTimeMillis() - time) + "ms].");
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) ResourceManager(org.sirix.api.ResourceManager) SirixIOException(org.sirix.exception.SirixIOException) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) Database(org.sirix.api.Database) XMLEventReader(javax.xml.stream.XMLEventReader) SirixException(org.sirix.exception.SirixException) File(java.io.File)

Aggregations

SirixException (org.sirix.exception.SirixException)74 DocumentException (org.brackit.xquery.xdm.DocumentException)25 IOException (java.io.IOException)18 Database (org.sirix.api.Database)17 JaxRxException (org.jaxrx.core.JaxRxException)14 NodeReadTrx (org.sirix.api.NodeReadTrx)13 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)13 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)12 Session (org.sirix.api.Session)12 ResourceManager (org.sirix.api.ResourceManager)10 WebApplicationException (javax.ws.rs.WebApplicationException)8 Path (java.nio.file.Path)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 QNm (org.brackit.xquery.atomic.QNm)7 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)6 OutputStream (java.io.OutputStream)5 SubtreeListener (org.brackit.xquery.node.parser.SubtreeListener)5 AbstractTemporalNode (org.brackit.xquery.xdm.AbstractTemporalNode)5 StreamingOutput (javax.ws.rs.core.StreamingOutput)4 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)4