Getting Compiler Error Expecting '{' but was: '(' at line 1 column 29 for Apex Class - class

I am getting Compile Error: Expecting '{' but was: '(' at line 1 column 29. Have had a look at code and looks find to me:
\
Public class pagedirections (){
String recordId = Apexpages.currentPage().getParameters().get('Id');
Public PageReference urlRedirection(recordId)
{
Case location;
location = [Select ID, Case_TEXT__c From Case Where ID =:recordId LIMIT 1];
if(location.Case_TEXT__c == 'Australia')
PageReference page = new PageReference('https://www.google.com');
return null;
}
}
\\
Any help on this would be great.

In the first line, you have used parenthesis for a class declaration which is the wrong syntax.
Public class pagedirections (){
please remove parenthesis
public class pagedirections{
}

Related

Hibernate 6+ Unable to map java enum to postgres enum

After upgrading to hibernate 6 I am having issues with postgres being able to save enum types. I am no longer able to use #TypeDef annotation as it was removed.
#Enumerated(value = EnumType.STRING)
#Type(MyPSQLType.class)
private MyType myType;
The postgres type is defined as
public class MyPSQLType extends org.hibernate.type.EnumType<MyType> {
#Override
public void nullSafeSet(PreparedStatement st, MyType value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
st.setObject(index, value != null ? value.name() : null, Types.OTHER);
}
}
This is the error I am now getting. Do I have to now register the custom type in a different way now that TypeDef is no longer applicable?
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: my_type = character varying
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 187
I don't know how the PostgreSQL JDBC driver expects the values to be passed in, or how you did this before, but I guess you will have to use this instead:
if ( value == null ) {
st.setNull(index, Types.OTHER, "my_type");
}
else {
PGObject object = new PGObject();
object.setType("my_type");
object.setValue(value.name());
st.setObject(index, object, Types.OTHER);
}

Eloquent relationship returns null, but a similar one is fine

Using Lumen 5.5.2 and illuminate/database v5.5.17.
I have 3 models set up, where one belongs to the other 2. So Quote, has an area, and a depot.
The relationship with the depot works as expected, the area returns null.
for example
$quoteModel = new Quote();
$quote = $quoteModel
->with('area')
->with('depot')
->where('id', '=', $id)
->first();
echo 'depot id : ' , $quote->depot->id , "<br>\n";
echo 'area id : ' , $quote->area->id , "<br>\n";
The depot id will be echoed, the area will cause an error because it is not an object.
Passing the models names as an array ->with(['area', 'depot']), or just requesting area (either method) does not fix it.
Quote.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Quote extends EloquentModel {
protected $table = 'quotes';
public function area() {
return $this->belongsTo('\App\Models\Area', 'area_id', 'id');
}
public function depot() {
return $this->belongsTo('\App\Models\Depot', 'depot_id', 'id');
}
}
Area.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Area extends EloquentModel {
protected $table = 'areas';
public $timestamps = false;
public $incrementing = false;
public function quotes() {
return $this->hasMany('\App\Models\Quote', 'area_id', 'id');
}
}
Depot.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Depot extends EloquentModel {
protected $table = 'depots';
public $timestamps = false;
public $incrementing = false;
public function quotes() {
return $this->hasMany('\App\Models\Quote', 'depot_id', 'id');
}
}
If I create a parse error in Area.php the script will fail, proving it is being included.
I have a listener set up so I can log the queries, and they show up just fine.
select * from `quotes` where `id` = 99192 limit 1
select * from `areas` where `areas`.`id` in (072)
select * from `depots` where `depots`.`id` in (07)
If I run the area query manually it returns the row I expect.
I tried changing the name of the area relationship, and it doesn't help.
So the missing piece of the puzzle, was that this project is setup up against a legacy database as part of updating an existing web app.
Turns out that there was some datatype inconsistency; I found this out when I could successfully link another model to area with no issues. The field for the area_id is normally a zero filled int, but for some reason on the quotes table it was a char; so the data looked correct when browsing in adminer, and worked when copied and pasted, but did not match up somewhere in Eloquents internals.
Changing the datatype on the table fixes the issue.

Ormlite and PostgreSQL - Error inserting text array with custom persister

I have been working to setup Ormlite as the primary data access layer between a PostgreSQL database and Java application. Everything has been fairly straightforward, until I started messing with PostgreSQL's array types. In my case, I have two tables that make use of text[] array type. Following the documentation, I created a custom data persister as below:
public class StringArrayPersister extends StringType {
private static final StringArrayPersister singleTon = new StringArrayPersister();
private StringArrayPersister() {
super(SqlType.STRING, new Class<?>[]{String[].class});
}
public static StringArrayPersister getSingleton() {
return singleTon;
}
#Override
public Object javaToSqlArg(FieldType fieldType, Object javaObject) {
String[] array = (String[]) javaObject;
if (array == null) {
return null;
} else {
String join = "";
for (String str : array) {
join += str +",";
}
return "'{" + join.substring(0,join.length() - 1) + "}'";
}
}
#Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) {
String string = (String) sqlArg;
if (string == null) {
return null;
} else {
return string.replaceAll("[{}]","").split(",");
}
}
}
And then in my business object implementation, I set up the persister class on the column likeso:
#DatabaseField(columnName = TAGS_FIELD, persisterClass = StringArrayPersister.class)
private String[] tags;
When ever I try inserting a new record with the Dao.create statement, I get an error message saying tags is of type text[], but got character varying... However, when querying existing records from the database, the business object (and text array) load just fine.
Any ideas?
UPDATE:
PostGresSQL 9.2. The exact error message:
Caused by: org.postgresql.util.PSQLException: ERROR: column "tags" is
of type text[] but expression is of type character varying Hint: You
will need to rewrite or cast the expression.
I've not used ormlite before (I generally use MyBatis), however, I believe the proximal issue is this code:
private StringArrayPersister() {
super(SqlType.STRING, new Class<?>[]{String[].class});
}
SqlType.String is mapped to varchar in SQL in the ormlite code, and so therefore I believe is the proximal cause of the error you're getting. See ormlite SQL Data Types info for more detail on that.
Try changing it to this:
private StringArrayPersister() {
super(SqlType.OTHER, new Class<?>[]{String[].class});
}
There may be other tweaks necessary as well to get it fully up and running, but that should get you passed this particular error with the varchar type mismatch.

Accessing bean properties with getters giving errors

If I try to access properties of a java bean with getters it gives me errors, while using bean property directly works properly. I understand that using bean properties directly is the recommended way as stated in documentation. However I prefer to use getters for some specific reasons in this case.
Here is the rule file and the errors I am getting. Would appreciate any clues on why I get this strange behavior.
Rule file
package test.rules
import java.lang.Math;
import test.MatFact;
import test.MatHotelAlternative;
import test.MatHotelItem;
dialect "mvel"
rule "0"
salience 0
when
$item: MatHotelItem()
$alt: MatHotelAlternative( processed == false
&& (getStarRating() == "*")
)
then
modify ($alt) {
// some actions here
}
end
Errors
[16,26]: [ERR 101] Line 16:26 no viable alternative at input ')' in rule "0" in pattern MatHotelAlternative
[16,28]: [ERR 102] Line 16:28 mismatched input '==' expecting ')' in rule "0" in pattern MatHotelAlternative
[17,1]: [ERR 102] Line 17:1 mismatched input ')' expecting 'then' in rule "0"
Related classes
MatHotelAlternative
public class MatHotelAlternative extends MatItemAlternative<Hotel>
{
public String getStarRating()
{
return alternative.getStarRating();
}
}
MatItemAlternative
public abstract class MatItemAlternative<T extends HolidayItem>
{
private boolean processed;
protected T alternative;
public MatItemAlternative( T alternative )
{
this.alternative = alternative;
}
public boolean isProcessed()
{
return processed;
}
public void setProcessed( boolean processed )
{
this.processed = processed;
}
}
this should work:
$alt: MatHotelAlternative( processed == false, starRating == "*")
can you try with that?
Which errors did you get with this alternative?
Looks like the class loader has been loading Drools 5.1.1 and this seems to be the cause of the problem. The problem was resolved when I switched to Drools 5.5.0 Final.

comparing String valued fields in Drool 5.5

I have some puzzling issues when use Drools 5.5 final to compare String valued fields.
Essentially, I am trying to find if there are a pair of persons sharing the same name. The Person class looks like below:
public class Person {
private String name;
public String getName() { return name; }
public void setName(String n) { this.name = n; }
public Person(String name) { this.name = name;}
}
The rule that I try to trigger is :
rule "uniquePersonName"
when
$p1: Person($n1: name)
$p2: Person(this != $p1, name == $n1)
then
System.out.println("Duplicated person name found : " + $n1 + " " + $p2.getName());
end
But it never got triggered. However, if I change it to :
when
$p1: Person($n1: name)
$p2: Person(this != $p1, name != $n1)
The system works as expected, in other words, it finds all the pairs where the persons have different names.
After digging deeper, I found if I changed the name field to be of type Integer, the original rule worked fine. That made me think it was because of some bug with String comparison. So with the name field defined as the String type, I tried:
not (name == $n1)
(name == $n1)
name.toString() = $n1.toString()
name == $p1.getName()
Unfortunately, none of them worked.
Finally, the only way I could get it behave is to write the rule as:
when
$p1: Person($n1: name)
$p2: Person(name == $n1)
eval($p1!=$p2)
This made me think perhaps the problem is caused by a combination of how this works and how String comparison is done.
This is very basic feature and I would be surprised this is caused by a bug in Drools 5.5 final. But again, I could not see a way through. Can any of you help?
Thanks.
GW
It turns out this != $p1 transforms to !this.equals($p1) under the hood, and in my code I have an auto-generated equals method (code not shown in the original post), which totally change the default comparison behavior. After removing that equals method, all worked as expected.