ScFileProviders.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. unit ScFileProviders;
  2. interface
  3. uses
  4. Windows, Classes, SysUtils, ConstMethodUnit, ADODB, ScFileArchiver, Forms;
  5. type
  6. EScFileProvider = class(Exception);
  7. TScConnection = class(TObject)
  8. private
  9. FRefCount: Integer;
  10. // FFileName: string;
  11. FConnection: TADOConnection;
  12. FFileArchiver: TScMDBArchiver;
  13. FIsNew: Boolean;
  14. FID: Integer;
  15. function GetFileName: string;
  16. public
  17. constructor Create;
  18. destructor Destroy; override;
  19. property ID: Integer read FID;
  20. property RefCount: Integer read FRefCount;
  21. property Connection: TADOConnection read FConnection;
  22. property FileName: string read GetFileName;
  23. property FileArchiver: TScMDBArchiver read FFileArchiver;
  24. property IsNew: Boolean read FIsNew;
  25. end;
  26. TScFileType = (ftProject, ftRationLib, ftFeeRate, ftUnitPrice);
  27. TScArchiverClass = class of TScMDBArchiver;
  28. TScFileProvider = class(TObject)
  29. private
  30. FConnections: TList;
  31. FFileType: TScFileType;
  32. FTemplateFileName: string;
  33. function GetArchiverClass: TScArchiverClass;
  34. function GetConnection(ID: Integer): TScConnection;
  35. function AddConnection(AFileName: string): TScConnection;
  36. function FindConnection(AFileName: string): TScConnection;
  37. procedure SetFileType(const Value: TScFileType);
  38. procedure SetTemplateFileName(const Value: string);
  39. procedure Clear;
  40. function GetNewID: Integer;
  41. public
  42. constructor Create;
  43. destructor Destroy; override;
  44. function New: Integer;
  45. function Open(AFileName: string): Integer;
  46. function Close(AFileName: string): Boolean; overload;
  47. function Close(AID: Integer): Boolean; overload;
  48. function Save(AFileName: string): Boolean; overload;
  49. function Save(AID: Integer): Boolean; overload;
  50. function SaveAs(AFileName: string; ANewFileName: string): Boolean; overload;
  51. function SaveAs(AID: Integer; ANewFileName: string): Boolean; overload;
  52. function Refresh(AID: Integer): Boolean; overload;
  53. function Refresh(AFileName: string): Boolean; overload;
  54. function IndexByName(AFileName: string): TScConnection;
  55. property Connection[ID: Integer]: TScConnection read GetConnection;
  56. property FileType: TScFileType read FFileType write SetFileType;
  57. property TemplateFileName: string read FTemplateFileName write SetTemplateFileName;
  58. end;
  59. var
  60. ProjProvider: TScFileProvider;
  61. FeeRateProvider: TScFileProvider;
  62. UnitPriceProvider: TScFileProvider;
  63. implementation
  64. uses
  65. ScConfig;
  66. { TScConnection }
  67. constructor TScConnection.Create;
  68. begin
  69. FRefCount := 0;
  70. // FFileName := '';
  71. FConnection := nil;
  72. FFileArchiver := nil;
  73. FIsNew := False;
  74. end;
  75. destructor TScConnection.Destroy;
  76. begin
  77. inherited;
  78. end;
  79. function TScConnection.GetFileName: string;
  80. begin
  81. if FFileArchiver <> nil then
  82. Result := FFileArchiver.FileName;
  83. end;
  84. { TScFileProvider }
  85. function TScFileProvider.AddConnection(AFileName: string): TScConnection;
  86. begin
  87. Result := nil;
  88. Result := TScConnection.Create;
  89. Result.FConnection := TADOConnection.Create(nil);
  90. Result.FFileArchiver := GetArchiverClass.Create;
  91. Result.FFileArchiver.Connection := Result.FConnection;
  92. Result.FFileArchiver.FileName := AFileName;
  93. try
  94. if not Result.FFileArchiver.OpenFile then
  95. begin
  96. FreeAndNil(Result);
  97. Exit;
  98. end;
  99. except
  100. Result := nil;
  101. MessageError(0, Format('无法打开文件[%s]!', [AFilename]));
  102. end;
  103. if Result <> nil then
  104. begin
  105. FConnections.Add(Result);
  106. Result.FID := GetNewID + 1;
  107. Inc(Result.FRefCount);
  108. end;
  109. end;
  110. function TScFileProvider.Close(AFileName: string): Boolean;
  111. var
  112. Con: TScConnection;
  113. begin
  114. Result := True;
  115. Con := FindConnection(AFileName);
  116. if Con <> nil then
  117. begin
  118. if Con.RefCount > 1 then
  119. Dec(Con.FRefCount)
  120. else
  121. begin
  122. FConnections.Remove(Con);
  123. Con.FileArchiver.CloseFile;
  124. Con.Connection.Connected := False;
  125. Con.Connection.Free;
  126. Con.FileArchiver.Free;
  127. Con.Free;
  128. end;
  129. end;
  130. end;
  131. function TScFileProvider.Close(AID: Integer): Boolean;
  132. var
  133. Con: TScConnection;
  134. begin
  135. Result := True;
  136. Con := Connection[AID];
  137. if Con <> nil then
  138. begin
  139. if Con.RefCount > 1 then
  140. Dec(Con.FRefCount)
  141. else
  142. begin
  143. FConnections.Remove(Con);
  144. Con.FileArchiver.CloseFile;
  145. Con.Connection.Connected := False;
  146. Con.Connection.Free;
  147. Con.FileArchiver.Free;
  148. Con.Free;
  149. end;
  150. end;
  151. end;
  152. constructor TScFileProvider.Create;
  153. begin
  154. FConnections := TList.Create;
  155. end;
  156. destructor TScFileProvider.Destroy;
  157. begin
  158. Clear;
  159. FConnections.Free;
  160. inherited;
  161. end;
  162. function TScFileProvider.FindConnection(AFileName: string): TScConnection;
  163. var
  164. I: Integer;
  165. begin
  166. Result := nil;
  167. for I := 0 to FConnections.Count - 1 do
  168. begin
  169. if SameText(AFileName, TScConnection(FConnections[I]).FileName) then
  170. begin
  171. Result := TScConnection(FConnections[I]);
  172. Break;
  173. end;
  174. end;
  175. end;
  176. function TScFileProvider.GetConnection(ID: Integer): TScConnection;
  177. var
  178. I: Integer;
  179. begin
  180. Result := nil;
  181. for I := 0 to FConnections.Count - 1 do
  182. begin
  183. if ID = TScConnection(FConnections[I]).FID then
  184. begin
  185. Result := TScConnection(FConnections[I]);
  186. Break;
  187. end;
  188. end;
  189. end;
  190. // 这方法不行,如果某个文件被关闭了,会从List中删除,这时会有错误。
  191. {begin
  192. Result := nil;
  193. if (ID >= 0) and (ID < FConnections.Count) then
  194. begin
  195. Result := TScConnection(FConnections.Items[ID]);
  196. if not (Result is TScConnection) then
  197. Result := nil;
  198. end
  199. else
  200. Result := nil;
  201. end;}
  202. function TScFileProvider.Open(AFileName: string): Integer;
  203. var
  204. Con: TScConnection;
  205. begin
  206. Result := -1;
  207. Con := FindConnection(AFileName);
  208. if Con = nil then
  209. begin
  210. Con := AddConnection(AFileName);
  211. if Con = nil then
  212. begin
  213. Result := -1;
  214. Exit;
  215. end;
  216. Con.Connection.LoginPrompt := False;
  217. Con.Connection.Connected := True;
  218. end
  219. else
  220. Inc(Con.FRefCount);
  221. Result := Con.ID;
  222. end;
  223. function TScFileProvider.Save(AFileName: string): Boolean;
  224. var
  225. Con: TScConnection;
  226. begin
  227. Result := False;
  228. Con := FindConnection(AFileName);
  229. if Con <> nil then
  230. begin
  231. Result := Con.FileArchiver.Save;
  232. end;
  233. end;
  234. function TScFileProvider.Save(AID: Integer): Boolean;
  235. var
  236. Con: TScConnection;
  237. begin
  238. Result := False;
  239. Con := Connection[AID];
  240. if Con <> nil then
  241. begin
  242. Result := Con.FileArchiver.Save;
  243. end;
  244. end;
  245. function TScFileProvider.SaveAs(AFileName: string;
  246. ANewFileName: string): Boolean;
  247. var
  248. Con: TScConnection;
  249. begin
  250. Result := False;
  251. Con := FindConnection(AFileName);
  252. if Con <> nil then
  253. begin
  254. Result := Con.FileArchiver.SaveTo(ANewFileName);
  255. end;
  256. end;
  257. function TScFileProvider.SaveAs(AID: Integer;
  258. ANewFileName: string): Boolean;
  259. var
  260. Con: TScConnection;
  261. begin
  262. Result := False;
  263. Con := Connection[AID];
  264. if Con <> nil then
  265. begin
  266. Result := Con.FileArchiver.SaveTo(ANewFileName);
  267. end;
  268. end;
  269. procedure TScFileProvider.SetFileType(const Value: TScFileType);
  270. begin
  271. FFileType := Value;
  272. end;
  273. function TScFileProvider.GetArchiverClass: TScArchiverClass;
  274. begin
  275. Result := TScMDBArchiver;
  276. case FFileType of
  277. ftProject:
  278. Result := TScProjectFileArchiver;
  279. ftRationLib:
  280. Result := TScRationLibArchiver;
  281. ftFeeRate:
  282. Result := TScFeeRateFileArchiver;
  283. ftUnitPrice:
  284. Result := TScUnitPriceFileArchiver;
  285. end;
  286. end;
  287. function TScFileProvider.New: Integer;
  288. var
  289. Con: TScConnection;
  290. begin
  291. Result := -1;
  292. if not FileExists(FTemplateFileName) then
  293. begin
  294. raise EScFileProvider.Create('文件系统故障,无法创建新文件!');
  295. end;
  296. Con := AddConnection(FTemplateFileName);
  297. Con.Connection.LoginPrompt := False;
  298. Con.Connection.Connected := True;
  299. Result := Con.ID;
  300. end;
  301. procedure TScFileProvider.SetTemplateFileName(const Value: string);
  302. begin
  303. FTemplateFileName := Value;
  304. end;
  305. procedure TScFileProvider.Clear;
  306. var
  307. I: Integer;
  308. Con: TScConnection;
  309. begin
  310. for I := 0 to FConnections.Count - 1 do
  311. begin
  312. Con := TScConnection(FConnections[I]);
  313. if Con <> nil then
  314. begin
  315. Con.FileArchiver.CloseFile;
  316. Con.Connection.Connected := False;
  317. Con.Connection.Free;
  318. Con.FileArchiver.Free;
  319. Con.Free;
  320. end;
  321. end;
  322. FConnections.Clear;
  323. end;
  324. function TScFileProvider.IndexByName(AFileName: string): TScConnection;
  325. begin
  326. Result := FindConnection(AFileName);
  327. end;
  328. function TScFileProvider.Refresh(AID: Integer): Boolean;
  329. var
  330. Con: TScConnection;
  331. begin
  332. Result := False;
  333. Con := Connection[AID];
  334. if Con <> nil then
  335. begin
  336. Result := Con.FileArchiver.Refresh;
  337. end;
  338. end;
  339. function TScFileProvider.Refresh(AFileName: string): Boolean;
  340. var
  341. Con: TScConnection;
  342. begin
  343. Result := False;
  344. Con := FindConnection(AFileName);
  345. if Con <> nil then
  346. begin
  347. Result := Con.FileArchiver.Refresh;
  348. end;
  349. end;
  350. function TScFileProvider.GetNewID: Integer;
  351. var
  352. I, iMaxID: Integer;
  353. begin
  354. iMaxID := 0;
  355. for I := 0 to FConnections.Count - 1 do
  356. begin
  357. if iMaxID < TScConnection(FConnections[I]).ID then
  358. iMaxID := TScConnection(FConnections[I]).ID;
  359. end;
  360. Result := iMaxID;
  361. end;
  362. initialization
  363. ProjProvider := TScFileProvider.Create;
  364. ProjProvider.FFileType := ftProject;
  365. // 这里ConfigInfo还没有加载,不行
  366. // ProjProvider.TemplateFileName := ConfigInfo.ProjFileTemplate;//ExtractFilePath(Application.ExeName) + 'Data\MainTemplate.dat';
  367. FeeRateProvider := TScFileProvider.Create;
  368. FeeRateProvider.FFileType := ftFeeRate;
  369. UnitPriceProvider := TScFileProvider.Create;
  370. UnitPriceProvider.FFileType := ftUnitPrice;
  371. finalization
  372. ProjProvider.Free;
  373. FeeRateProvider.Free;
  374. UnitPriceProvider.Free;
  375. end.