public DesignServiceManager() throws JAXBException, IOException {

    this.serviceDefinitions = new HashMap<Project, Map<String, Service>>();

    Unmarshaller unmarshaller = definitionsContext.createUnmarshaller();
    ClassPathResource primitiveServiceFile =
        new ClassPathResource("com/wavemaker/tools/service/primitive_types.xml");
    InputStream inputStream = primitiveServiceFile.getInputStream();
    try {
      Service primitiveService = (Service) unmarshaller.unmarshal(inputStream);
      this.primitiveTypes =
          Collections.unmodifiableList(primitiveService.getDataobjects().getDataobject());

      Map<String, String> m = new TreeMap<String, String>();

      for (DataObject o : this.primitiveTypes) {
        if (!o.isInternal()) {
          m.put(o.getName(), o.getJavaType());
        }
      }

      this.primitivesMap = Collections.unmodifiableMap(m);
    } finally {
      inputStream.close();
    }
  }
  /**
   * 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 the dataobjects specific to the specified service.
   *
   * @param serviceId
   * @return
   */
  public List<DataObject> getLocalDataObjects(String serviceId) {

    Service service = getService(serviceId);
    if (service.getDataobjects() != null) {
      return service.getDataobjects().getDataobject();
    } else {
      return new ArrayList<DataObject>();
    }
  }
  /** Return the operation for the passed name, or null if it doesn't exist. */
  public Operation getOperation(String serviceId, String name) {

    Service service = getCurrentServiceDefinition(serviceId);
    for (Operation operation : service.getOperation()) {
      if (operation.getName().equals(name)) {
        return operation;
      }
    }
    return null;
  }
  /**
   * Return a list of all operations registered with a given service.
   *
   * @param serviceId The service to examine.
   * @return
   */
  public List<String> getOperationNames(String serviceId) {

    Service service = getCurrentServiceDefinition(serviceId);
    List<Operation> operations = service.getOperation();
    List<String> ret = new ArrayList<String>(operations.size());
    for (Operation op : operations) {
      ret.add(op.getName());
    }

    return ret;
  }
  /**
   * 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;
  }
  /**
   * Return data objects specific to this project's services.
   *
   * @return
   */
  public List<DataObject> getLocalDataObjects() {

    Set<DataObject> ret = new HashSet<DataObject>();
    Collection<Service> services = getServices();

    for (Service service : services) {
      if (service.getDataobjects() != null) {
        List<DataObject> serviceObjects = service.getDataobjects().getDataobject();
        ret.addAll(serviceObjects);
      }
    }

    return new ArrayList<DataObject>(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);
    }
  }
  private Service loadServiceDefinition(String serviceId) {

    Service ret = null;

    Resource serviceDefFile = getServiceDefXml(serviceId);
    if (serviceDefFile.exists()) {
      try {
        ret = loadServiceDefinition(serviceDefFile);
        if (ret.getId() == null || "" == ret.getId()) {
          throw new WMRuntimeException(MessageResource.INVALID_SERVICE_DEF_NO_ID, serviceId);
        }
      } catch (JAXBException e) {
        throw new WMRuntimeException(
            MessageResource.ERROR_LOADING_SERVICEDEF, e, serviceId, serviceDefFile, e.getMessage());
      }
    }
    return ret;
  }
  // -----------------------------------------------------------------------
  // internal ops
  // -----------------------------------------------------------------------
  protected void doUpdateOperation(
      ServiceOperation so,
      Service service,
      ServiceDefinition sd,
      IPwsServiceModifier serviceModifier) {

    List<Operation> ops = service.getOperation();

    Operation op = null;
    for (Operation opTemp : ops) {
      if (opTemp.getName().equals(so.getName())) {
        op = opTemp;
        break;
      }
    }
    if (op == null) {
      op = new Operation();
      op.setName(so.getName());
      ops.add(op);
    }

    List<Operation.Parameter> params = op.getParameter();

    if (params.size() > 0) {
      params.clear();
    }

    for (FieldDefinition input : so.getParameterTypes()) {
      Operation.Parameter param = new Operation.Parameter();
      param.setIsList(input.getDimensions() > 0);
      param.setName(input.getName());
      if (input.getTypeDefinition() != null) {
        param.setTypeRef(input.getTypeDefinition().getTypeName());
      }
      params.add(param);
    }

    if (so.getReturnType() != null) {
      Operation.Return ret = new Operation.Return();
      FieldDefinition retType;
      if (serviceModifier == null) {
        retType = so.getReturnType();
      } else {
        retType = serviceModifier.getOperationReturnType(so);
      }

      ret.setIsList(retType.getDimensions() > 0);
      if (retType.getTypeDefinition() != null) {
        ret.setTypeRef(retType.getTypeDefinition().getTypeName());
      }
      op.setReturn(ret);

      op.setOperationType(so.getOperationType());
    }
  }
  /**
   * Generate the appropriate runtime configuration for the current project. Currently,
   * project-services.xml and project-managers.xml are generated.
   *
   * @param services The list of services to update; if null, all services' SMDs are generated.
   * @throws JAXBException
   * @throws IOException
   */
  private void generateRuntimeConfiguration(SortedSet<Service> services)
      throws IOException, NoSuchMethodException {

    Project project = getProjectManager().getCurrentProject();

    if (services == null) {
      services = getServices();
    }
    SortedSet<Service> allServices = getServices();

    try {
      ConfigurationCompiler.generateServices(
          ConfigurationCompiler.getRuntimeServicesXmlFile(project), allServices);
      ConfigurationCompiler.generateManagers(
          ConfigurationCompiler.getRuntimeManagersXmlFile(project), allServices);
    } catch (JAXBException ex) {
      throw new WMRuntimeException(ex);
    }

    ConfigurationCompiler.generateSMDs(project, services);

    // For dsalesforce, add an additional smd for loginService
    if (!services.isEmpty()) {
      Service svc = services.first();
      if (svc.getId().equals(CommonConstants.SALESFORCE_SERVICE)) {
        SortedSet<Service> svcs = new TreeSet<Service>();
        svcs.add(getCurrentServiceDefinition(CommonConstants.SFLOGIN_SERVICE));
        ConfigurationCompiler.generateSMDs(project, svcs);
      }
    }

    ConfigurationCompiler.generateTypes(
        project,
        ConfigurationCompiler.getTypesFile(project),
        allServices,
        getPrimitiveDataObjects());
  }
  /**
   * Update the types associated with a given service. Currently, this will clobber all types
   * associated with the given service.
   *
   * @param service
   * @param serviceDef
   */
  public void updateServiceTypes(
      Service service,
      ServiceDefinition serviceDef,
      String username,
      String password) { // salesforce

    if (service.getDataobjects() == null) {
      service.setDataobjects(new DataObjects());
    }
    List<DataObject> dos = service.getDataobjects().getDataobject();
    dos.clear();

    List<TypeDefinition> elementTypes;
    if (service.getId().equals(CommonConstants.SALESFORCE_SERVICE)) {
      elementTypes = serviceDef.getLocalTypes(username, password);
    } else {
      elementTypes = serviceDef.getLocalTypes();
    }

    for (TypeDefinition et : elementTypes) {
      // No need to check the existence of the types in other services as there is no problem if
      // there are two services using same dataObjects.
      /*if (findDataObjectFromJavaType(et.getTypeName()) != null) {
          throw new WMRuntimeException("Conflicting java type: " + et.getTypeName());
      }*/

      DataObject dso = new DataObject();
      dos.add(dso);

      dso.setJavaType(et.getTypeName());
      dso.setName(et.getShortName());
      dso.setSupportsQuickData(et.isLiveService());

      if (et instanceof ObjectTypeDefinition) {
        ObjectTypeDefinition otd = (ObjectTypeDefinition) et;
        List<Element> dsoelems = dso.getElement();
        for (Map.Entry<String, FieldDefinition> entry : otd.getFields().entrySet()) {

          Element e = new Element();
          dsoelems.add(e);
          if (0 != entry.getKey().compareTo(entry.getValue().getName())) {
            throw new WMRuntimeException(
                "key: " + entry.getKey() + " != " + entry.getValue().getName());
          }

          // key or value.name?
          e.setName(entry.getKey());

          if (entry.getValue().getTypeDefinition() != null) {
            e.setTypeRef(entry.getValue().getTypeDefinition().getTypeName());
          }
          e.setIsList(entry.getValue().getDimensions() > 0);
          e.setAllowNull(entry.getValue().isAllowNull());
          e.setSubType(entry.getValue().getSubType()); // salesforce

          for (OperationEnumeration oe : entry.getValue().getRequire()) {
            e.getRequire().add(oe);
          }
          for (OperationEnumeration oe : entry.getValue().getExclude()) {
            e.getExclude().add(oe);
          }
          for (OperationEnumeration oe : entry.getValue().getNoChange()) {
            e.getNoChange().add(oe);
          }
        }
      }
    }
  }
  /**
   * Define the definition of a service; this will destroy any existing configuration associated
   * with the serviceid.
   *
   * @param serviceDef The service definition for the newly defined service.
   */
  @SuppressWarnings("deprecation")
  public void defineService(ServiceDefinition serviceDef, String username, String password) {

    try {
      validateServiceId(serviceDef.getServiceId());
    } catch (DuplicateServiceIdException ignore) {
      // ignore
    }

    Service oldService = getService(serviceDef.getServiceId());

    Service service = new Service();

    /** Copying the service operations to the new service */
    if (serviceDef instanceof AbstractDeprecatedServiceDefinition) {
      // List<String> opNames =
      // ((AbstractDeprecatedServiceDefinition)serviceDef).getOperationNames();
      if (oldService != null) {
        List<Operation> ops = oldService.getOperation();
        if (ops != null) {
          for (Operation op : ops) {
            // if(opNames != null && opNames.contains(op.getName())){
            service.addOperation(op);
            // }
          }
        }
      }
    }

    getCurrentServiceDefinitions().put(serviceDef.getServiceId(), service);

    IPwsServiceModifier serviceModifier;
    if (this.pwsServiceModifierBeanFactory == null || serviceDef.getPartnerName() == null) {
      serviceModifier = null;
    } else {
      serviceModifier =
          this.pwsServiceModifierBeanFactory.getPwsServiceModifier(serviceDef.getPartnerName());
    }

    try {
      for (ServiceOperation op : serviceDef.getServiceOperations(serviceModifier)) {
        doUpdateOperation(op, service, serviceDef, serviceModifier);
      }
      service.setId(serviceDef.getServiceId());
      service.setType(serviceDef.getServiceType().getTypeName());
      service.setCRUDService(serviceDef.isLiveDataService());
      if (serviceDef instanceof ReflectServiceDefinition) {
        service.setClazz(((ReflectServiceDefinition) serviceDef).getServiceClass());
      }

      if (serviceDef.getRuntimeConfiguration() != null) {
        service.setSpringFile(serviceDef.getRuntimeConfiguration());
      } else if (oldService != null && oldService.getSpringFile() != null) {
        service.setSpringFile(oldService.getSpringFile());
      }

      updateServiceTypes(service, serviceDef, username, password);
    } catch (WMRuntimeException e) {
      // if we had an error, remove the service def so we'll use the last
      // known-good one
      getCurrentServiceDefinitions().remove(serviceDef.getServiceId());
      throw e;
    }

    String svcId = service.getId();
    // TODO: Skipping salesforce setup when null is passed in for username
    // is not good programming practice.
    // TODO: The logic has to be rewritten later when we have time.
    if (svcId.equals(CommonConstants.SALESFORCE_SERVICE) && username != null) { // salesforce
      SalesforceHelper.setupSalesforceSrc(this.projectManager, username, password);
    }

    defineService(service);
  }
 protected DataModelConfiguration getDataModelConfiguration(Service service) {
   // Todo: cftempfix
   return new DataModelConfiguration(getCfgFile(service.getId()));
 }