Which function to use to validate - input year should not be greater than the current year - pega

I have a requirement where the input year should not be greater than the current year. This validation has to be given to the field... (using Integer input field)

Well no such out of the box function present in Pega.
You need to write it on your own.
If you are looking for Edit Validate rule then you can write an edit validate rule with below code.
if (theValue.trim().length() == 0) {
return false;
}
// theValue is by default the value present on the field. Its String by default.
int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR); // get the current year value.
if (Integer.parseInt(theValue) < year){ //convert theValue to int and check if it is less than the current year.
return true;
}
else {
return false;
}
You can write a java function also and call it in Obj Validate rule. If you want to chose the java function option instead of edit validate then that function should accept theValue as parameter.

Related

Serilog Custom Sink Formatting Issue with Serilog LogEventPropertyValue

I'm having to create my own custom sink because none of the ones currently available give me what I need.
Issue I have is when fetching the key/value pair Value from the logEvent message in the Emit Method, the value is wrapped with quotation marks & backslashes.
I've tried converting the out value from the dictionary into a string and then removing the unwanted attributes but nothing is working for me.
Method in my Custom Sink Class:
public void Emit(LogEvent logEvent)
{
var properties = logEvent.Properties;
Serilog.Events.LogEventPropertyValue value;
if (properties.TryGetValue("logEventCategory", out value))
{
// Regex.Replace((value.ToString() ?? "").Replace("'", #"\'").Trim(), #"[\r\n]+", " "); // Not working
var notWorking = value.ToString();
var formattedValueNotWorking = value.ToString().Replace("\r\n", "\\r\\n");
}
}
It just seems that any attempted formatting of the key/value pair Value is ignored: You see that the example string value System is wrapped with a \"System\"
All I want is the actual string, not the backslashes or quotation marks that is wrapped around the string.
Creating my own sink is a hard enough task and I just want to keep things simple, have spent two days trying to understand the wider picture in message formatting but with custom sinks it gets too complicated and bloated coding for what I need. All the other standard message structure attributes are rendering OK, such as message / level / timestamp etc, it's just fine tuning the rendering of the propertie values I require in order to save these values into their own columns in my DB.
You need to unwrap the string from the enclosing ScalarValue:
// using Serilog.Events;
public void Emit(LogEvent logEvent)
{
var properties = logEvent.Properties;
Serilog.Events.LogEventPropertyValue value;
if (properties.TryGetValue("logEventCategory", out value) &&
value is ScalarValue sv &&
sv.Value is string rawValue)
{
// `rawValue` is what you're looking for
Looks like I just needed to use the correct syntax for string replace:
public void Emit(LogEvent logEvent)
{
var properties = logEvent.Properties;
Serilog.Events.LogEventPropertyValue value;
if (properties.TryGetValue("logEventCategory", out value))
{
var formattedValueWorking = value.ToString().Replace("\"", "");
var test = formattedValueWorking;
}
}

Multiple OnSaving event in DevExpress XAF

Im working on a piece of code using DevExpress XAF, I noticed that if im using the event OnSaving that the code executes 2 times, how can i prevent that
protected override void OnSaving()
{
if (PrestamoP != null)
{
PrestamoP.Prestado -= Monto;
PrestamoP.Save();
}
else if (PrestamoG != null)
{
PrestamoG.Prestado -= Monto;
PrestamoG.Save();
}
base.OnSaving();
}
XPO does not guarantee that the OnSaving method is called once. See the corresponding note in the XPO Best Practices article.
I can see that you are changing the PrestamoP.Prestado property based on the value of the Monto property. This code is fine if you execute it only once and only when the Monto property is specified for the first time. This code is not fine if you:
Save this object without changing the Monto property;
Update the early specified Monto value.
So, it appears that a more complex logic is required for the PrestamoG.Prestado property. First, I would move it to the Monto property setter and take the previous value into account (do not forget to check the IsLoading property in this case). Second, I would consider calculating the Prestado value dynamically instead of storing its value. This will allow you to resolve issues with the duplicate business logic execution. See an example here: How to: Calculate a Property Value Based on Values from a Detail Collection.
I can offer different methods for CRUD functions on onSaving method.
IsNewObject, IsDeleted.
// insert
if (Session.IsNewObject(this))
{
a = new a(Session);
a.CreateReferencedProperties(this);
}
// delete
else if (IsDeleted)
{
a= Session.FindObject<A>(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("A=?", this));
if (a!= null)
a.Delete();
}
// update
else
{
a= Session.FindObject<A>(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("A=?", this));
if (a!= null)
a.CreateReferencedProperties(this);
}
You can use the code below to prevent xaf from entering on saving twice.
base.OnSaving();
SessionObjectLayer sessionObjectLayer = this.Session.ObjectLayer as SessionObjectLayer;
if (sessionObjectLayer == null || sessionObjectLayer.ParentSession == null)
{
//Enter only once
}

asp.net date time is not result same

this is the property I have :
[DataType(DataType.Time)]
public TimeSpan StartingTime { get; set; }
now in controller I will check if the starting time is less then now
var now = DateTime.Now.TimeOfDay;
return View(db.SchoolTime.ToList().Where(a => a.StartingTime < now );
here even the starting-time is less then now in database but not returning list to vew
I think it's because Starting-time is lik(09:00:00) but the now is like 09:10:00:12312312
please help thanks in advance
It is not because of the '<' condition.
Try changing the code to:
return View(db.SchoolTime.Where(a => a.StartingTime < now).ToList());
Where condition doesn't get applied unless you enumerate over the collection.
ToList() will help you do that.
Else it will execute once the collection is enumerated in the View.
Use the following code
DateTime parsedDate;
string pattern = ;
DateTime.TryParseExact(StartingTime, "MM-dd-yy", null, DateTimeStyles.None, out parsedDate);
parsedDate.TimeOfDay;// out put in your format MM:HH:SS
Inorder to make the above code to work you need to add using System.Globalization; namespace.
Update 1 :
As per your code
var now = DateTime.Now.TimeOfDay;
returns 00:00:00 every time because it should be assigned when you are creating the DateTime object itself.
So use this
var now = DateTime.Now.ToString("hh:mm:ss")
return View(db.SchoolTime.ToList().Where(a => a.StartingTime < now );

Modified table value

I want to create condition that check other table field in same datasource before modify/update table data.
For example I have table student, field in that table is "status", "name" and "score". Status is enum type and default is "not allow". User can change status field using data grid form.
I want to create condition that Status can change to "allow" if score > 50 else it can't be change. Thanks
As Jan already wrote, one way to solve this would be to overwrite the validateField method of your student table. This method is called every time a field of the table is changed by the user in the form of the table. In the method you could write some code to handle changes to the Status field like e.g.
public boolean validateField(FieldId _fieldId)
{
boolean ret;
ret = super();
switch (_fieldId)
{
case fieldNum(MyStudentTable, Status):
if (this.Status == MyStatus::Allow
&& this.Score <= 50)
{
ret = checkFailed("Score must be greater than 50 to Change the Status."); // TODO create a label
}
break;
}
return ret;
}
What about using validation?
Using validateField or validateWrite on the table or datasource you can test whether the chosen value is valid.
Also the validate method on the datasource or control could be used.
Search the Tables or Forms node of the AOT for thousands of examples.
This answer applies to most "check the value of an entered field".

How to use a parameters value as the property to be selected in a LINQ Query?

I am using LINQ-to-Entities and here is my query
public void SomeFunction(string searchField)
{
var data = from a in dx.SomeTable where
a.SomeProperty="270"
select a;
.
.
.
}
Now if I had to use the value of the parameter "searchField" as the property to be selected in the where clause then how do I do it ?
i.e I want to assign the value of the parameter "searchField" to the property I am checking in the where clause.
So ... the value of "SomeProperty" in a.SomeProperty must be the value of "searchField". How to do this ?
PS :
I dont want a.SomeProperty=searchField.
What I want is "SomeProperty" itself to be replaced by the value of "searchField" and then , this has to be checked to see if its equal to 270.
Scottgu posted how to do this a while ago:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
If the caller knows the type of your a (let's say it's MyType), I would suggest that you don't need dynamic LINQ - you can just pass in a predicate function:
public void SomeFunction(Func<MyType, bool> selector)
{
// ...
var data = from a in dx.SomeTable
where selector(a)
select a;
}
// Calling code
SomeFunction(a => a.SomeProperty == "270");
Or if you want to keep the 270 value within SomeFunction, pass in a projection function to pull out SomeProperty :
public void SomeFunction(Func<MyType, string> propertyExtractor)
{
// ...
var data = from a in dx.SomeTable
where propertyExtractor(a) == "270"
select a;
}
// Calling code
SomeFunction(a => a.SomeProperty);
(apologies if this doesn't compile, am currently away from a compiler)

Resources