344 lines
12 KiB
C#
344 lines
12 KiB
C#
using System;
|
|
using System.Linq;
|
|
using LeafWeb.Core.Entities;
|
|
using LeafWeb.WebCms.Models;
|
|
|
|
namespace LeafWeb.WebCms.Utility
|
|
{
|
|
// NOTE: Make sure to update both Search functions with any changes.
|
|
internal static class QueryFilter
|
|
{
|
|
|
|
public static IQueryable<LeafInput> Search(IQueryable<LeafInput> leafInput, LeafDataQuery query, string currentUserEmail)
|
|
{
|
|
// search functionality
|
|
if (!string.IsNullOrEmpty(query.q))
|
|
{
|
|
leafInput =
|
|
query.q.Split(' ')
|
|
.Select(p => p.Trim())
|
|
.Aggregate(
|
|
leafInput,
|
|
(current, piece) =>
|
|
from li in current
|
|
where li.Name.Contains(piece)
|
|
|| li.Identifier.Contains(piece)
|
|
|| li.SiteId.Contains(piece)
|
|
select li);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.species))
|
|
{
|
|
leafInput =
|
|
from li in leafInput
|
|
where li.LeafInputData.Any(lid => lid.MajorSpecies.Contains(query.species))
|
|
select li;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.siteid))
|
|
{
|
|
leafInput =
|
|
from li in leafInput
|
|
where li.SiteId.Contains(query.siteid)
|
|
select li;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.lat))
|
|
{
|
|
var latitude = int.Parse(query.lat);
|
|
var range = 0;
|
|
if (!string.IsNullOrEmpty(query.latr))
|
|
{
|
|
range = int.Parse(query.latr);
|
|
}
|
|
|
|
leafInput =
|
|
from li in leafInput
|
|
where li.LeafInputData.Any(lid =>
|
|
lid.Site.Latitude >= latitude - range &&
|
|
lid.Site.Latitude <= latitude + range)
|
|
select li;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.lon))
|
|
{
|
|
var longitude = int.Parse(query.lon);
|
|
var range = 0;
|
|
if (!string.IsNullOrEmpty(query.lonr))
|
|
{
|
|
range = int.Parse(query.lonr);
|
|
}
|
|
|
|
leafInput =
|
|
from li in leafInput
|
|
where li.LeafInputData.Any(lid =>
|
|
lid.Site.Longitude >= longitude - range &&
|
|
lid.Site.Longitude <= longitude + range)
|
|
select li;
|
|
}
|
|
|
|
// co2s
|
|
if (!string.IsNullOrEmpty(query.co2s))
|
|
{
|
|
var p = double.Parse(query.co2s);
|
|
|
|
leafInput =
|
|
from li in leafInput
|
|
where
|
|
// maximum range inside LeafInputFiles
|
|
li.LeafInputData.Max(lid =>
|
|
// range of LeafInputData
|
|
lid.Data.Max(d => d.CO2S)
|
|
- lid.Data.Min(d => d.CO2S))
|
|
>= p
|
|
select li;
|
|
}
|
|
|
|
// pari
|
|
if (!string.IsNullOrEmpty(query.pari))
|
|
{
|
|
var p = double.Parse(query.pari);
|
|
|
|
leafInput =
|
|
from li in leafInput
|
|
where
|
|
// maximum range inside LeafInputFiles
|
|
li.LeafInputData.Max(lid =>
|
|
// range of LeafInputData
|
|
lid.Data.Max(d => d.PARi)
|
|
- lid.Data.Min(d => d.PARi))
|
|
>= p
|
|
select li;
|
|
}
|
|
|
|
// tleaf
|
|
if (!string.IsNullOrEmpty(query.tleaf))
|
|
{
|
|
var p = double.Parse(query.tleaf);
|
|
|
|
leafInput =
|
|
from li in leafInput
|
|
where
|
|
// maximum range inside LeafInputFiles
|
|
li.LeafInputData.Max(lid =>
|
|
// range of LeafInputData
|
|
lid.Data.Max(d => d.Tleaf)
|
|
- lid.Data.Min(d => d.Tleaf))
|
|
>= p
|
|
select li;
|
|
}
|
|
|
|
// phips2
|
|
if (!string.IsNullOrEmpty(query.phips2))
|
|
{
|
|
var p = double.Parse(query.phips2);
|
|
|
|
leafInput =
|
|
from li in leafInput
|
|
where
|
|
// maximum range inside LeafInputFiles
|
|
li.LeafInputData.Max(lid =>
|
|
// range of LeafInputData
|
|
lid.Data.Max(d => d.PhiPS2)
|
|
- lid.Data.Min(d => d.PhiPS2))
|
|
>= p
|
|
select li;
|
|
}
|
|
|
|
if (query.ShowOnlySuccessfullyCompleted)
|
|
{
|
|
leafInput =
|
|
from li in leafInput
|
|
where
|
|
li.CurrentStatus == LeafInputStatusType.Complete
|
|
// didn't have an error message
|
|
&& ! li.OutputFiles.Any(f => f.Filename.Contains(LeafOutputFile.Filename_ErrorMessage))
|
|
select li;
|
|
}
|
|
|
|
if (query.ShowOnlyErred)
|
|
{
|
|
leafInput =
|
|
from li in leafInput
|
|
where
|
|
// has an error message
|
|
li.OutputFiles.Any(f => f.Filename.Contains(LeafOutputFile.Filename_ErrorMessage))
|
|
select li;
|
|
}
|
|
|
|
if (query.OnlyUserData && !string.IsNullOrEmpty(currentUserEmail))
|
|
{
|
|
leafInput =
|
|
from li in leafInput
|
|
where
|
|
li.Email.Equals(currentUserEmail, StringComparison.OrdinalIgnoreCase)
|
|
select li;
|
|
}
|
|
|
|
return leafInput;
|
|
}
|
|
|
|
|
|
public static IQueryable<LeafInputData> Search(IQueryable<LeafInputData> leafInputData, LeafDataQuery query, string currentUserEmail)
|
|
{
|
|
// search functionality
|
|
if (!string.IsNullOrEmpty(query.q))
|
|
{
|
|
leafInputData =
|
|
query.q.Split(' ')
|
|
.Select(p => p.Trim())
|
|
.Aggregate(
|
|
leafInputData,
|
|
(current, piece) =>
|
|
from lid in current
|
|
where lid.LeafInput.Name.Contains(piece)
|
|
|| lid.LeafInput.Identifier.Contains(piece)
|
|
|| lid.LeafInput.SiteId.Contains(piece)
|
|
select lid);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.species))
|
|
{
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where lid.MajorSpecies.Contains(query.species)
|
|
select lid;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.siteid))
|
|
{
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where lid.LeafInput.SiteId.Contains(query.siteid)
|
|
select lid;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.lat))
|
|
{
|
|
var latitude = int.Parse(query.lat);
|
|
var range = 0;
|
|
if (!string.IsNullOrEmpty(query.latr))
|
|
{
|
|
range = int.Parse(query.latr);
|
|
}
|
|
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
lid.Site.Latitude >= latitude - range &&
|
|
lid.Site.Latitude <= latitude + range
|
|
select lid;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.lon))
|
|
{
|
|
var longitude = int.Parse(query.lon);
|
|
var range = 0;
|
|
if (!string.IsNullOrEmpty(query.lonr))
|
|
{
|
|
range = int.Parse(query.lonr);
|
|
}
|
|
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
lid.Site.Longitude >= longitude - range &&
|
|
lid.Site.Longitude <= longitude + range
|
|
select lid;
|
|
}
|
|
|
|
|
|
// co2s
|
|
if (!string.IsNullOrEmpty(query.co2s))
|
|
{
|
|
var p = double.Parse(query.co2s);
|
|
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
// range of LeafInputData
|
|
(lid.Data.Max(d => d.CO2S)
|
|
- lid.Data.Min(d => d.CO2S))
|
|
>= p
|
|
select lid;
|
|
}
|
|
|
|
// pari
|
|
if (!string.IsNullOrEmpty(query.pari))
|
|
{
|
|
var p = double.Parse(query.pari);
|
|
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
// range of LeafInputData
|
|
(lid.Data.Max(d => d.PARi)
|
|
- lid.Data.Min(d => d.PARi))
|
|
>= p
|
|
select lid;
|
|
}
|
|
|
|
// tleaf
|
|
if (!string.IsNullOrEmpty(query.tleaf))
|
|
{
|
|
var p = double.Parse(query.tleaf);
|
|
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
// range of LeafInputData
|
|
(lid.Data.Max(d => d.Tleaf)
|
|
- lid.Data.Min(d => d.Tleaf))
|
|
>= p
|
|
select lid;
|
|
}
|
|
|
|
// phips2
|
|
if (!string.IsNullOrEmpty(query.phips2))
|
|
{
|
|
var p = double.Parse(query.phips2);
|
|
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
// range of LeafInputData
|
|
(lid.Data.Max(d => d.PhiPS2)
|
|
- lid.Data.Min(d => d.PhiPS2))
|
|
>= p
|
|
select lid;
|
|
}
|
|
|
|
if (query.ShowOnlySuccessfullyCompleted)
|
|
{
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
lid.LeafInput.CurrentStatus == LeafInputStatusType.Complete
|
|
// didn't have an error message
|
|
&& !lid.LeafInput.OutputFiles.Any(f => f.Filename.Contains(LeafOutputFile.Filename_ErrorMessage))
|
|
select lid;
|
|
}
|
|
|
|
if (query.ShowOnlyErred)
|
|
{
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
// has an error message
|
|
lid.LeafInput.OutputFiles.Any(f => f.Filename.Contains(LeafOutputFile.Filename_ErrorMessage))
|
|
select lid;
|
|
}
|
|
|
|
if (query.OnlyUserData && !string.IsNullOrEmpty(currentUserEmail))
|
|
{
|
|
leafInputData =
|
|
from lid in leafInputData
|
|
where
|
|
lid.LeafInput.Email.Equals(currentUserEmail, StringComparison.OrdinalIgnoreCase)
|
|
select lid;
|
|
}
|
|
|
|
return leafInputData;
|
|
}
|
|
}
|
|
} |