Search in sources :

Example 1 with RemoteRepositoryProvider

use of org.omegat.core.team2.RemoteRepositoryProvider in project omegat by omegat-org.

the class ProjectUICommands method projectOpen.

/**
 * Open project. Does nothing if a project is already open and closeCurrent is false.
 *
 * @param projectDirectory
 *            project directory or null if user must choose it
 * @param closeCurrent
 *            whether or not to close the current project first, if any
 */
public static void projectOpen(final File projectDirectory, boolean closeCurrent) {
    UIThreadsUtil.mustBeSwingThread();
    if (Core.getProject().isProjectLoaded()) {
        if (closeCurrent) {
            // Register to try again after closing the current project.
            CoreEvents.registerProjectChangeListener(new IProjectEventListener() {

                public void onProjectChanged(PROJECT_CHANGE_TYPE eventType) {
                    if (eventType == PROJECT_CHANGE_TYPE.CLOSE) {
                        projectOpen(projectDirectory, false);
                        CoreEvents.unregisterProjectChangeListener(this);
                    }
                }
            });
            projectClose();
        }
        return;
    }
    final File projectRootFolder;
    if (projectDirectory == null) {
        // select existing project file - open it
        OmegaTFileChooser pfc = new OpenProjectFileChooser();
        if (OmegaTFileChooser.APPROVE_OPTION != pfc.showOpenDialog(Core.getMainWindow().getApplicationFrame())) {
            return;
        }
        projectRootFolder = pfc.getSelectedFile();
    } else {
        projectRootFolder = projectDirectory;
    }
    new SwingWorker<Void, Void>() {

        protected Void doInBackground() throws Exception {
            IMainWindow mainWindow = Core.getMainWindow();
            Cursor hourglassCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
            Cursor oldCursor = mainWindow.getCursor();
            mainWindow.setCursor(hourglassCursor);
            try {
                // convert old projects if need
                ConvertProject.convert(projectRootFolder);
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_CONVERT_PROJECT");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_CONVERT_PROJECT");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            // check if project okay
            ProjectProperties props;
            try {
                props = ProjectFileStorage.loadProjectProperties(projectRootFolder.getAbsoluteFile());
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            try {
                boolean needToSaveProperties = false;
                if (props.hasRepositories()) {
                    // This is a remote project
                    if (!Core.getParams().containsKey(CLIParameters.NO_TEAM)) {
                        // Save repository mapping
                        List<RepositoryDefinition> repos = props.getRepositories();
                        // Update project.properties
                        mainWindow.showStatusMessageRB("TEAM_OPEN");
                        try {
                            RemoteRepositoryProvider remoteRepositoryProvider = new RemoteRepositoryProvider(props.getProjectRootDir(), props.getRepositories());
                            remoteRepositoryProvider.switchToVersion(OConsts.FILE_PROJECT, null);
                            // Overwrite omegat.project
                            remoteRepositoryProvider.copyFilesFromRepoToProject(OConsts.FILE_PROJECT);
                            // Reload project properties
                            props = ProjectFileStorage.loadProjectProperties(projectRootFolder.getAbsoluteFile());
                            if (props.getRepositories() == null) {
                                // We have a 3.6 style project,
                                // so we restore the mapping we just lost
                                props.setRepositories(repos);
                                needToSaveProperties = true;
                            }
                        } catch (Exception e) {
                            Log.logErrorRB(e, "TF_PROJECT_PROPERTIES_ERROR");
                            Log.log(e);
                            throw new IOException(OStrings.getString("TF_PROJECT_PROPERTIES_ERROR") + "\n" + e.getMessage(), e);
                        }
                    }
                    // team project - non-exist directories could be created from repo
                    props.autocreateDirectories();
                } else {
                    // not a team project - ask for non-exist directories
                    while (!props.isProjectValid()) {
                        needToSaveProperties = true;
                        // something wrong with the project - display open dialog
                        // to fix it
                        ProjectPropertiesDialog prj = new ProjectPropertiesDialog(Core.getMainWindow().getApplicationFrame(), props, new File(projectRootFolder, OConsts.FILE_PROJECT).getAbsolutePath(), ProjectPropertiesDialog.Mode.RESOLVE_DIRS);
                        prj.setVisible(true);
                        props = prj.getResult();
                        prj.dispose();
                        if (props == null) {
                            // user clicks on 'Cancel'
                            mainWindow.setCursor(oldCursor);
                            return null;
                        }
                    }
                }
                final ProjectProperties propsP = props;
                Core.executeExclusively(true, () -> ProjectFactory.loadProject(propsP, true));
                if (needToSaveProperties) {
                    Core.getProject().saveProjectProperties();
                }
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            RecentProjects.add(projectRootFolder.getAbsolutePath());
            mainWindow.setCursor(oldCursor);
            return null;
        }

        protected void done() {
            try {
                get();
                SwingUtilities.invokeLater(Core.getEditor()::requestFocus);
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
            }
        }
    }.execute();
}
Also used : RemoteRepositoryProvider(org.omegat.core.team2.RemoteRepositoryProvider) OmegaTFileChooser(org.omegat.util.gui.OmegaTFileChooser) ProjectProperties(org.omegat.core.data.ProjectProperties) IOException(java.io.IOException) Cursor(java.awt.Cursor) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OpenProjectFileChooser(org.omegat.util.gui.OpenProjectFileChooser) ArrayList(java.util.ArrayList) List(java.util.List) ProjectPropertiesDialog(org.omegat.gui.dialogs.ProjectPropertiesDialog) IProjectEventListener(org.omegat.core.events.IProjectEventListener) File(java.io.File)

Example 2 with RemoteRepositoryProvider

use of org.omegat.core.team2.RemoteRepositoryProvider in project omegat by omegat-org.

the class TestTeamIntegration method prepareRepo.

/**
 * Prepare repository.
 */
static String prepareRepo() throws Exception {
    File tmp = new File(DIR);
    FileUtils.deleteDirectory(tmp);
    if (tmp.exists()) {
        throw new Exception("Impossible to delete test dir");
    }
    tmp.mkdirs();
    File origDir = new File(tmp, "repo");
    origDir.mkdir();
    ProjectProperties config = createConfig(REPO, origDir);
    RemoteRepositoryProvider remote = new RemoteRepositoryProvider(config.getProjectRootDir(), config.getRepositories());
    remote.switchAllToLatest();
    new File(origDir, "omegat").mkdirs();
    File f = new File(origDir, "omegat/project_save.tmx");
    TMXWriter2 wr = new TMXWriter2(f, SRC_LANG, TRG_LANG, true, false, true);
    wr.close();
    ProjectFileStorage.writeProjectFile(config);
    remote.copyFilesFromProjectToRepo("omegat.project", null);
    remote.commitFiles("omegat.project", "Prepare for team test");
    remote.copyFilesFromProjectToRepo("omegat/project_save.tmx", null);
    remote.commitFiles("omegat/project_save.tmx", "Prepare for team test");
    return remote.getVersion("omegat/project_save.tmx");
}
Also used : RemoteRepositoryProvider(org.omegat.core.team2.RemoteRepositoryProvider) TMXWriter2(org.omegat.util.TMXWriter2) File(java.io.File) SVNException(org.tmatesoft.svn.core.SVNException)

Example 3 with RemoteRepositoryProvider

use of org.omegat.core.team2.RemoteRepositoryProvider in project omegat by omegat-org.

the class TestTeamIntegrationChild method main.

public static void main(String[] args) throws Exception {
    if (args.length != 6) {
        System.err.println("Wrong arguments count");
        System.exit(1);
    }
    try {
        source = args[0];
        long time = Long.parseLong(args[1]);
        dir = args[2];
        repo = args[3];
        maxDelaySeconds = Integer.parseInt(args[4]);
        segCount = Integer.parseInt(args[5]);
        finishTime = System.currentTimeMillis() + time;
        TestPreferencesInitializer.init();
        Preferences.setPreference(Preferences.TEAM_AUTHOR, source);
        // Init UI stubs. In a CI environment, errors can occur when
        // initializing the RemoteRepositoryProvider, so we need to make
        // sure the "main window" is available for logging first.
        Core.initializeConsole(Collections.emptyMap());
        TestCoreInitializer.initMainWindow(mainWindow);
        TestCoreInitializer.initAutoSave(autoSave);
        TestCoreInitializer.initEditor(editor);
        ProjectProperties config = TestTeamIntegration.createConfig(repo, new File(dir));
        new RemoteRepositoryProvider(config.getProjectRootDir(), config.getRepositories());
        // load project
        ProjectProperties projectProperties = ProjectFileStorage.loadProjectProperties(new File(dir));
        projectProperties.autocreateDirectories();
        Core.getAutoSave().disable();
        RealProject p = new TestRealProject(projectProperties);
        Core.setProject(p);
        p.loadProject(true);
        if (p.isProjectLoaded()) {
            Core.getAutoSave().enable();
            CoreEvents.fireProjectChange(IProjectEventListener.PROJECT_CHANGE_TYPE.LOAD);
        } else {
            throw new Exception("Project can't be loaded");
        }
        key = new EntryKey[segCount];
        ste = new SourceTextEntry[segCount];
        for (int c = 0; c < segCount; c++) {
            key[c] = new EntryKey("file", source + "/" + c, null, null, null, null);
            ste[c] = new SourceTextEntry(key[c], 0, null, null, new ArrayList<ProtectedPart>());
        }
        keyC = new EntryKey("file", CONCURRENT_NAME, null, null, null, null);
        steC = new SourceTextEntry(keyC, 0, null, null, new ArrayList<ProtectedPart>());
        Random rnd = new Random();
        v = new long[segCount];
        mc: while (true) {
            for (int c = 1; c < segCount; c++) {
                // change concurrent segment
                changeConcurrent();
                if (System.currentTimeMillis() >= finishTime) {
                    break mc;
                }
                // change /0 segment
                Thread.sleep(rnd.nextInt(maxDelaySeconds * 1000) + 10);
                checksavecheck(0);
                // change /1..N segment
                Thread.sleep(rnd.nextInt(maxDelaySeconds * 1000) + 10);
                checksavecheck(c);
            }
        }
        Core.getProject().closeProject();
        // load again and check
        ProjectFactory.loadProject(projectProperties, true);
        checkAll();
        System.exit(200);
    } catch (Throwable ex) {
        ex.printStackTrace();
        System.exit(1);
    }
}
Also used : RemoteRepositoryProvider(org.omegat.core.team2.RemoteRepositoryProvider) ArrayList(java.util.ArrayList) HeadlessException(java.awt.HeadlessException) Random(java.util.Random) File(java.io.File)

Example 4 with RemoteRepositoryProvider

use of org.omegat.core.team2.RemoteRepositoryProvider in project omegat by omegat-org.

the class RealProject method rebaseAndCommitProject.

/**
 * Rebase changes in project to remote HEAD and upload changes to remote if possible.
 * <p>
 * How it works.
 * <p>
 * At each moment we have 3 versions of translation (project_save.tmx file) or writable glossary:
 * <ol>
 * <li>BASE - version which current translator downloaded from remote repository previously(on previous
 * synchronization or startup).
 *
 * <li>WORKING - current version in translator's OmegaT. It doesn't exist it remote repository yet. It's
 * inherited from BASE version, i.e. BASE + local changes.
 *
 * <li>HEAD - latest version in repository, which other translators committed. It's also inherited from
 * BASE version, i.e. BASE + remote changes.
 * </ol>
 * In an ideal world, we could just calculate diff between WORKING and BASE - it will be our local changes
 * after latest synchronization, then rebase these changes on the HEAD revision, then commit into remote
 * repository.
 * <p>
 * But we have some real world limitations:
 * <ul>
 * <li>Computers and networks work slowly, i.e. this synchronization will require some seconds, but
 * translator should be able to edit translation in this time.
 * <li>We have to handle network errors
 * <li>Other translators can commit own data in the same time.
 * </ul>
 * So, in the real world synchronization works by these steps:
 * <ol>
 * <li>Download HEAD revision from remote repository and load it in memory.
 * <li>Load BASE revision from local disk.
 * <li>Calculate diff between WORKING and BASE, then rebase it on the top of HEAD revision. This step
 * synchronized around memory TMX, so, all edits are stopped. Since it's enough fast step, it's okay.
 * <li>Upload new revision into repository.
 * </ol>
 */
private void rebaseAndCommitProject(boolean processGlossary) throws Exception {
    Log.logInfoRB("TEAM_REBASE_START");
    final String author = Preferences.getPreferenceDefault(Preferences.TEAM_AUTHOR, System.getProperty("user.name"));
    final StringBuilder commitDetails = new StringBuilder("Translated by " + author);
    String tmxPath = config.getProjectInternalRelative() + OConsts.STATUS_EXTENSION;
    if (remoteRepositoryProvider.isUnderMapping(tmxPath)) {
        RebaseAndCommit.rebaseAndCommit(tmxPrepared, remoteRepositoryProvider, config.getProjectRootDir(), tmxPath, new RebaseAndCommit.IRebase() {

            ProjectTMX baseTMX, headTMX;

            @Override
            public void parseBaseFile(File file) throws Exception {
                baseTMX = new ProjectTMX(config.getSourceLanguage(), config.getTargetLanguage(), config.isSentenceSegmentingEnabled(), file, null);
            }

            @Override
            public void parseHeadFile(File file) throws Exception {
                headTMX = new ProjectTMX(config.getSourceLanguage(), config.getTargetLanguage(), config.isSentenceSegmentingEnabled(), file, null);
            }

            @Override
            public void rebaseAndSave(File out) throws Exception {
                mergeTMX(baseTMX, headTMX, commitDetails);
                projectTMX.exportTMX(config, out, false, false, true);
            }

            @Override
            public String getCommentForCommit() {
                return commitDetails.toString();
            }

            @Override
            public String getFileCharset(File file) throws Exception {
                return TMXReader2.detectCharset(file);
            }
        });
        if (projectTMX != null) {
            // it can be not loaded yet
            ProjectTMX newTMX = new ProjectTMX(config.getSourceLanguage(), config.getTargetLanguage(), config.isSentenceSegmentingEnabled(), new File(config.getProjectInternalDir(), OConsts.STATUS_EXTENSION), null);
            projectTMX.replaceContent(newTMX);
        }
    }
    if (processGlossary) {
        final String glossaryPath = config.getWritableGlossaryFile().getUnderRoot();
        final File glossaryFile = config.getWritableGlossaryFile().getAsFile();
        new File(config.getProjectRootDir(), glossaryPath);
        if (glossaryPath != null && remoteRepositoryProvider.isUnderMapping(glossaryPath)) {
            final List<GlossaryEntry> glossaryEntries;
            if (glossaryFile.exists()) {
                glossaryEntries = GlossaryReaderTSV.read(glossaryFile, true);
            } else {
                glossaryEntries = Collections.emptyList();
            }
            RebaseAndCommit.rebaseAndCommit(glossaryPrepared, remoteRepositoryProvider, config.getProjectRootDir(), glossaryPath, new RebaseAndCommit.IRebase() {

                List<GlossaryEntry> baseGlossaryEntries, headGlossaryEntries;

                @Override
                public void parseBaseFile(File file) throws Exception {
                    if (file.exists()) {
                        baseGlossaryEntries = GlossaryReaderTSV.read(file, true);
                    } else {
                        baseGlossaryEntries = new ArrayList<GlossaryEntry>();
                    }
                }

                @Override
                public void parseHeadFile(File file) throws Exception {
                    if (file.exists()) {
                        headGlossaryEntries = GlossaryReaderTSV.read(file, true);
                    } else {
                        headGlossaryEntries = new ArrayList<GlossaryEntry>();
                    }
                }

                @Override
                public void rebaseAndSave(File out) throws Exception {
                    List<GlossaryEntry> deltaAddedGlossaryLocal = new ArrayList<GlossaryEntry>(glossaryEntries);
                    deltaAddedGlossaryLocal.removeAll(baseGlossaryEntries);
                    List<GlossaryEntry> deltaRemovedGlossaryLocal = new ArrayList<GlossaryEntry>(baseGlossaryEntries);
                    deltaRemovedGlossaryLocal.removeAll(glossaryEntries);
                    headGlossaryEntries.addAll(deltaAddedGlossaryLocal);
                    headGlossaryEntries.removeAll(deltaRemovedGlossaryLocal);
                    for (GlossaryEntry ge : headGlossaryEntries) {
                        GlossaryReaderTSV.append(out, ge);
                    }
                }

                @Override
                public String getCommentForCommit() {
                    final String author = Preferences.getPreferenceDefault(Preferences.TEAM_AUTHOR, System.getProperty("user.name"));
                    return "Glossary changes by " + author;
                }

                @Override
                public String getFileCharset(File file) throws Exception {
                    return GlossaryReaderTSV.getFileEncoding(file);
                }
            });
        }
    }
    Log.logInfoRB("TEAM_REBASE_END");
}
Also used : RebaseAndCommit(org.omegat.core.team2.RebaseAndCommit) ArrayList(java.util.ArrayList) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) GlossaryEntry(org.omegat.gui.glossary.GlossaryEntry) List(java.util.List) ArrayList(java.util.ArrayList) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 5 with RemoteRepositoryProvider

use of org.omegat.core.team2.RemoteRepositoryProvider in project omegat by omegat-org.

the class ProjectUICommands method projectTeamCreate.

public static void projectTeamCreate() {
    UIThreadsUtil.mustBeSwingThread();
    if (Core.getProject().isProjectLoaded()) {
        return;
    }
    new SwingWorker<Void, Void>() {

        File projectRoot;

        protected Void doInBackground() throws Exception {
            Core.getMainWindow().showStatusMessageRB(null);
            final NewTeamProject dialog = new NewTeamProject(Core.getMainWindow().getApplicationFrame());
            dialog.setVisible(true);
            if (!dialog.ok) {
                Core.getMainWindow().showStatusMessageRB("TEAM_CANCELLED");
                return null;
            }
            IMainWindow mainWindow = Core.getMainWindow();
            Cursor hourglassCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
            Cursor oldCursor = mainWindow.getCursor();
            mainWindow.setCursor(hourglassCursor);
            Core.getMainWindow().showStatusMessageRB("CT_DOWNLOADING_PROJECT");
            // retrieve omegat.project
            projectRoot = new File(dialog.getSaveLocation());
            List<RepositoryDefinition> repos = new ArrayList<RepositoryDefinition>();
            RepositoryDefinition repo = new RepositoryDefinition();
            repos.add(repo);
            repo.setType(dialog.getRepoType());
            repo.setUrl(dialog.getRepoUrl());
            RepositoryMapping mapping = new RepositoryMapping();
            mapping.setLocal("");
            mapping.setRepository("");
            repo.getMapping().add(mapping);
            RemoteRepositoryProvider remoteRepositoryProvider = new RemoteRepositoryProvider(projectRoot, repos);
            remoteRepositoryProvider.switchAllToLatest();
            for (String file : new String[] { OConsts.FILE_PROJECT, OConsts.DEFAULT_INTERNAL + '/' + FilterMaster.FILE_FILTERS, OConsts.DEFAULT_INTERNAL + '/' + SRX.CONF_SENTSEG }) {
                remoteRepositoryProvider.copyFilesFromRepoToProject(file);
            }
            ProjectProperties props = ProjectFileStorage.loadProjectProperties(projectRoot);
            if (props.getRepositories() == null) {
                // We assume it's a 3.6 style project with no repository mapping,
                // so we add root repository mapping
                props.setRepositories(repos);
            }
            // We write in all cases, because we might have added default excludes, for instance
            ProjectFileStorage.writeProjectFile(props);
            // String projectFileURL = dialog.txtRepositoryOrProjectFileURL.getText();
            // File localDirectory = new File(dialog.txtDirectory.getText());
            // try {
            // localDirectory.mkdirs();
            // byte[] projectFile = WikiGet.getURLasByteArray(projectFileURL);
            // FileUtils.writeByteArrayToFile(new File(localDirectory, OConsts.FILE_PROJECT), projectFile);
            // } catch (Exception ex) {
            // ex.printStackTrace();
            // Core.getMainWindow().displayErrorRB(ex, "TEAM_CHECKOUT_ERROR");
            // mainWindow.setCursor(oldCursor);
            // return null;
            // }
            // projectOpen(localDirectory);
            mainWindow.setCursor(oldCursor);
            return null;
        }

        @Override
        protected void done() {
            Core.getMainWindow().showProgressMessage(" ");
            try {
                get();
                if (projectRoot != null) {
                    // don't ask open if user cancelled previous dialog
                    SwingUtilities.invokeLater(() -> {
                        Core.getEditor().requestFocus();
                        projectOpen(projectRoot);
                    });
                }
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_DOWNLOAD_TEAM_PROJECT");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_DOWNLOAD_TEAM_PROJECT");
            }
        }
    }.execute();
}
Also used : RemoteRepositoryProvider(org.omegat.core.team2.RemoteRepositoryProvider) NewTeamProject(org.omegat.gui.dialogs.NewTeamProject) ProjectProperties(org.omegat.core.data.ProjectProperties) Cursor(java.awt.Cursor) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RepositoryDefinition(gen.core.project.RepositoryDefinition) RepositoryMapping(gen.core.project.RepositoryMapping) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Aggregations

File (java.io.File)5 ArrayList (java.util.ArrayList)4 RemoteRepositoryProvider (org.omegat.core.team2.RemoteRepositoryProvider)4 IOException (java.io.IOException)3 List (java.util.List)3 KnownException (org.omegat.core.KnownException)3 Cursor (java.awt.Cursor)2 ExecutionException (java.util.concurrent.ExecutionException)2 ProjectProperties (org.omegat.core.data.ProjectProperties)2 RepositoryDefinition (gen.core.project.RepositoryDefinition)1 RepositoryMapping (gen.core.project.RepositoryMapping)1 HeadlessException (java.awt.HeadlessException)1 RandomAccessFile (java.io.RandomAccessFile)1 Random (java.util.Random)1 IProjectEventListener (org.omegat.core.events.IProjectEventListener)1 RebaseAndCommit (org.omegat.core.team2.RebaseAndCommit)1 NewTeamProject (org.omegat.gui.dialogs.NewTeamProject)1 ProjectPropertiesDialog (org.omegat.gui.dialogs.ProjectPropertiesDialog)1 GlossaryEntry (org.omegat.gui.glossary.GlossaryEntry)1 TMXWriter2 (org.omegat.util.TMXWriter2)1