|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | 2 | using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
4 | 4 | using System.Linq; |
5 | 5 | using SmartStore.Core; |
6 | | -using SmartStore.Core.Caching; |
7 | 6 | using SmartStore.Core.Data; |
8 | | -using SmartStore.Core.Domain.Common; |
9 | 7 | using SmartStore.Core.Domain.Customers; |
10 | 8 | using SmartStore.Core.Domain.Logging; |
11 | | -using SmartStore.Data; |
12 | 9 | using SmartStore.Core.Logging; |
13 | 10 |
|
14 | 11 | namespace SmartStore.Services.Logging |
15 | 12 | { |
16 | | - /// <summary> |
17 | | - /// Customer activity service |
18 | | - /// </summary> |
19 | | - public class CustomerActivityService : ICustomerActivityService |
| 13 | + /// <summary> |
| 14 | + /// Customer activity service |
| 15 | + /// </summary> |
| 16 | + public class CustomerActivityService : ICustomerActivityService |
20 | 17 | { |
21 | | - #region Fields |
| 18 | + #region Fields |
22 | 19 |
|
23 | | - /// <summary> |
24 | | - /// Cache manager |
25 | | - /// </summary> |
26 | | - private readonly ICacheManager _cacheManager; |
27 | | - private readonly IRepository<ActivityLog> _activityLogRepository; |
| 20 | + private const int _deleteNumberOfEntries = 1000; |
| 21 | + |
| 22 | + private readonly IRepository<ActivityLog> _activityLogRepository; |
28 | 23 | private readonly IRepository<ActivityLogType> _activityLogTypeRepository; |
29 | | - private readonly IWorkContext _workContext; |
| 24 | + private readonly IRepository<Customer> _customerRepository; |
| 25 | + private readonly IWorkContext _workContext; |
30 | 26 | private readonly IDbContext _dbContext; |
31 | | - private readonly IDataProvider _dataProvider; |
32 | | - private readonly CommonSettings _commonSettings; |
33 | | - // codehint: sm-add |
| 27 | + |
34 | 28 | private readonly static object s_lock = new object(); |
35 | 29 | private readonly static ConcurrentDictionary<string, ActivityLogType> s_logTypes = new ConcurrentDictionary<string, ActivityLogType>(); |
36 | | - #endregion |
37 | 30 |
|
38 | | - #region Ctor |
39 | | - /// <summary> |
40 | | - /// Ctor |
41 | | - /// </summary> |
42 | | - /// <param name="cacheManager">Cache manager</param> |
43 | | - /// <param name="activityLogRepository">Activity log repository</param> |
44 | | - /// <param name="activityLogTypeRepository">Activity log type repository</param> |
45 | | - /// <param name="workContext">Work context</param> |
46 | | - /// <param name="dbContext">DB context</param>> |
47 | | - /// <param name="dataProvider">WeData provider</param> |
48 | | - /// <param name="commonSettings">Common settings</param> |
49 | | - public CustomerActivityService(ICacheManager cacheManager, |
| 31 | + #endregion |
| 32 | + |
| 33 | + #region Ctor |
| 34 | + |
| 35 | + public CustomerActivityService( |
50 | 36 | IRepository<ActivityLog> activityLogRepository, |
51 | 37 | IRepository<ActivityLogType> activityLogTypeRepository, |
52 | | - IWorkContext workContext, |
53 | | - IDbContext dbContext, IDataProvider dataProvider, CommonSettings commonSettings) |
| 38 | + IRepository<Customer> customerRepository, |
| 39 | + IWorkContext workContext, |
| 40 | + IDbContext dbContext) |
54 | 41 | { |
55 | | - this._cacheManager = cacheManager; |
56 | 42 | this._activityLogRepository = activityLogRepository; |
57 | 43 | this._activityLogTypeRepository = activityLogTypeRepository; |
| 44 | + this._customerRepository = customerRepository; |
58 | 45 | this._workContext = workContext; |
59 | 46 | this._dbContext = dbContext; |
60 | | - this._dataProvider = dataProvider; |
61 | | - this._commonSettings = commonSettings; |
62 | 47 | } |
63 | 48 |
|
64 | 49 | #endregion |
@@ -218,27 +203,41 @@ public virtual void DeleteActivity(ActivityLog activityLog) |
218 | 203 | _activityLogRepository.Delete(activityLog); |
219 | 204 | } |
220 | 205 |
|
221 | | - /// <summary> |
222 | | - /// Gets all activity log items |
223 | | - /// </summary> |
224 | | - /// <param name="createdOnFrom">Log item creation from; null to load all customers</param> |
225 | | - /// <param name="createdOnTo">Log item creation to; null to load all customers</param> |
226 | | - /// <param name="customerId">Customer identifier; null to load all customers</param> |
227 | | - /// <param name="activityLogTypeId">Activity log type identifier</param> |
228 | | - /// <param name="pageIndex">Page index</param> |
229 | | - /// <param name="pageSize">Page size</param> |
230 | | - /// <returns>Activity log collection</returns> |
231 | | - public virtual IPagedList<ActivityLog> GetAllActivities(DateTime? createdOnFrom, |
| 206 | + /// <summary> |
| 207 | + /// Gets all activity log items |
| 208 | + /// </summary> |
| 209 | + /// <param name="createdOnFrom">Log item creation from; null to load all customers</param> |
| 210 | + /// <param name="createdOnTo">Log item creation to; null to load all customers</param> |
| 211 | + /// <param name="customerId">Customer identifier; null to load all customers</param> |
| 212 | + /// <param name="activityLogTypeId">Activity log type identifier</param> |
| 213 | + /// <param name="pageIndex">Page index</param> |
| 214 | + /// <param name="pageSize">Page size</param> |
| 215 | + /// <param name="email">Customer email</param> |
| 216 | + /// <returns>Activity log collection</returns> |
| 217 | + public virtual IPagedList<ActivityLog> GetAllActivities(DateTime? createdOnFrom, |
232 | 218 | DateTime? createdOnTo, int? customerId, int activityLogTypeId, |
233 | | - int pageIndex, int pageSize) |
| 219 | + int pageIndex, int pageSize, string email = null) |
234 | 220 | { |
235 | 221 | var query = _activityLogRepository.Table; |
| 222 | + |
| 223 | + if (email.HasValue()) |
| 224 | + { |
| 225 | + query = |
| 226 | + from al in _activityLogRepository.Table |
| 227 | + join c in _customerRepository.Table on al.CustomerId equals c.Id |
| 228 | + where c.Email == email |
| 229 | + select al; |
| 230 | + } |
| 231 | + |
236 | 232 | if (createdOnFrom.HasValue) |
237 | 233 | query = query.Where(al => createdOnFrom.Value <= al.CreatedOnUtc); |
| 234 | + |
238 | 235 | if (createdOnTo.HasValue) |
239 | 236 | query = query.Where(al => createdOnTo.Value >= al.CreatedOnUtc); |
| 237 | + |
240 | 238 | if (activityLogTypeId > 0) |
241 | 239 | query = query.Where(al => activityLogTypeId == al.ActivityLogTypeId); |
| 240 | + |
242 | 241 | if (customerId.HasValue) |
243 | 242 | query = query.Where(al => customerId.Value == al.CustomerId); |
244 | 243 |
|
@@ -266,29 +265,54 @@ public virtual ActivityLog GetActivityById(int activityLogId) |
266 | 265 | return activityLog; |
267 | 266 | } |
268 | 267 |
|
269 | | - /// <summary> |
270 | | - /// Clears activity log |
271 | | - /// </summary> |
272 | | - public virtual void ClearAllActivities() |
273 | | - { |
274 | | - if (_commonSettings.UseStoredProceduresIfSupported && _dataProvider.StoredProceduresSupported) |
275 | | - { |
276 | | - //although it's not a stored procedure we use it to ensure that a database supports them |
277 | | - //we cannot wait until EF team has it implemented - http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1015357-batch-cud-support |
| 268 | + public virtual IList<ActivityLog> GetActivityByIds(int[] activityLogIds) |
| 269 | + { |
| 270 | + if (activityLogIds == null || activityLogIds.Length == 0) |
| 271 | + return new List<ActivityLog>(); |
278 | 272 |
|
| 273 | + var query = _activityLogRepository.Table |
| 274 | + .Where(x => activityLogIds.Contains(x.Id)) |
| 275 | + .OrderByDescending(x => x.CreatedOnUtc); |
279 | 276 |
|
280 | | - //do all databases support "Truncate command"? |
281 | | - //TODO: do not hard-code the table name |
282 | | - _dbContext.ExecuteSqlCommand("TRUNCATE TABLE [ActivityLog]"); |
283 | | - } |
284 | | - else |
285 | | - { |
286 | | - var activityLog = _activityLogRepository.Table.ToList(); |
287 | | - foreach (var activityLogItem in activityLog) |
288 | | - _activityLogRepository.Delete(activityLogItem); |
289 | | - } |
290 | | - } |
291 | | - #endregion |
| 277 | + return query.ToList(); |
| 278 | + } |
292 | 279 |
|
| 280 | + /// <summary> |
| 281 | + /// Clears activity log |
| 282 | + /// </summary> |
| 283 | + public virtual void ClearAllActivities() |
| 284 | + { |
| 285 | + try |
| 286 | + { |
| 287 | + _dbContext.ExecuteSqlCommand("TRUNCATE TABLE [ActivityLog]"); |
| 288 | + } |
| 289 | + catch |
| 290 | + { |
| 291 | + try |
| 292 | + { |
| 293 | + for (int i = 0; i < 100000; ++i) |
| 294 | + { |
| 295 | + if (_dbContext.ExecuteSqlCommand("Delete Top ({0}) From [ActivityLog]", false, null, _deleteNumberOfEntries) < _deleteNumberOfEntries) |
| 296 | + break; |
| 297 | + } |
| 298 | + } |
| 299 | + catch { } |
| 300 | + |
| 301 | + try |
| 302 | + { |
| 303 | + _dbContext.ExecuteSqlCommand("DBCC CHECKIDENT('ActivityLog', RESEED, 0)"); |
| 304 | + } |
| 305 | + catch |
| 306 | + { |
| 307 | + try |
| 308 | + { |
| 309 | + _dbContext.ExecuteSqlCommand("Alter Table [ActivityLog] Alter Column [Id] Identity(1,1)"); |
| 310 | + } |
| 311 | + catch { } |
| 312 | + } |
| 313 | + } |
| 314 | + } |
| 315 | + |
| 316 | + #endregion |
293 | 317 | } |
294 | 318 | } |
0 commit comments