iipRequestCreateResource method

void iipRequestCreateResource(
  1. int callback,
  2. int storeId,
  3. int parentId,
  4. DC content,
)

Implementation

void iipRequestCreateResource(
    int callback, int storeId, int parentId, DC content) {
  Warehouse.getById(storeId).then((store) {
    if (store == null) {
      sendError(
          ErrorType.Management, callback, ExceptionCode.StoreNotFound.index);
      return;
    }

    if (!(store is IStore)) {
      sendError(ErrorType.Management, callback,
          ExceptionCode.ResourceIsNotStore.index);
      return;
    }

    // check security
    if (store.instance?.applicable(
            _session as Session, ActionType.CreateResource, null) !=
        Ruling.Allowed) {
      sendError(
          ErrorType.Management, callback, ExceptionCode.CreateDenied.index);
      return;
    }

    Warehouse.getById(parentId).then((parent) {
      // check security

      if (parent != null) if (parent.instance
              ?.applicable(_session as Session, ActionType.AddChild, null) !=
          Ruling.Allowed) {
        sendError(ErrorType.Management, callback,
            ExceptionCode.AddChildDenied.index);
        return;
      }

      int offset = 0;

      var className = content.getString(offset + 1, content[0]);
      offset += 1 + content[0];

      var nameLength = content.getUint16(offset);
      offset += 2;
      var name = content.getString(offset, nameLength);

      var cl = content.getUint32(offset);
      offset += 4;

      var type = null; //Type.getType(className);

      if (type == null) {
        sendError(ErrorType.Management, callback,
            ExceptionCode.ClassNotFound.index);
        return;
      }

      Codec.parseVarArray(content, offset, cl, this).then((parameters) {
        offset += cl;
        cl = content.getUint32(offset);
        Codec.parseStructure(content, offset, cl, this).then((attributes) {
          offset += cl;
          cl = content.length - offset;

          Codec.parseStructure(content, offset, cl, this).then((values) {
            var constructors =
                []; //Type.GetType(className).GetTypeInfo().GetConstructors();

            var matching = constructors.where((x) {
              var ps = x.GetParameters();
              // if (ps.length > 0 && ps.length == parameters.length + 1)
              //   if (ps.Last().ParameterType == typeof(DistributedConnection))
              //     return true;

              return ps.length == parameters.length;
            }).toList();

            var pi = matching[0].getParameters();

            // cast arguments
            //List<dynamic>? args = null;

            if (pi.length > 0) {
              int argsCount = pi.length;
              //args = new List<dynamic>(pi.length);

              if (pi[pi.length - 1].parameterType.runtimeType ==
                  DistributedConnection) {
                //args[--argsCount] = this;
              }

              if (parameters != null) {
                for (int i = 0; i < argsCount && i < parameters.length; i++) {
                  //args[i] = DC.CastConvert(parameters[i], pi[i].ParameterType);
                }
              }
            }

            // create the resource
            IResource? resource =
                null; //Activator.CreateInstance(type, args) as IResource;

            Warehouse.put<IResource>(
                    name, resource as IResource, store, parent)
                .then<dynamic>((ok) {
              sendReply(IIPPacketAction.CreateResource, callback)
                ..addUint32((resource.instance as Instance).id)
                ..done();
            }).error((ex) {
              // send some error
              sendError(ErrorType.Management, callback,
                  ExceptionCode.AddToStoreFailed.index);
            });
          });
        });
      });
    });
  });
}