Batch apex class to update all the account records daily(update field description on account) - apex

Here is the code
global class TestBatchClass implements Database.Batchable <sobject>{
String query;
global Database.querylocator start(Database.BatchableContext BC){
Query = 'Select id,name,Description from account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sobject> scope){
List<account> accList = new List<account>();
for(account s : scope){
s.Description = 'Updated in batch apex class';
accList.add(s);
}
update accList;
}
global void finish(Database.BatchableContext BC){
//comment - finish method can remain blank
}
}
Getting this error :
Error in line 9: Invalid loop variable type expected SObject was Account

The easiest way to get around this error is changing the execute's method signature to this; global void execute(Database.BatchableContext BC, List<Account> scope)
You can also cast the sObject to an Account object but it's not necessary in this case since you're only updating accounts.

Related

How can I find out if a MUC exists using Smack?

Working with Smack 4.3.0 in a Multi User Chat (XEP-0045-1.21) I'm trying to find out if a room is already created or not, but I'm not sure if what I'm doing is the correct way.
I have search for it and the most relative answer to it was does MUC exist?.
Technically speaking:
Rooms are created as public and members-only by default in OpenFire 4.2.0.
All room's names are an id defined by the members name in a hash string i.e. XXXXXX029d8c36b62259d0eXXXXXXXX. This means that user A can create a room with B, C and get a groupId like the previous one, but then user B (in another device) can try to create same room (with users A,B,C), which is going to give him same groupId.
Exist a architecture layer like whatsapp, so users can leave a Group Chat and rejoin whenever they want.
What I'm doing at this moment:
#WorkerThread
public boolean isGroupChatAlreadyCreated(#NonNull final String groupId)
throws
XmppStringprepException,
XMPPException.XMPPErrorException,
MultiUserChatException.NotAMucServiceException,
SmackException.NotConnectedException,
InterruptedException,
SmackException.NoResponseException {
List<HostedRoom> hostedRooms = manager.getHostedRooms(JidCreate.domainBareFrom(serviceDomain));
for (HostedRoom hostedRoom : hostedRooms) {
if (hostedRoom.getName().equalsIgnoreCase(groupId)) {
return true;
}
}
return false;
}
where manager is MultiUserChatManager manager and serviceDomain is just a String.
so, the questions: is this right way to do it? can it be improved?
I believe the easier way is get some info about room, for example its configuration form. If nothing will be returned then it means room does not exist:
public boolean isGroupChatAlreadyCreated(#NonNull final EntityBareJid groupId)
throws
XMPPErrorException,
NotConnectedException,
InterruptedException,
NoResponseException {
MultiUserChat multiUserChat = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(groupId);
return multiUserChat.getConfigurationForm() != null;
}
More info here https://github.com/igniterealtime/Smack/blob/4.3/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java#L809
It is essentially the right way.
Ideally you simply use MulitUserChatManager.getRoomInfo(EntityBareJid). The method will return a RoomInfo if the room exists, or throw if it does not.
Your original approach can also be improved by changing the type of 'groupId' to EntityBareJid using equals() instead of equalsIgnoreCase(). And putting your groupId as Localpart of the MUC's address. So your function becomes:
public boolean isGroupChatAlreadyCreated(#NonNull final EntityBareJid groupId)
throws
XmppStringprepException,
XMPPErrorException,
NotAMucServiceException,
NotConnectedException,
InterruptedException,
NoResponseException {
List<HostedRoom> hostedRooms = manager.getHostedRooms(JidCreate.domainBareFrom(serviceDomain));
for (HostedRoom hostedRoom : hostedRooms) {
if (hostedRoom.getJid().equals(groupId)) {
return true;
}
}
return false;
}

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.

How to set values in ItemPreparedStatementSetter for one to many mapping

I am trying to use JdbcBatchItemWriter for a domain object RemittanceClaimVO . RemittanceClaimVO has a List of another domain object , ClaimVO .
public class RemittanceClaimVO {
private long remitId;
private List<ClaimVO> claims = new ArrayList<ClaimVO>();
//setter and getters
}
So for each remit id, there would be multiple claims and I wish to use single batch statement to insert all rows.
With plain jdbc, I used to write this object by putting values in batches like below ,
ist<ClaimVO> claims = remittanceClaimVO.getClaims();
if(claims != null && !claims.isEmpty()){
for(ClaimVO claim:claims){
int counter = 1 ;
stmt.setLong(counter++, remittanceClaimVO.getRemitId());
stmt.setLong(counter++, claim.getClaimId());
stmt.addBatch();
}
}
stmt.executeBatch();
I am not sure how to achieve same in Spring Batch by using ItemPreparedStatementSetter.
I have tried similar loop as above in setValues method but values not getting set.
#Override
public void setValues(RemittanceClaimVO remittanceClaimVO, PreparedStatement ps) throws SQLException {
List<ClaimVO> claims = remittanceClaimVO.getClaims();
for(ClaimVO claim:claims){
int counter = 1 ;
ps.setLong(counter++, remittanceClaimVO.getRemitId());
ps.setLong(counter++, claim.getClaimId());
}
}
This seems another related question.
Please suggest.

Unit testing "object reference not set to an instance " at NUnit

i have a ASP.Net project and Nunitasp framework work for unit testing,i have a object in account.aspx.cs file when i tried to test the object(NugetplatformModel) value i get"object reference not set to an instance" error,
my account page code is given below
public partial class Account : System.Web.UI.Page
{
public NugetPlatformModel NugetPlatformModels;
public string result = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("/login", true);
}
else
{
result = "success";
NugetPlatformModels = new NugetPlatformModel();
}
}
my test case code is given below
[Test]
public void AccountPage_ValidCredential_AccessModel()
{
Browser.GetPage(domain + "account");
string ExpectedPage = domain + "account";
logon();
Account acccountPage = new Account();
AssertEquals("success", acccountPage.result);
AssertEquals("should have license",true,acccountPage.NugetPlatformModels.IsHavingLicense);
}
How can I access and test that code behind variables? when start the testing the NUgetplatformmodel has been assigned i have checked it by debugging but after that in nunit gui it displays null reference error, i thought there is a problem in accessing variable in testcase..please help me..
It seems your code is not complete. From what I see here your account needs to run Page_Load in order to fill result and NugetPlatformModels. But I do not see how this method is launched in your test. Is it run from the constructor of Account?
It would be helpfull if you put all the code for Account in your post.

Update a single property of a record in Entity Framework Code First

How can I update a single property of a record without retrieving it first?
I'm asking in the context of EF Code First 4.1
Says I have a class User, mapping to table Users in Database:
class User
{
public int Id {get;set;}
[Required]
public string Name {get;set;}
public DateTime LastActivity {get;set;}
...
}
Now I want to update LastActivity of a user. I have user id. I can easily do so by querying the user record, set new value to LastActivity, then call SaveChanges(). But this would result in a redundant query.
I work around by using Attach method. But because EF throws a validation exception on Name if it's null, I set Name to a random string (will not be updated back to DB). But this doesn't seem a elegant solution:
using (var entities = new MyEntities())
{
User u = new User {Id = id, Name="this wont be updated" };
entities.Users.Attach(u);
u.LastActivity = DateTime.Now;
entities.SaveChanges();
}
I would be very appriciate if someone can provide me a better solution. And forgive me for any mistake as this is the first time I've asked a question on SO.
This is a problem of validation implementation. The validation is able to validate only a whole entity. It doesn't validate only modified properties as expected. Because of that the validation should be turned off in scenarios where you want to use incomplete dummy objects:
using (var entities = new MyEntities())
{
entities.Configuration.ValidateOnSaveEnabled = false;
User u = new User {Id = id, LastActivity = DateTime.Now };
entities.Users.Attach(u);
entities.Entry(user).Property(u => u.LastActivity).IsModified = true;
entities.SaveChanges();
}
This is obviously a problem if you want to use the same context for update of dummy objects and for update of whole entities where the validation should be used. The validation take place in SaveChanges so you can't say which objects should be validated and which don't.
I'm actually dealing with this right now. What I decided to do was override the ValidateEntity method in the DB context.
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
var result = base.ValidateEntity(entityEntry, items);
var errors = new List<DbValidationError>();
foreach (var error in result.ValidationErrors)
{
if (entityEntry.Property(error.PropertyName).IsModified)
{
errors.Add(error);
}
}
return new DbEntityValidationResult(entityEntry, errors);
}
I'm sure there's some holes that can be poked in it, but it seemed better than the alternatives.
You can try a sort of hack:
context.Database.ExecuteSqlCommand("update [dbo].[Users] set [LastActivity] = #p1 where [Id] = #p2",
new System.Data.SqlClient.SqlParameter("p1", DateTime.Now),
new System.Data.SqlClient.SqlParameter("p2", id));