/*
   * (non-Jsdoc)
   *
   * @see
   * org.talend.designer.codegen.ITalendSynchronizer#getRoutinesFile(org.talend.core.model.properties.RoutineItem)
   */
  @Override
  public IFile getRoutinesFile(Item item) throws SystemException {
    try {
      if (item instanceof BeanItem) {
        BeanItem routineItem = (BeanItem) item;
        ProjectManager projectManager = ProjectManager.getInstance();
        org.talend.core.model.properties.Project project = projectManager.getProject(routineItem);
        IProject iProject =
            ResourcesPlugin.getWorkspace().getRoot().getProject(project.getTechnicalLabel());
        String repositoryPath =
            ERepositoryObjectType.getFolderName(CamelRepositoryNodeType.repositoryBeansType);
        String folderPath =
            RepositoryNodeUtilities.getPath(routineItem.getProperty().getId()).toString();
        String fileName =
            routineItem.getProperty().getLabel()
                + '_'
                + routineItem.getProperty().getVersion()
                + JavaUtils.ITEM_EXTENSION;
        String path = null;
        if (folderPath != null && folderPath.trim().length() > 0) {
          path = repositoryPath + '/' + folderPath + '/' + fileName;
        } else {
          path = repositoryPath + '/' + fileName;
        }

        IFile file = iProject.getFile(path);
        return file;
      }

    } catch (Exception e) {
      throw new SystemException(e);
    }

    return null;
  }
 /*
  * (non-Jsdoc)
  *
  * @see
  * org.talend.designer.codegen.AbstractRoutineSynchronizer#renameBeanClass(org.talend.core.model.properties.Item)
  */
 @Override
 public void renameBeanClass(Item beanItem) {
   if (beanItem == null) {
     return;
   }
   if (beanItem instanceof BeanItem) {
     BeanItem item = (BeanItem) beanItem;
     String routineContent = new String(item.getContent().getInnerContent());
     String label = item.getProperty().getLabel();
     //
     String regexp = "public(\\s)+class(\\s)+\\w+(\\s)+\\{"; // $NON-NLS-1$
     routineContent =
         routineContent.replaceFirst(
             regexp, "public class " + label + " {"); // $NON-NLS-1$//$NON-NLS-2$
     item.getContent().setInnerContent(routineContent.getBytes());
   }
 }
  /*
   * (non-Jsdoc)
   *
   * @see org.talend.designer.codegen.AbstractRoutineSynchronizer#doSyncBean(org.talend.core.model.properties.Item,
   * boolean)
   */
  @Override
  protected void doSyncBean(Item item, boolean copyToTemp) throws SystemException {
    if (item instanceof BeanItem) {
      BeanItem beanItem = (BeanItem) item;
      FileOutputStream fos = null;
      try {
        IFile file = getBeanFile(beanItem);
        if (file == null) {
          return;
        }
        if (beanItem.getProperty().getModificationDate() != null) {
          long modificationItemDate = beanItem.getProperty().getModificationDate().getTime();
          long modificationFileDate = file.getModificationStamp();
          if (modificationItemDate <= modificationFileDate) {
            return;
          }
        } else {
          beanItem.getProperty().setModificationDate(new Date());
        }

        if (copyToTemp) {
          String beanContent = new String(beanItem.getContent().getInnerContent());
          // see 14713
          String version = VersionUtils.getVersion();
          if (beanContent.contains("%GENERATED_LICENSE%")) { // $NON-NLS-1$
            IService service =
                GlobalServiceRegister.getDefault().getService(IBrandingService.class);
            if (service instanceof AbstractBrandingService) {
              String routineHeader =
                  ((AbstractBrandingService) service).getRoutineLicenseHeader(version);
              beanContent =
                  beanContent.replace("%GENERATED_LICENSE%", routineHeader); // $NON-NLS-1$
            }
          } // end
          String label = beanItem.getProperty().getLabel();
          if (!label.equals(ITalendSynchronizer.BEAN_TEMPLATE) && beanContent != null) {
            beanContent = beanContent.replaceAll(ITalendSynchronizer.BEAN_TEMPLATE, label);
            File f = file.getLocation().toFile();
            fos = new FileOutputStream(f);
            fos.write(beanContent.getBytes());
            fos.close();
          }
        }
        file.refreshLocal(1, null);
      } catch (CoreException e) {
        throw new SystemException(e);
      } catch (IOException e) {
        throw new SystemException(e);
      } finally {
        try {
          fos.close();
        } catch (Exception e) {
          // ignore me even if i'm null
        }
      }
    }
  }