1

I use standalone MDrivenServer. Question - is it possible to expose a viewmodel as a REST service in this configuration without MDrivenTurnkey installation, e.g. using url like ...Rest/Get?command=vmname&id=rootobjref ? Thank you!

Alex
  • 107
  • 9

1 Answers1

1

No the MDrivenServer has only the persistence mapper api's exposed. In order to simulate a viewmodel driven rest get-service you can go like this in a MVC application:

Derive your controller from ModelDrivenControllerBase<ESType>

/// <summary>
/// command should match a viewmodel, id should be external id for root or $null$
/// will try and match extra params to viewmodel variables
/// will execute actions on root level
/// will return complete vm on success as json except if string attribute RawJSon is found - then this is returned instead
/// </summary>
public virtual ActionResult Get(string command, string id)
{
  Eco.ObjectRepresentation.IModifiableVariableList vars = FindAdditionalRequestParamsAndTreatAsVars();
  if (string.IsNullOrEmpty(id))
    id = ObjectId.nulltoken;
  SaveVariablesToSessionState(command, id, vars);


    VMClass onlinevm = CreateVMClassFromName(command, id);
    if (onlinevm != null)
    {
      if (!CheckRestAllowed(onlinevm))       <-- turnkey checks the rest allowed flag 
        return Content("Must set RestAllowed on VM " + command);

      foreach (var col in onlinevm.ViewModelClass.Columns)
      {
        if (col.IsAction)
        {
          col.ViewModel.ExecuteAction(col.ViewModelClass.RuntimeName, col.RuntimeName);
        }
      }
      return GetJsonFromVm(onlinevm);   <-- this can be implemented with the Tajson concept: https://wiki.mdriven.net/index.php/Tajson
    }
    else
      return Content("Access denied");
}



/// <summary>
/// targetViewRootObject may be both string and IEcoObject. 
/// If the newrootAsObject happens to be a guid string - and the IClass has a property guid - then we will try and PS-resolve the guid and use it as root 
/// </summary>
protected VMClass CreateVMClassFromName(string targetViewName, object targetViewRootObject)
{
  EnsureEcoSpace();

  if (targetViewRootObject is string)
    targetViewRootObject = SafeObjectForId((string)targetViewRootObject);

  VMClass createdVMClass = ViewModelHelper.CreateFromViewModel(targetViewName, EcoSpace, targetViewRootObject as IEcoObject, false);
  IEcoObject root = targetViewRootObject as IEcoObject;
  if (root == null && createdVMClass.ViewModelClass.IClass is IClass && targetViewRootObject is string)
  {
    // Handle special case of a GUID as target
    // Used to act on direct links for an object
    if (targetViewRootObject is string)
    {
      root = EcoSpace.ExternalIds.ObjectForUnkownId(targetViewRootObject as string, createdVMClass.ViewModelClass.IClass as IClass);
      if (root != null)
        createdVMClass.Content = root.AsIObject();
    }

  }
  bool vm_visibleDueToAccessGroups = createdVMClass.ViewModelClass.ViewModel.VisibleDueToAccessGroups(EcoSpace, root != null ? root.AsIObject() : null);
  if (!vm_visibleDueToAccessGroups)
  {
    return null;
  }
  LoadVariablesFromSessionStateIfAvailable(targetViewName, SafeIDForObject(targetViewRootObject), createdVMClass.Variables);
  return createdVMClass;
}
Hans Karlsen
  • 2,275
  • 1
  • 15
  • 15