/**
   * Define the definition of a service; this will destroy any existing configuration associated
   * with the serviceid.
   *
   * @param service The service for the newly defined service.
   */
  public void defineService(Service service) {

    if (service.getSpringFile() == null) {
      service.setSpringFile(getServiceBeanName(service.getId()));
    }

    saveServiceDefinition(service.getId());

    // and, create the service bean
    File serviceBeanFile = getServiceBeanXmlFile(service.getId());
    if (getServiceBeanName(service.getId()).equals(service.getSpringFile())
        && !serviceBeanFile.exists()) {

      try {
        generateSpringServiceConfig(
            service.getId(),
            service.getClazz(),
            getDesignServiceType(service.getType()),
            serviceBeanFile,
            this.projectManager.getCurrentProject());
      } catch (JAXBException e) {
        throw new WMRuntimeException(e);
      } catch (IOException e) {
        throw new WMRuntimeException(e);
      }
    }
  }
  private void deleteServiceShallow(String serviceId) throws IOException {
    Folder classesFolder = getProjectWebAppRoot().getFolder("WEB-INF/classes");
    File springConfig = classesFolder.getFile(serviceId + ".spring.xml");
    springConfig.delete();
    File dbProperty = classesFolder.getFile(serviceId + ".properties");
    if (dbProperty.exists()) {
      dbProperty.delete();
    }
    Service service = getService(serviceId);
    if (service != null) {
      com.wavemaker.tools.io.Resource target;
      if (service.getType().equals("JavaService")) {
        target = classesFolder.getFile(StringUtils.classNameToClassFilePath(service.getClazz()));
      } else {
        String packageName = StringUtils.getPackage(service.getClazz());
        target = classesFolder.getFolder(StringUtils.packageToSrcFilePath(packageName));
      }
      target.delete();
    }

    Map<String, Service> serviceDefs = getCurrentServiceDefinitions();
    serviceDefs.remove(serviceId);

    Folder serviceHome = getServiceFolder(serviceId);
    Project project = this.projectManager.getCurrentProject();
    project.deleteFile(serviceHome);
    project.deleteFile(ConfigurationCompiler.getSmdFile(project, serviceId));
  }
  public void saveServiceDefinition(String serviceId) {

    Resource serviceDefFile = getServiceDefXml(serviceId, false);
    Service service = getCurrentServiceDefinition(serviceId);

    Service oldService = null;
    if (serviceId.equals(CommonConstants.SALESFORCE_SERVICE)) {
      // For salesforceService, we need to retain service definitions in
      // the old service definition file.
      oldService = loadServiceDefinition(serviceId);

      if (oldService != null) {
        for (Operation op : oldService.getOperation()) {
          if (isDeletedQuery(op)) {
            this.cfg.removeDeletedSFQueries(op.getName());
            continue;
          }
          service.addOperation(op);
        }

        List<DataObject> dataObjects = service.getDataobjects().getDataobject();
        for (DataObject dobj : oldService.getDataobjects().getDataobject()) {
          if (isDeletedSFDataObject(dobj)) {
            this.cfg.removeDeletedSFDataObjects(dobj.getName());
            continue;
          }
          dataObjects.add(dobj);
        }

        service.setType(oldService.getType());
        service.setCRUDService(oldService.getCRUDService());
      }
    } // salesforce
    try {
      Marshaller marshaller = definitionsContext.createMarshaller();
      marshaller.setProperty("jaxb.formatted.output", true);
      OutputStream outputStream = this.fileSystem.getOutputStream(serviceDefFile);
      try {
        marshaller.marshal(service, outputStream);
      } finally {
        try {
          outputStream.close();
        } catch (IOException e) {
          throw new IllegalStateException("Unable to close service def file", e);
        }
      }
      // XXX MAV-569 should do a real build here, or actually outside
      // this method maybe
      SortedSet<Service> s = new TreeSet<Service>(new ServiceComparator());
      s.add(service);
      generateRuntimeConfiguration(s);
    } catch (JAXBException e) {
      throw new WMRuntimeException(e);
    } catch (IOException e) {
      throw new WMRuntimeException(e);
    } catch (NoSuchMethodException e) {
      throw new WMRuntimeException(e);
    }
  }
  /**
   * Return a list of all services with the specified type.
   *
   * @param type
   * @return
   */
  public SortedSet<Service> getServicesByType(String type) {

    SortedSet<Service> services = getServices();
    SortedSet<Service> ret = new TreeSet<Service>(new ServiceComparator());

    for (Service service : services) {
      if (service.getType().equals(type)) {
        ret.add(service);
      }
    }

    return ret;
  }
  @Override
  public final void doUpgrade(Project project, UpgradeInfo upgradeInfo) {
    this.mgr = DesignTimeUtils.getDesignServiceManager(project);

    for (Service service : this.mgr.getServices()) {
      if (0 == DataServiceType.TYPE_NAME.compareTo(service.getType())) {
        upgrade(service);
      }
    }

    String msg = getUpgradeMsg();

    if (msg != null) {
      upgradeInfo.addVerbose(msg);
    }
  }