Skip to content

Commit fe7172c

Browse files
committed
Resolves smartstore#829 Activity Log: Searching by customer email out of function
Added deletion of selected customer activity logs Deletion of all activity logs now uses fast and smart truncate
1 parent 934345d commit fe7172c

7 files changed

Lines changed: 305 additions & 190 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
* #796 Selected specification in product filter mask is displayed with default language (not localized)
7878
* #805 Product filter is reset if 'product sorting' or 'view mode' or 'amount of displayed products per page' is changed
7979
* Hide link to a topic page if it is limited to stores
80+
* #829 Activity Log: Searching by customer email out of function
8081

8182

8283
## SmartStore.NET 2.2.2

src/Libraries/SmartStore.Core/Logging/ICustomerActivityService.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3-
using SmartStore.Core;
43
using SmartStore.Core.Domain.Customers;
54
using SmartStore.Core.Domain.Logging;
65

76
namespace SmartStore.Core.Logging
87
{
9-
/// <summary>
10-
/// Customer activity service interface
11-
/// </summary>
12-
public partial interface ICustomerActivityService
8+
/// <summary>
9+
/// Customer activity service interface
10+
/// </summary>
11+
public partial interface ICustomerActivityService
1312
{
1413
/// <summary>
1514
/// Inserts an activity log type item
@@ -84,10 +83,11 @@ ActivityLog InsertActivity(string systemKeyword,
8483
/// <param name="activityLogTypeId">Activity log type identifier</param>
8584
/// <param name="pageIndex">Page index</param>
8685
/// <param name="pageSize">Page size</param>
86+
/// <param name="email">Customer email</param>
8787
/// <returns>Activity log collection</returns>
8888
IPagedList<ActivityLog> GetAllActivities(DateTime? createdOnFrom,
8989
DateTime? createdOnTo, int? customerId,
90-
int activityLogTypeId, int pageIndex, int pageSize);
90+
int activityLogTypeId, int pageIndex, int pageSize, string email = null);
9191

9292
/// <summary>
9393
/// Gets an activity log item
@@ -96,9 +96,16 @@ IPagedList<ActivityLog> GetAllActivities(DateTime? createdOnFrom,
9696
/// <returns>Activity log item</returns>
9797
ActivityLog GetActivityById(int activityLogId);
9898

99-
/// <summary>
100-
/// Clears activity log
101-
/// </summary>
102-
void ClearAllActivities();
99+
/// <summary>
100+
/// Gets activity logs be identifier
101+
/// </summary>
102+
/// <param name="activityLogIds">Activity log identifiers</param>
103+
/// <returns>List of activity logs</returns>
104+
IList<ActivityLog> GetActivityByIds(int[] activityLogIds);
105+
106+
/// <summary>
107+
/// Clears activity log
108+
/// </summary>
109+
void ClearAllActivities();
103110
}
104111
}

src/Libraries/SmartStore.Services/Logging/CustomerActivityService.cs

Lines changed: 93 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,49 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
44
using System.Linq;
55
using SmartStore.Core;
6-
using SmartStore.Core.Caching;
76
using SmartStore.Core.Data;
8-
using SmartStore.Core.Domain.Common;
97
using SmartStore.Core.Domain.Customers;
108
using SmartStore.Core.Domain.Logging;
11-
using SmartStore.Data;
129
using SmartStore.Core.Logging;
1310

1411
namespace SmartStore.Services.Logging
1512
{
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
2017
{
21-
#region Fields
18+
#region Fields
2219

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;
2823
private readonly IRepository<ActivityLogType> _activityLogTypeRepository;
29-
private readonly IWorkContext _workContext;
24+
private readonly IRepository<Customer> _customerRepository;
25+
private readonly IWorkContext _workContext;
3026
private readonly IDbContext _dbContext;
31-
private readonly IDataProvider _dataProvider;
32-
private readonly CommonSettings _commonSettings;
33-
// codehint: sm-add
27+
3428
private readonly static object s_lock = new object();
3529
private readonly static ConcurrentDictionary<string, ActivityLogType> s_logTypes = new ConcurrentDictionary<string, ActivityLogType>();
36-
#endregion
3730

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(
5036
IRepository<ActivityLog> activityLogRepository,
5137
IRepository<ActivityLogType> activityLogTypeRepository,
52-
IWorkContext workContext,
53-
IDbContext dbContext, IDataProvider dataProvider, CommonSettings commonSettings)
38+
IRepository<Customer> customerRepository,
39+
IWorkContext workContext,
40+
IDbContext dbContext)
5441
{
55-
this._cacheManager = cacheManager;
5642
this._activityLogRepository = activityLogRepository;
5743
this._activityLogTypeRepository = activityLogTypeRepository;
44+
this._customerRepository = customerRepository;
5845
this._workContext = workContext;
5946
this._dbContext = dbContext;
60-
this._dataProvider = dataProvider;
61-
this._commonSettings = commonSettings;
6247
}
6348

6449
#endregion
@@ -218,27 +203,41 @@ public virtual void DeleteActivity(ActivityLog activityLog)
218203
_activityLogRepository.Delete(activityLog);
219204
}
220205

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,
232218
DateTime? createdOnTo, int? customerId, int activityLogTypeId,
233-
int pageIndex, int pageSize)
219+
int pageIndex, int pageSize, string email = null)
234220
{
235221
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+
236232
if (createdOnFrom.HasValue)
237233
query = query.Where(al => createdOnFrom.Value <= al.CreatedOnUtc);
234+
238235
if (createdOnTo.HasValue)
239236
query = query.Where(al => createdOnTo.Value >= al.CreatedOnUtc);
237+
240238
if (activityLogTypeId > 0)
241239
query = query.Where(al => activityLogTypeId == al.ActivityLogTypeId);
240+
242241
if (customerId.HasValue)
243242
query = query.Where(al => customerId.Value == al.CustomerId);
244243

@@ -266,29 +265,54 @@ public virtual ActivityLog GetActivityById(int activityLogId)
266265
return activityLog;
267266
}
268267

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>();
278272

273+
var query = _activityLogRepository.Table
274+
.Where(x => activityLogIds.Contains(x.Id))
275+
.OrderByDescending(x => x.CreatedOnUtc);
279276

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+
}
292279

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
293317
}
294318
}

0 commit comments

Comments
 (0)