コード例 #1
0
  public void importItemRecords(
      final IProgressMonitor progressMonitor,
      final ResourcesManager resManager,
      final List<ImportItem> checkedItemRecords,
      final boolean overwrite,
      final ImportItem[] allImportItemRecords,
      final IPath destinationPath)
      throws InvocationTargetException {
    TimeMeasure.display = CommonsPlugin.isDebugMode();
    TimeMeasure.displaySteps = CommonsPlugin.isDebugMode();
    TimeMeasure.measureActive = CommonsPlugin.isDebugMode();
    TimeMeasure.begin("ImportingItems"); // $NON-NLS-1$

    /*
     * Re-order the import items according to the priority of extension point.
     */
    final List<IImportItemsHandler> importItemHandlersList =
        new ArrayList<IImportItemsHandler>(Arrays.asList(getImportHandlers()));
    Collections.sort(
        checkedItemRecords,
        new Comparator<ImportItem>() {

          @Override
          public int compare(ImportItem o1, ImportItem o2) {
            IImportItemsHandler importHandler1 = o1.getImportHandler();
            IImportItemsHandler importHandler2 = o2.getImportHandler();
            if (importHandler1 != null && importHandler2 != null) {
              int index1 = importItemHandlersList.indexOf(importHandler1);
              int index2 = importItemHandlersList.indexOf(importHandler2);
              if (index1 > -1 && index2 > -1) { // both found
                return index1 - index2;
              }
            }
            return 0;
          }
        });

    ImportCacheHelper importCacheHelper = ImportCacheHelper.getInstance();
    try {
      // cache
      importCacheHelper.beforeImportItems();

      if (resManager == null || checkedItemRecords.isEmpty()) {
        return;
      }
      progressMonitor.beginTask(
          Messages.getString("ImportExportHandlersManager_importingItemsMessage"),
          checkedItemRecords.size() * 2 + 1); // $NON-NLS-1$

      /*
       * FIXME ????? why need sort it?
       *
       * Maybe, Have done by priority for import handler, so no need.
       */
      // Collections.sort(itemRecords, new Comparator<ImportItem>() {
      //
      // @Override
      // public int compare(ImportItem o1, ImportItem o2) {
      // if (o1.getProperty().getItem() instanceof RoutineItem && o2.getProperty().getItem()
      // instanceof
      // RoutineItem) {
      // return 0;
      // } else if (!(o1.getProperty().getItem() instanceof RoutineItem)
      // && !(o2.getProperty().getItem() instanceof RoutineItem)) {
      // return 0;
      // } else if (o1.getProperty().getItem() instanceof RoutineItem) {
      // return -1;
      // } else {
      // return 1;
      // }
      // }
      // });

      //

      RepositoryWorkUnit repositoryWorkUnit =
          new RepositoryWorkUnit(
              Messages.getString(
                  "ImportExportHandlersManager_importingItemsMessage")) { //$NON-NLS-1$

            @Override
            public void run() throws PersistenceException {
              final IWorkspaceRunnable op =
                  new IWorkspaceRunnable() {

                    @Override
                    public void run(final IProgressMonitor monitor) throws CoreException {
                      // pre import
                      preImport(
                          monitor,
                          resManager,
                          checkedItemRecords.toArray(new ImportItem[0]),
                          allImportItemRecords);

                      // bug 10520
                      final Set<String> overwriteDeletedItems = new HashSet<String>();
                      final Set<String> idDeletedBeforeImport = new HashSet<String>();

                      Map<String, String> nameToIdMap = new HashMap<String, String>();

                      for (ImportItem itemRecord : checkedItemRecords) {
                        if (monitor.isCanceled()) {
                          return;
                        }
                        if (itemRecord.isValid()) {
                          if (itemRecord.getState() == State.ID_EXISTED) {
                            String id =
                                nameToIdMap.get(
                                    itemRecord.getProperty().getLabel()
                                        + ERepositoryObjectType.getItemType(
                                                itemRecord.getProperty().getItem())
                                            .toString());
                            if (id == null) {
                              /*
                               * if id exsist then need to genrate new id for this job,in this case the
                               * job won't override the old one
                               */
                              id = EcoreUtil.generateUUID();
                              nameToIdMap.put(
                                  itemRecord.getProperty().getLabel()
                                      + ERepositoryObjectType.getItemType(
                                              itemRecord.getProperty().getItem())
                                          .toString(),
                                  id);
                            }
                            itemRecord.getProperty().setId(id);
                          }
                        }
                      }

                      try {
                        importItemRecordsWithRelations(
                            monitor,
                            resManager,
                            checkedItemRecords,
                            overwrite,
                            allImportItemRecords,
                            destinationPath,
                            overwriteDeletedItems,
                            idDeletedBeforeImport);
                      } catch (Exception e) {
                        if (Platform.inDebugMode()) {
                          ExceptionHandler.process(e);
                        }
                        throw new CoreException(
                            new Status(
                                IStatus.ERROR,
                                FrameworkUtil.getBundle(this.getClass()).getSymbolicName(),
                                Messages.getString(
                                    "ImportExportHandlersManager_importingItemsError"),
                                e)); //$NON-NLS-1$
                      }

                      if (PluginChecker.isJobLetPluginLoaded()) {
                        IJobletProviderService service =
                            (IJobletProviderService)
                                GlobalServiceRegister.getDefault()
                                    .getService(IJobletProviderService.class);
                        if (service != null) {
                          service.loadComponentsFromProviders();
                        }
                      }
                      ImportCacheHelper.getInstance().checkDeletedFolders();
                      monitor.done();

                      TimeMeasure.step(
                          "importItemRecords", "before save"); // $NON-NLS-1$ //$NON-NLS-2$

                      if (RelationshipItemBuilder.getInstance().isNeedSaveRelations()) {
                        RelationshipItemBuilder.getInstance().saveRelations();

                        TimeMeasure.step(
                            "importItemRecords", "save relations"); // $NON-NLS-1$ //$NON-NLS-2$
                      } else {
                        // only save the project here if no relation need to be saved, since project
                        // will
                        // already be
                        // saved
                        // with relations
                        try {
                          final IProxyRepositoryFactory factory =
                              CoreRuntimePlugin.getInstance().getProxyRepositoryFactory();
                          factory.saveProject(ProjectManager.getInstance().getCurrentProject());
                        } catch (PersistenceException e) {
                          if (Platform.inDebugMode()) {
                            ExceptionHandler.process(e);
                          }
                          throw new CoreException(
                              new Status(
                                  IStatus.ERROR,
                                  FrameworkUtil.getBundle(this.getClass()).getSymbolicName(),
                                  Messages.getString(
                                      "ImportExportHandlersManager_importingItemsError"),
                                  e)); //$NON-NLS-1$
                        }
                        TimeMeasure.step(
                            "importItemRecords", "save project"); // $NON-NLS-1$//$NON-NLS-2$
                      }

                      // post import
                      List<ImportItem> importedItemRecords =
                          ImportCacheHelper.getInstance().getImportedItemRecords();
                      postImport(
                          monitor, resManager, importedItemRecords.toArray(new ImportItem[0]));
                    }

                    private void importItemRecordsWithRelations(
                        final IProgressMonitor monitor,
                        final ResourcesManager manager,
                        final List<ImportItem> processingItemRecords,
                        final boolean overwriting,
                        ImportItem[] allPopulatedImportItemRecords,
                        IPath destinationPath,
                        final Set<String> overwriteDeletedItems,
                        final Set<String> idDeletedBeforeImport)
                        throws Exception {
                      for (ImportItem itemRecord : processingItemRecords) {
                        if (monitor.isCanceled()) {
                          return;
                        }
                        if (itemRecord.isImported()) {
                          continue; // have imported
                        }
                        try {
                          final IImportItemsHandler importHandler = itemRecord.getImportHandler();
                          if (importHandler != null && itemRecord.isValid()) {
                            List<ImportItem> relatedItemRecord =
                                importHandler.findRelatedImportItems(
                                    monitor, manager, itemRecord, allPopulatedImportItemRecords);
                            // import related items first
                            if (importHandler.isPriorImportRelatedItem()) {
                              if (!relatedItemRecord.isEmpty()) {
                                importItemRecordsWithRelations(
                                    monitor,
                                    manager,
                                    relatedItemRecord,
                                    overwriting,
                                    allPopulatedImportItemRecords,
                                    destinationPath,
                                    overwriteDeletedItems,
                                    idDeletedBeforeImport);
                              }
                            }
                            if (monitor.isCanceled()) {
                              return;
                            }

                            // will import
                            importHandler.doImport(
                                monitor,
                                manager,
                                itemRecord,
                                overwriting,
                                destinationPath,
                                overwriteDeletedItems,
                                idDeletedBeforeImport);

                            if (monitor.isCanceled()) {
                              return;
                            }
                            // if import related items behind current item
                            if (!importHandler.isPriorImportRelatedItem()) {
                              if (!relatedItemRecord.isEmpty()) {
                                importItemRecordsWithRelations(
                                    monitor,
                                    manager,
                                    relatedItemRecord,
                                    overwriting,
                                    allPopulatedImportItemRecords,
                                    destinationPath,
                                    overwriteDeletedItems,
                                    idDeletedBeforeImport);
                              }
                            }

                            importHandler.afterImportingItems(monitor, manager, itemRecord);

                            // record the imported items with related items too.
                            ImportCacheHelper.getInstance()
                                .getImportedItemRecords()
                                .add(itemRecord);

                            monitor.worked(1);
                          }
                        } catch (Exception e) {
                          // ???, PTODO if there one error, need throw error or not.
                          if (Platform.inDebugMode()) {
                            // FIXME, catch the exception, and don't block others to import
                            itemRecord.addError(e.getMessage());
                            // same the the ImportBasicHandler.logError
                            ImportCacheHelper.getInstance().setImportingError(true);
                            ExceptionHandler.process(e);
                          }
                        }
                      }
                    }
                  };
              IWorkspace workspace = ResourcesPlugin.getWorkspace();
              try {
                ISchedulingRule schedulingRule = workspace.getRoot();
                // the update the project files need to be done in the workspace runnable to avoid
                // all
                // notification
                // of changes before the end of the modifications.
                workspace.run(op, schedulingRule, IWorkspace.AVOID_UPDATE, progressMonitor);
              } catch (CoreException e) {
                if (Platform.inDebugMode()) {
                  ExceptionHandler.process(e);
                }
              }
            }
          };
      repositoryWorkUnit.setAvoidUnloadResources(true);
      repositoryWorkUnit.setUnloadResourcesAfterRun(true);
      ProxyRepositoryFactory.getInstance().executeRepositoryWorkUnit(repositoryWorkUnit);

      progressMonitor.done();

      if (ImportCacheHelper.getInstance().hasImportingError()) {
        throw new InvocationTargetException(
            new CoreException(
                new Status(
                    IStatus.ERROR,
                    FrameworkUtil.getBundle(this.getClass()).getSymbolicName(),
                    Messages.getString(
                        "ImportExportHandlersManager_importingItemsError")))); //$NON-NLS-1$
      }
    } finally {
      // cache
      importCacheHelper.afterImportItems();
      //
      TimeMeasure.end("ImportingItems"); // $NON-NLS-1$
      TimeMeasure.display = false;
      TimeMeasure.displaySteps = false;
      TimeMeasure.measureActive = false;
    }
  }
コード例 #2
0
  public List<ImportItem> populateImportingItems(
      ResourcesManager resManager,
      boolean overwrite,
      IProgressMonitor progressMonitor,
      boolean enableProductChecking)
      throws Exception {
    IProgressMonitor monitor = progressMonitor;
    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }
    TimeMeasure.display = CommonsPlugin.isDebugMode();
    TimeMeasure.displaySteps = CommonsPlugin.isDebugMode();
    TimeMeasure.measureActive = CommonsPlugin.isDebugMode();
    TimeMeasure.begin("populateItems"); // $NON-NLS-1$

    try {
      // pre populate
      prePopulate(monitor, resManager);

      ImportCacheHelper.getInstance().beforePopulateItems();

      if (resManager == null) {
        return Collections.emptyList();
      }

      Set<IPath> resPaths = resManager.getPaths();

      monitor.beginTask(
          Messages.getString("ImportExportHandlersManager_populatingItemsMessage"),
          resPaths.size()); // $NON-NLS-1$

      List<ImportItem> items = new ArrayList<ImportItem>();

      ImportHandlerHelper importHandlerHelper = new ImportHandlerHelper();
      for (IPath path : resPaths) {
        if (monitor.isCanceled()) {
          return Collections.emptyList(); //
        }
        ImportItem importItem =
            importHandlerHelper.computeImportItem(monitor, resManager, path, overwrite);
        if (importItem != null) {
          IImportItemsHandler importHandler =
              findValidImportHandler(importItem, enableProductChecking);
          if (importHandler != null) {
            if (importHandler instanceof ImportBasicHandler) {
              // save as the createImportItem of ImportBasicHandler
              ImportBasicHandler importBasicHandler = (ImportBasicHandler) importHandler;
              if (importBasicHandler.checkItem(resManager, importItem, overwrite)) {
                importBasicHandler.checkAndSetProject(resManager, importItem);
              }
            }
          } else {
            // if don't find valid handler, will try to check by noraml path of items, so set null
            // here.
            importItem = null;
          }
        }

        // if can't load rightly via *.properties, try to check another way for normal files.
        if (importItem == null) {
          IImportItemsHandler importHandler =
              findValidImportHandler(resManager, path, enableProductChecking);
          if (importHandler != null) {
            importItem =
                importHandler.createImportItem(progressMonitor, resManager, path, overwrite, items);
          }
        }

        if (importItem != null) {
          items.add(importItem);
        }
        monitor.worked(1);
      }

      // post populate
      postPopulate(monitor, resManager, items.toArray(new ImportItem[0]));

      return items;
    } finally {

      ImportCacheHelper.getInstance().afterPopulateItems();
      //
      TimeMeasure.end("populateItems"); // $NON-NLS-1$
      TimeMeasure.display = false;
      TimeMeasure.displaySteps = false;
      TimeMeasure.measureActive = false;
    }
  }