Skip to content

Commit

Permalink
Added JSONRPC Hooks - Renamed RenderXXX Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Mar 30, 2020
1 parent 5fb572c commit 1fd7740
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 60 deletions.
114 changes: 74 additions & 40 deletions sources/MVCFramework.ActiveRecord.pas
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ TMVCActiveRecord = class
/// <summary>
/// Called everywhere before persist object into database
/// </summary>
procedure OnValidation; virtual;
procedure OnValidation(const EntityAction: TMVCEntityAction); virtual;

/// <summary>
/// Called just after load the object state from database
Expand Down Expand Up @@ -258,6 +258,7 @@ TMVCActiveRecord = class
constructor Create; overload; virtual;
destructor Destroy; override;
procedure EnsureConnection;
procedure InvalidateConnection(const ReacquireAfterInvalidate: Boolean = false);
/// <summary>
/// Executes an Insert or an Update if primary key is defined or not
/// </summary>
Expand Down Expand Up @@ -310,6 +311,14 @@ TMVCActiveRecordHelper = class helper for TMVCActiveRecord
const Params: array of Variant;
const ParamTypes: array of TFieldType;
const Options: TMVCActiveRecordLoadOptions = []): TObjectList<T>; overload;
class function SelectOne<T: TMVCActiveRecord, constructor>(const SQL: string;
const Params: array of Variant;
const ParamTypes: array of TFieldType;
const Options: TMVCActiveRecordLoadOptions = [];
const RaiseExceptionIfNotFound: Boolean = True): T; overload;
class function SelectOne<T: TMVCActiveRecord, constructor>(const SQL: string;
const Params: array of Variant;
const RaiseExceptionIfNotFound: Boolean = True): T; overload;
class function SelectRQL<T: constructor, TMVCActiveRecord>(const RQL: string; const MaxRecordCount: Integer)
: TObjectList<T>; overload;
class function All<T: TMVCActiveRecord, constructor>: TObjectList<T>; overload;
Expand Down Expand Up @@ -375,6 +384,7 @@ TMVCEntitiesRegistry = class(TInterfacedObject, IMVCEntitiesRegistry)
procedure SetCurrent(const aName: string);
function GetCurrent: TFDConnection;
function GetCurrentBackend: string;
procedure SetDefault;
end;

TMVCConnectionsRepository = class(TInterfacedObject, IMVCActiveRecordConnections)
Expand Down Expand Up @@ -402,6 +412,7 @@ TConnHolder = class
function GetCurrent: TFDConnection;
function GetByName(const aName: string): TFDConnection;
function GetCurrentBackend: string;
procedure SetDefault;
end;

TMVCSQLGenerator = class abstract
Expand Down Expand Up @@ -706,6 +717,11 @@ procedure TMVCConnectionsRepository.SetCurrent(const aName: string);
end;
end;

procedure TMVCConnectionsRepository.SetDefault;
begin
SetCurrent('default');
end;

function ActiveRecordMappingRegistry: IMVCEntitiesRegistry;
begin
if gEntitiesRegistry = nil then
Expand Down Expand Up @@ -812,41 +828,6 @@ function TMVCActiveRecord.ExecNonQuery(const SQL: string; RefreshAutoGenerated:
if (lValue.Kind = tkRecord) then
begin
MapDataSetFieldToNullableRTTIField(lValue, lQry.Fields[0], fPrimaryKey, Self);
// if SameText(lValue.TypeInfo.Name, 'Nullable<System.Integer>') then
// begin
// lInteger :=lQry.FieldByName(fPrimaryKeyFieldName).AsInteger;
// TValue.MakeWithoutCopy(@lInteger, TypeInfo(NullableInteger), lValue);
// //lValue := TValue.From<NullableInteger>(Nullable<Integer>(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
// // if lValue.IsType<NullableInt32> then
// // lValue := TValue.From<NullableInt32>(NullableInt32(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
// end
// else if SameText(lValue.TypeInfo.Name, 'Nullable<System.Int64>') then
// begin
/// / lLargeInt :=lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt;
/// / TValue.MakeWithoutCopy(@lLargeInt, TypeInfo(NullableInt64), lValue);
// lNullableInt64.Value := lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt;
/// / fPrimaryKey.SetValue(Self, TValue.From<Nullable<System.Int64>>(lNullableInt64));
// TValue.Make(@lNullableInt64, TypeInfo(NullableInt64), lOutValue);
// fPrimaryKey.SetValue(Self,3);
// //fPrimaryKey.SetValue(Self,lOutValue);
//
/// / TValue.MakeWithoutCopy(@lNullableInt64, TypeInfo(NullableInt64), lValue);
// //lValue := TValue.From<NullableInt64>(lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt)
// // else if lValue.IsType<NullableInt64> then
// // lValue := TValue.From<NullableInt64>(NullableInt64(lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt))
// end
// else if SameText(lValue.TypeInfo.Name, 'Nullable<System.UInt32>') then
// lValue := lQry.FieldByName(fPrimaryKeyFieldName).AsInteger
/// / else if lValue.IsType<NullableUInt32> then
/// / lValue := TValue.From<NullableUInt32>(NullableUInt32(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
// else if lValue.IsType<NullableUInt64> then
// lValue := TValue.From<NullableUInt64>(NullableUInt64(lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt))
// else if lValue.IsType<NullableInt16> then
// lValue := TValue.From<NullableInt16>(NullableInt16(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
// else if lValue.IsType<NullableUInt16> then
// lValue := TValue.From<NullableUInt16>(NullableUInt16(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
// else
// raise EMVCActiveRecord.Create('Invalid type for primary key');
end
else
begin
Expand Down Expand Up @@ -922,7 +903,12 @@ procedure TMVCActiveRecord.InitTableInfo;
end;

if fTableName = '' then
raise Exception.Create('Cannot find TableNameAttribute');
begin
if [eaCreate, eaUpdate, eaDelete] * fEntityAllowedActions <> [] then
begin
raise Exception.Create('Cannot find TableNameAttribute');
end;
end;

fProps := fRTTIType.GetFields;
for lRTTIField in fProps do
Expand Down Expand Up @@ -975,7 +961,7 @@ procedure TMVCActiveRecord.Insert;
SQL: string;
begin
CheckAction(TMVCEntityAction.eaCreate);
OnValidation;
OnValidation(TMVCEntityAction.eaCreate);
OnBeforeInsert;
OnBeforeInsertOrUpdate;
if fMap.NonTransientFieldsCount = 0 then
Expand Down Expand Up @@ -1259,6 +1245,8 @@ procedure TMVCActiveRecord.Delete;
var
SQL: string;
begin
CheckAction(TMVCEntityAction.eaDelete);
OnValidation(TMVCEntityAction.eaDelete);
OnBeforeDelete;
if not Assigned(fPrimaryKey) then
raise Exception.CreateFmt('Cannot delete %s without a primary key', [ClassName]);
Expand Down Expand Up @@ -1848,11 +1836,20 @@ procedure TMVCActiveRecord.OnBeforeUpdate;
// do nothing
end;

procedure TMVCActiveRecord.OnValidation;
procedure TMVCActiveRecord.OnValidation(const EntityAction: TMVCEntityAction);
begin
// do nothing
end;

procedure TMVCActiveRecord.InvalidateConnection(const ReacquireAfterInvalidate: Boolean = false);
begin
FreeAndNil(fConn);
if ReacquireAfterInvalidate then
begin
EnsureConnection;
end;
end;

class function TMVCActiveRecord.Select(const aClass: TMVCActiveRecordClass; const SQL: string;
const Params: array of Variant): TMVCActiveRecordList;
begin
Expand Down Expand Up @@ -1938,6 +1935,43 @@ class function TMVCActiveRecordHelper.Select<T>(const SQL: string;
end;
end;

class function TMVCActiveRecordHelper.SelectOne<T>(const SQL: string;
const Params: array of Variant; const RaiseExceptionIfNotFound: Boolean): T;
begin
Result := SelectOne<T>(SQL, Params, [], [], RaiseExceptionIfNotFound);
end;

class function TMVCActiveRecordHelper.SelectOne<T>(const SQL: string;
const Params: array of Variant;
const ParamTypes: array of TFieldType;
const Options: TMVCActiveRecordLoadOptions;
const RaiseExceptionIfNotFound: Boolean): T;
var
lDataSet: TDataSet;
lAR: TMVCActiveRecord;
lHandled: Boolean;
lList: TObjectList<T>;
begin
Result := nil;
lList := Select<T>(SQL, Params, ParamTypes, Options);
try
if (lList.Count = 0) then
begin
if RaiseExceptionIfNotFound then
raise EMVCActiveRecordNotFound.Create('Got 0 rows when exactly 1 was expected')
else
Exit(nil);
end;
if lList.Count > 1 then
begin
raise EMVCActiveRecordNotFound.CreateFmt('Got %d rows when exactly 1 was expected', [lList.Count]);
end;
Result := lList.ExtractAt(0);
finally
lList.Free;
end;
end;

class function TMVCActiveRecordHelper.SelectRQL<T>(const RQL: string; const MaxRecordCount: Integer): TObjectList<T>;
var
lAR: TMVCActiveRecord;
Expand Down Expand Up @@ -2172,7 +2206,7 @@ procedure TMVCActiveRecord.Update;
SQL: string;
begin
CheckAction(TMVCEntityAction.eaUpdate);
OnValidation;
OnValidation(TMVCEntityAction.eaUpdate);
OnBeforeUpdate;
OnBeforeInsertOrUpdate;
if fMap.NonTransientFieldsCount = 0 then
Expand Down
2 changes: 1 addition & 1 deletion sources/MVCFramework.JSONRPC.Client.pas
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ procedure JSONRPCExec(const aJSONRPCURL: string; const aJSONRPCRequest: IJSONRPC
lSS.Position := 0;
lHTTP := THTTPClient.Create;
try
lHttpResp := lHTTP.Post('http://localhost:8080/jsonrpc', lSS, nil,
lHttpResp := lHTTP.Post(aJSONRPCURL, lSS, nil,
[TNetHeader.Create('content-type', 'application/json'), TNetHeader.Create('accept', 'application/json')]);
if (lHttpResp.StatusCode <> 204) then
begin
Expand Down
30 changes: 27 additions & 3 deletions sources/MVCFramework.JSONRPC.pas
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ interface
JSONRPC_DATA = 'data';

const
JSONRPC_HOOKS_ON_BEFORE_ROUTING = 'OnBeforeRouting';
JSONRPC_HOOKS_ON_BEFORE_CALL = 'OnBeforeCall';
JSONRPC_HOOKS_ON_BEFORE_SEND_RESPONSE = 'OnBeforeSendResponse';
JSONRPC_HOOKS_ON_BEFORE_ROUTING = 'OnBeforeRoutingHook';
JSONRPC_HOOKS_ON_BEFORE_CALL = 'OnBeforeCallHook';
JSONRPC_HOOKS_ON_BEFORE_SEND_RESPONSE = 'OnBeforeSendResponseHook';
JSONRPC_HOOKS_METHOD_NAMES: array [0 .. 2] of string = (
JSONRPC_HOOKS_ON_BEFORE_ROUTING,
JSONRPC_HOOKS_ON_BEFORE_CALL,
JSONRPC_HOOKS_ON_BEFORE_SEND_RESPONSE
);

{
http://www.jsonrpc.org/historical/json-rpc-over-http.html#response-codes
Expand Down Expand Up @@ -396,6 +401,20 @@ implementation
var
GProxyGeneratorsRegister: TDictionary<string, TJSONRPCProxyGeneratorClass>;

function IsReservedMethodName(const MethodName: string): Boolean;
var
lMethod: string;
begin
Result := False;
for lMethod in JSONRPC_HOOKS_METHOD_NAMES do
begin
if SameText(MethodName, lMethod) then
begin
Exit(True);
end;
end;
end;

procedure AppendTValueToJsonArray(const Value: TValue; const ParamType: TJSONRPCParamDataType;
const JSONArr: TJDOJsonArray);
var
Expand Down Expand Up @@ -724,6 +743,7 @@ function TMVCJSONRPCController.CanBeRemotelyInvoked(
begin
Result := (RTTIMethod.Visibility = mvPublic) and
(RTTIMethod.MethodKind in [mkProcedure, mkFunction]);
Result := Result and not IsReservedMethodName(RTTIMethod.Name);
end;

constructor TMVCJSONRPCController.Create;
Expand Down Expand Up @@ -932,6 +952,10 @@ procedure TMVCJSONRPCController.GetPublishedMethodList;
var
lAtt: MVCDocAttribute;
begin
if IsReservedMethodName(aRTTIMethod.Name) then
begin
Exit;
end;
lAtt := TRTTIUtils.GetAttribute<MVCDocAttribute>(aRTTIMethod);
if Assigned(lAtt) then
begin
Expand Down
Loading

0 comments on commit 1fd7740

Please sign in to comment.