Search in sources :

Example 1 with Command

use of com.ramussoft.common.journal.command.Command in project ramus by Vitaliy-Yakovchuk.

the class Runner method recoverySession.

public boolean recoverySession(final String sessionPath, final File sourceFile) {
    Window rFrame = new Window(null);
    String recovering = GlobalResourcesManager.getString("File.Recovering");
    rFrame.add(new Label(MessageFormat.format(recovering, ((sourceFile == null) ? GlobalResourcesManager.getString("Session.NoName") : sourceFile.getName()))));
    rFrame.pack();
    rFrame.setLocationRelativeTo(null);
    rFrame.setVisible(true);
    final String s = sessionPath + File.separator + "source.rms";
    MemoryDatabase database = new MemoryDatabase() {

        @Override
        protected Collection<? extends PluginProvider> getAdditionalSuits() {
            ArrayList<PluginProvider> ps = new ArrayList<PluginProvider>(1);
            initAdditionalPluginSuits(ps);
            return ps;
        }

        @Override
        protected File getFile() {
            File file = new File(s);
            if (file.exists())
                return file;
            return null;
        }

        protected FileIEngineImpl createFileIEngine(PluginFactory factory) throws ClassNotFoundException, ZipException, IOException {
            return new FileIEngineImpl(0, template, factory, sessionPath);
        }
    };
    final Engine engine = database.getEngine(null);
    DirectoryJournalFactory factory = database.getJournalFactory();
    Journal[] journals = factory.loadJournals(database.getJournaledEngine());
    if (journals.length == 0)
        return false;
    boolean exist = false;
    Journal.RedoCallback redoCallback = new Journal.RedoCallback() {

        boolean hadStartUserTransaction;

        @Override
        public boolean execute(Command command) {
            if (command instanceof StartUserTransactionCommand) {
                hadStartUserTransaction = true;
            }
            return hadStartUserTransaction;
        }
    };
    for (Journal journal : journals) {
        try {
            Command command = null;
            while (journal.canRedo()) {
                command = journal.redo(redoCallback);
            }
            if ((journal.getPointer() == 0l) || (command instanceof StopUndoPointCommand)) {
                rFrame.setVisible(false);
                continue;
            } else
                exist = true;
            if (!(command instanceof EndUserTransactionCommand))
                throw new Exception();
        } catch (Exception e) {
            e.printStackTrace();
            while (journal.canUndo()) {
                if (journal.undo() instanceof StartUserTransactionCommand)
                    break;
            }
        }
    }
    if (!exist) {
        try {
            ((FileIEngineImpl) engine.getDeligate()).close();
            for (Journal journal : journals) journal.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    ((FileIEngineImpl) engine.getDeligate()).recoveryStreams();
    engine.setPluginProperty(CORE, "Changed", Boolean.TRUE);
    engine.setActiveBranch(-1l);
    final AccessRules accessor = database.getAccessRules(null);
    rFrame.setVisible(false);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            SplashScreen screen = new SplashScreen() {

                /**
                 */
                private static final long serialVersionUID = -2727237354089088151L;

                @Override
                protected String getImageName() {
                    return Runner.this.getSplashImageName();
                }
            };
            screen.setLocationRelativeTo(null);
            screen.setVisible(true);
            final JFrame frame = openInNewWindows(engine, accessor, sourceFile, true);
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    String recovered = GlobalResourcesManager.getString("File.Recovered");
                    frame.setTitle(frame.getTitle() + " " + recovered);
                }
            });
            screen.setVisible(false);
        }
    };
    recoveredCount++;
    Thread thread = new Thread(runnable);
    thread.start();
    return true;
}
Also used : Label(java.awt.Label) ArrayList(java.util.ArrayList) Journal(com.ramussoft.common.journal.Journal) JFrame(javax.swing.JFrame) SplashScreen(com.ramussoft.gui.common.SplashScreen) SimleGUIPluginFactory(com.ramussoft.gui.core.simple.SimleGUIPluginFactory) GUIPluginFactory(com.ramussoft.gui.core.GUIPluginFactory) AbstractGUIPluginFactory(com.ramussoft.gui.common.AbstractGUIPluginFactory) PluginFactory(com.ramussoft.common.PluginFactory) Engine(com.ramussoft.common.Engine) Window(java.awt.Window) DirectoryJournalFactory(com.ramussoft.common.journal.DirectoryJournalFactory) FileIEngineImpl(com.ramussoft.core.impl.FileIEngineImpl) StopUndoPointCommand(com.ramussoft.common.journal.StopUndoPointCommand) PluginProvider(com.ramussoft.common.PluginProvider) IOException(java.io.IOException) ZipException(java.util.zip.ZipException) FileMinimumVersionException(com.ramussoft.core.impl.FileMinimumVersionException) IOException(java.io.IOException) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand) Command(com.ramussoft.common.journal.command.Command) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand) StopUndoPointCommand(com.ramussoft.common.journal.StopUndoPointCommand) MemoryDatabase(com.ramussoft.database.MemoryDatabase) AccessRules(com.ramussoft.common.AccessRules) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with Command

use of com.ramussoft.common.journal.command.Command in project ramus by Vitaliy-Yakovchuk.

the class Journal method redo.

public Command redo(RedoCallback callback) {
    Command command = null;
    long index = getPointer();
    try {
        command = readNext();
        IEngine engine = command.getEngine().deligate;
        if (callback.execute(command))
            command.redo(engine);
        JournalEvent event = new JournalEvent(this, command, index);
        afterRedo(event);
    } catch (Exception e) {
        try {
            accessFile.seek(index);
        } catch (IOException e1) {
        }
        throw new RuntimeException(e);
    }
    return command;
}
Also used : CreateAttributeCommand(com.ramussoft.common.journal.command.CreateAttributeCommand) UpdateAttributeCommand(com.ramussoft.common.journal.command.UpdateAttributeCommand) DeleteElementCommand(com.ramussoft.common.journal.command.DeleteElementCommand) SetElementQualifierCommand(com.ramussoft.common.journal.command.SetElementQualifierCommand) CreateQualifierCommand(com.ramussoft.common.journal.command.CreateQualifierCommand) DeleteQualifierCommand(com.ramussoft.common.journal.command.DeleteQualifierCommand) TransactionStorageCommand(com.ramussoft.common.journal.command.TransactionStorageCommand) Command(com.ramussoft.common.journal.command.Command) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) DeleteAttributeCommand(com.ramussoft.common.journal.command.DeleteAttributeCommand) FormulaCommand(com.ramussoft.common.journal.command.FormulaCommand) NewBranchCommand(com.ramussoft.common.journal.command.NewBranchCommand) UpdateQualifierCommand(com.ramussoft.common.journal.command.UpdateQualifierCommand) CreateElementCommand(com.ramussoft.common.journal.command.CreateElementCommand) SetStreamCommand(com.ramussoft.common.journal.command.SetStreamCommand) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand) NextCommand(com.ramussoft.common.journal.command.NextCommand) IEngine(com.ramussoft.common.IEngine) JournalEvent(com.ramussoft.common.journal.event.JournalEvent) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with Command

use of com.ramussoft.common.journal.command.Command in project ramus by Vitaliy-Yakovchuk.

the class Journal method rollbackUserTransaction.

@Override
public void rollbackUserTransaction() {
    Command command;
    while (!((command = undo()) instanceof StartUserTransactionCommand)) {
        if (command instanceof EndUserTransactionCommand) {
            redo();
            throw new RuntimeException("Trying to rollback commited transaction");
        }
    }
    ;
    transaction = false;
    try {
        if (accessFile.length() > accessFile.getFilePointer()) {
            accessFile.setLength(accessFile.getFilePointer());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : CreateAttributeCommand(com.ramussoft.common.journal.command.CreateAttributeCommand) UpdateAttributeCommand(com.ramussoft.common.journal.command.UpdateAttributeCommand) DeleteElementCommand(com.ramussoft.common.journal.command.DeleteElementCommand) SetElementQualifierCommand(com.ramussoft.common.journal.command.SetElementQualifierCommand) CreateQualifierCommand(com.ramussoft.common.journal.command.CreateQualifierCommand) DeleteQualifierCommand(com.ramussoft.common.journal.command.DeleteQualifierCommand) TransactionStorageCommand(com.ramussoft.common.journal.command.TransactionStorageCommand) Command(com.ramussoft.common.journal.command.Command) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) DeleteAttributeCommand(com.ramussoft.common.journal.command.DeleteAttributeCommand) FormulaCommand(com.ramussoft.common.journal.command.FormulaCommand) NewBranchCommand(com.ramussoft.common.journal.command.NewBranchCommand) UpdateQualifierCommand(com.ramussoft.common.journal.command.UpdateQualifierCommand) CreateElementCommand(com.ramussoft.common.journal.command.CreateElementCommand) SetStreamCommand(com.ramussoft.common.journal.command.SetStreamCommand) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand) NextCommand(com.ramussoft.common.journal.command.NextCommand) IOException(java.io.IOException) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand)

Example 4 with Command

use of com.ramussoft.common.journal.command.Command in project ramus by Vitaliy-Yakovchuk.

the class Journal method readCommand.

private Command readCommand() throws IOException {
    int engineId = accessFile.read();
    JournaledEngine engine = getEngine(engineId);
    int commandType = accessFile.read();
    Class<? extends Command> clazz = commandsTypeBytes.get(commandType);
    try {
        Constructor<? extends Command> c = clazz.getConstructor(JournaledEngine.class);
        Command command = c.newInstance(engine);
        command.readCommand(accessFile, 2);
        if (command instanceof StopUndoPointCommand)
            minCommandPos = accessFile.getFilePointer();
        return command;
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : CreateAttributeCommand(com.ramussoft.common.journal.command.CreateAttributeCommand) UpdateAttributeCommand(com.ramussoft.common.journal.command.UpdateAttributeCommand) DeleteElementCommand(com.ramussoft.common.journal.command.DeleteElementCommand) SetElementQualifierCommand(com.ramussoft.common.journal.command.SetElementQualifierCommand) CreateQualifierCommand(com.ramussoft.common.journal.command.CreateQualifierCommand) DeleteQualifierCommand(com.ramussoft.common.journal.command.DeleteQualifierCommand) TransactionStorageCommand(com.ramussoft.common.journal.command.TransactionStorageCommand) Command(com.ramussoft.common.journal.command.Command) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) DeleteAttributeCommand(com.ramussoft.common.journal.command.DeleteAttributeCommand) FormulaCommand(com.ramussoft.common.journal.command.FormulaCommand) NewBranchCommand(com.ramussoft.common.journal.command.NewBranchCommand) UpdateQualifierCommand(com.ramussoft.common.journal.command.UpdateQualifierCommand) CreateElementCommand(com.ramussoft.common.journal.command.CreateElementCommand) SetStreamCommand(com.ramussoft.common.journal.command.SetStreamCommand) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand) NextCommand(com.ramussoft.common.journal.command.NextCommand) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with Command

use of com.ramussoft.common.journal.command.Command in project ramus by Vitaliy-Yakovchuk.

the class Journal method undo.

public Command undo() {
    long index = getPointer();
    Command command = null;
    try {
        command = readPrev();
        IEngine engine = command.getEngine().deligate;
        command.undo(engine);
        JournalEvent event = new JournalEvent(this, command, getPointer());
        afterUndo(event);
    } catch (Exception e) {
        try {
            accessFile.seek(index);
        } catch (IOException e1) {
        }
        throw new RuntimeException(e);
    }
    return command;
}
Also used : CreateAttributeCommand(com.ramussoft.common.journal.command.CreateAttributeCommand) UpdateAttributeCommand(com.ramussoft.common.journal.command.UpdateAttributeCommand) DeleteElementCommand(com.ramussoft.common.journal.command.DeleteElementCommand) SetElementQualifierCommand(com.ramussoft.common.journal.command.SetElementQualifierCommand) CreateQualifierCommand(com.ramussoft.common.journal.command.CreateQualifierCommand) DeleteQualifierCommand(com.ramussoft.common.journal.command.DeleteQualifierCommand) TransactionStorageCommand(com.ramussoft.common.journal.command.TransactionStorageCommand) Command(com.ramussoft.common.journal.command.Command) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) DeleteAttributeCommand(com.ramussoft.common.journal.command.DeleteAttributeCommand) FormulaCommand(com.ramussoft.common.journal.command.FormulaCommand) NewBranchCommand(com.ramussoft.common.journal.command.NewBranchCommand) UpdateQualifierCommand(com.ramussoft.common.journal.command.UpdateQualifierCommand) CreateElementCommand(com.ramussoft.common.journal.command.CreateElementCommand) SetStreamCommand(com.ramussoft.common.journal.command.SetStreamCommand) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand) NextCommand(com.ramussoft.common.journal.command.NextCommand) IEngine(com.ramussoft.common.IEngine) JournalEvent(com.ramussoft.common.journal.event.JournalEvent) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Command (com.ramussoft.common.journal.command.Command)10 EndUserTransactionCommand (com.ramussoft.common.journal.command.EndUserTransactionCommand)10 StartUserTransactionCommand (com.ramussoft.common.journal.command.StartUserTransactionCommand)10 CreateAttributeCommand (com.ramussoft.common.journal.command.CreateAttributeCommand)7 CreateElementCommand (com.ramussoft.common.journal.command.CreateElementCommand)7 CreateQualifierCommand (com.ramussoft.common.journal.command.CreateQualifierCommand)7 DeleteAttributeCommand (com.ramussoft.common.journal.command.DeleteAttributeCommand)7 DeleteElementCommand (com.ramussoft.common.journal.command.DeleteElementCommand)7 DeleteQualifierCommand (com.ramussoft.common.journal.command.DeleteQualifierCommand)7 FormulaCommand (com.ramussoft.common.journal.command.FormulaCommand)7 NewBranchCommand (com.ramussoft.common.journal.command.NewBranchCommand)7 NextCommand (com.ramussoft.common.journal.command.NextCommand)7 SetElementQualifierCommand (com.ramussoft.common.journal.command.SetElementQualifierCommand)7 SetStreamCommand (com.ramussoft.common.journal.command.SetStreamCommand)7 TransactionStorageCommand (com.ramussoft.common.journal.command.TransactionStorageCommand)7 UpdateAttributeCommand (com.ramussoft.common.journal.command.UpdateAttributeCommand)7 UpdateQualifierCommand (com.ramussoft.common.journal.command.UpdateQualifierCommand)7 IOException (java.io.IOException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 IEngine (com.ramussoft.common.IEngine)2