Hi all,
For performance reasons I'm using a stored procedure to generate availability for an employee. At this moment the stored procedure returns a list of the generated availability or nothing if the availability has been deleted. Because I don't need the list after the stored procedure has executed, I want to know if it is possible to rebuild everything so that the stored procedure doesn't return anything. I found an example of this in java, but is this also possible in NHibernate?
java code:
Code:
Session session = null;
session = HibernateUtil.getSession();
Query query = session.getNamedQuery("ICC.PRC_UNFIT_FILER");
query.setParameter(0, "12345");
query.setParameter(1, new Date());
query.setParameter(2, new Date());
query.setParameter(3, "UNINDSU");
int mylist = query.executeUpdate();
My stored procedure:
Code:
ALTER PROCEDURE [dbo].[USP_GenerateAvailability]
@employeeID int,
@startDate datetime,
@endDate datetime,
@operator int,
@currDate datetime,
@generate int,
@deleteBetween int
AS
BEGIN
DECLARE @dayOfWeek VARCHAR(20)
DECLARE @regimeHours INT
DECLARE @orgStartDate DATETIME
SET NOCOUNT ON;
SET DATEFIRST 1;
SET @orgStartDate = @startDate;
IF (@deleteBetween = 1)
BEGIN
-- delete all existing availability for this period and employee
DELETE FROM AVAILABLE
WHERE PlannedDate BETWEEN @startDate AND @endDate
AND EmployeeID = @employeeID
END
ELSE
BEGIN
-- delete all existing availability from this date on
DELETE FROM AVAILABLE
WHERE PlannedDate > @endDate
AND EmployeeID = @employeeID
END
IF (@generate = 1)
BEGIN
WHILE (@startDate <= @endDate)
BEGIN
--check for weekdays
SET @dayOfWeek = (SELECT upper(DATENAME(WEEKDAY, @startDate)))
IF (@dayOfWeek <> 'SATURDAY' AND @dayOfWeek <> 'SUNDAY')
BEGIN
-- get regimehours
SET @regimeHours = (SELECT top 1 CASE WHEN datepart(weekday, @startDate) = 1 THEN isnull(hoursmonday, 8)
WHEN datepart(weekday, @startDate) = 2 THEN isnull(hourstuesday, 8)
WHEN datepart(weekday, @startDate) = 3 THEN isnull(hourswednesday, 8)
WHEN datepart(weekday, @startDate) = 4 THEN isnull(hoursthursday, 8)
WHEN datepart(weekday, @startDate) = 5 THEN isnull(hoursfriday, 7)
ELSE 8 END as regimehours
FROM Employee e
INNER JOIN EmployeeRegime er ON e.EmployeeID = er.EmployeeID
WHERE e.employeeID = @employeeID
AND er.FromDate <= @startDate
ORDER BY er.FromDate desc )
-- insert availability
INSERT INTO available (EmployeeID, PlannedDate, PlannedHours, DateAdded, AddedBy, DateModified, ModifiedBy)
VALUES(@employeeID, @startDate, @regimeHours, @currDate, @operator, @currDate, @operator)
END
SET @startDate = (SELECT DATEADD(dd,1,@startDate))
END
END
IF (@deleteBetween = 1)
BEGIN
SELECT availableID, employeeID, plannedDate, plannedHours, DateAdded, AddedBy, DateModified, ModifiedBy, PlannedYear, PlannedMonth
FROM dbo.AVAILABLE
WHERE PlannedDate BETWEEN @orgStartDate AND @endDate
AND EmployeeID = @employeeID
END
ELSE
BEGIN
SELECT availableID, employeeID, plannedDate, plannedHours, DateAdded, AddedBy, DateModified, ModifiedBy, PlannedYear, PlannedMonth
FROM dbo.AVAILABLE
WHERE PlannedDate > @endDate
AND EmployeeID = @employeeID
END
END
My NHibernate map file is
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="GenerateAvailability">
<return alias="available" class="Rls.CapacityPlanning.Entities.AvailableState, Rls.CapacityPlanning.Entities">
<return-property name="AvailableID" column="AvailableID"/>
<return-property name="EmployeeID" column="EmployeeID"/>
<return-property name="PlannedDate" column="PlannedDate"/>
<return-property name="PlannedHours" column="PlannedHours"/>
<return-property name="DateAdded" column="DateAdded"/>
<return-property name="AddedBy" column="AddedBy"/>
<return-property name="DateModified" column="DateModified"/>
<return-property name="ModifiedBy" column="ModifiedBy"/>
<return-property name="PlannedYear" column="PlannedYear"/>
<return-property name="PlannedMonth" column="PlannedMonth"/>
</return>
EXEC USP_GenerateAvailability :employeeID, :startDate, :endDate, :operator, :currDate, :generate, :deleteBetween
</sql-query>
</hibernate-mapping>
My code:
Code:
private AvailableList.Dto DoFetch(AvailableList.GeneratedAvailabilityFilter filter)
{
global::NHibernate.ISession session = GetSession();
IQuery query = session.GetNamedQuery("GenerateAvailability");
query.SetParameter("employeeID", filter.EmployeeID);
query.SetParameter("startDate", filter.StartDate);
query.SetParameter("endDate", filter.EndDate);
query.SetParameter("operator", filter.OperatorID);
query.SetParameter("currDate", filter.CurrDate);
query.SetParameter("generate", filter.Generate);
query.SetParameter("deleteBetween", filter.DeleteBetween);
AvailableList.Dto dto = new AvailableList.Dto();
IList<AvailableState> list = query.List<AvailableState>();
foreach (AvailableState state in list)
{
dto.AvailableDtoList.Add(new Available.Dto(state));
}
return dto;
}