abas-ERP: Copy TN field into another TN field with edp/epi - abas

I want to create an invoice and fill the field pftext (TN2) with the content of another TN2 field called ytdescription.
My problem is, that I always have the path of my textfield in pftext but not the content of my textfield.
1st try of edp header:
kunde;artex;pftext;mge
'M|ytkdnr';'M|ytartnr';'M|ytdescription';'M|ytmge'
2nd try:
kunde;artex;*pftext;mge;
'M|ytkdnr';'M|ytartnr';'M|ytdescription';'M|ytmge'
Of course I could create a T254 field and store the content of M|ytdescription in the new field, but then I am stuck to max 3000 chars for the content.
Many other tries followed but with no success :-(
Any help is highly appreciated!

I Don't know the options to do this per EDP, but there is a solution to do this per AJO:
public int run(DbContext dbContext, String[] args) {
/*
* Get the object to copy data from, in this case a customer with idno 70001
*/
String idno = "70001";
SelectionBuilder<Customer> selectionBuilder = SelectionBuilder.create(Customer.class);
selectionBuilder.add(Conditions.eq(Customer.META.idno, idno));
Customer customer = QueryUtil.getFirst(dbContext, selectionBuilder.build());
VendorEditor vendorEditor = null;
// Read the original content to a StringWriter
StringWriter originalFreeText = new StringWriter();
try {
customer.getFreeText(originalFreeText);
// Create a new object to write the values to. In example a Supplier
vendorEditor = dbContext.newObject(VendorEditor.class);
vendorEditor.setSwd("Searchword");
vendorEditor.setAddr("Vendor Name");
if (!originalFreeText.toString().equals("")) {
vendorEditor.setFreeText(new StringReader(originalFreeText.toString()));
}
vendorEditor.commit();
} catch (IOException e) {
dbContext.out().println(e.getMessage());
} finally {
if (vendorEditor != null) {
if (vendorEditor.active()) {
vendorEditor.abort();
}
}
}
return 0;
}

Related

SaveChanges() outside of foreach saves only the last object added

Seems that the SaveChanges() only saves the last object added into the table:
static void Main(string[] args)
{
Category c = new Category();
using (GenericDBEntities db = new GenericDBEntities())
{
foreach (Match i in db.Matches)
{
if (!db.Categories.Any())
{
c.CategoryInternalId = i.CategoryId;
c.CategoryName = i.CategoryName;
c.SportId = i.SportId;
db.Categories.Add(c);
}
else
{
foreach (Category a in db.Categories)
{
if (i.CategoryId != a.CategoryInternalId)
{
c.CategoryInternalId = i.CategoryId;
c.CategoryName = i.CategoryName;
c.SportId = i.SportId;
db.Categories.Add(c);
}
else
{
return;
}
}
}
}
db.SaveChanges();
I have tried it in a few different ways which all have the same or less result, the for loop saves them all, no matter if condition is met or not. Why is it only saving the last object of the Match table? What am I missing?
Clarification: The task is to go through Match table, which has duplicate CategoryIDs and to, if it is not already in the Category table, add it, so it won't store duplicates again, the problem is Category table is initially empty, hence the newbie logic, still learnin'!
You need to create the Category object for each inserted category, otherwise you have just one category object which you are editing all the time and hence just this one (the last edit) will be saved to the DB.
If you want to add all new categories from Matches based on CategoryId you can use something like this:
using (GenericDBEntities db = new GenericDBEntities())
{
var newCategories = db.Matches
.Where(m => !db.Categories
.Select(c => c.CategoryInternalId)
.Distinct().Contains(m.CategoryId))
.Select(m => new { m.CategoryId, m.CategoryName, m.SportId })
.GroupBy(m => m.otherid)
.Select(g => g.FirstOrDefault())
.ToList();
foreach (var i in newCategories)
{
var c = new Category()
{
CategoryInternalId = i.CategoryId,
CategoryName = i.CategoryName,
SportId = i.SportId
};
db.Categories.Add(c);
}
db.SaveChanges();
}
Now you'll get all the new categories from the Matches table in one go. This is a first draft, meaning that the performance can be further tuned if needed.

Inserting values in two tables using one controller in ASP.NET MVC4

I have two tables in my database the first one is librarybooklot where I want to keep record when new book is bought.
The next table is LibraryBookInventory where I want to keep record
of individual book.
Now what I want to do is when Book name A is bought in 1st lot and number of book is 10, the record should be entered in 1st table as well as second table, in second table 10 records need to entered for 10 books with different accession number, the accession number may be +1 of previous.
this is my function in LibBookLotDAO.cs
public static bool NewLibBookLotInformation(LibBookLotDTO LibBookLotDTO)
{
try
{
string sql = #"Insert into LibBookLot
(BookCode,Price,ReceivedOn,NumberOfBooks,Edition)
values (#BookCode,#Price,#ReceivedOn,#NumberOfBooks,#Edition ); ";
_dbm = new DBManager();
_dbm.CreateParameters(5);
_dbm.AddParameters(0, "#BookCode", LibBookLotDTO.BookCode);
_dbm.AddParameters(1, "#Price", LibBookLotDTO.Price);
_dbm.AddParameters(2, "#ReceivedOn", LibBookLotDTO.ReceivedOn);
_dbm.AddParameters(3, "#NumberOfBooks", LibBookLotDTO.NumberOfBooks);
_dbm.AddParameters(4, "#Edition", LibBookLotDTO.Edition);
int row = _dbm.ExecuteNonQuery(CommandType.Text, sql);
if (row == 0) return false;
else return true;
}
catch (Exception ex) { throw ex; }
finally { _dbm.Close(); }
}
and this is my code in LibBookLotController.cs
[HttpPost]
public ActionResult Add(LibBookLotDTO LibBookLotDTO)
{
if (LibBookLotDAO.NewLibBookLotInformation(LibBookLotDTO))
{
Session["INFO"] = "Successfully Added";
}
else
{
Session["ERROR"] = "Failed To Add";
}
return Redirect("Index?plugin=Library");
}
Plz someone help me, since I am new to asp.net

Combining two different jTables and adding button into jTable

I try to do for forum using java swing. Here are my codes for table :
public void SetUpJTable() {
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
String[] data = new String[4];
db.setUp("IT Innovation Project");
String sql = "Select topic_title,topic_description,topic_by from forumTopics WHERE topic_id = "
+ topicId + "";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
data[0] = resultSet.getString("topic_title");
data[1] = resultSet.getString("topic_description");
data[2] = resultSet.getString("topic_by");
tableModel.addRow(data);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
I set up this table to retrieve the topic details which user select certain thread from main page. And I set up another table to store for the replies by users. Here is it :
public void SetUpJTableComment() {
DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
.getModel();
String[] data = new String[3];
db.setUp("IT Innovation Project");
String sql = "Select reply_content,reply_by from forumReplies WHERE reply_topic = "
+ topicId + "";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
data[0] = resultSet.getString("reply_content");
data[1] = resultSet.getString("reply_by");
tableModel1.addRow(data);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
And this is how I set up the table :
private JTable getJTableComment() {
String header[] = { "Comment", "Reply By" };
if (jTableComment == null) {
jTableComment = new JTable() {
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
}
DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
.getModel();
tableModel1.setColumnIdentifiers(header);
jTableComment.getColumnModel().getColumn(0).setMinWidth(700);
jTableComment.getColumnModel().getColumn(0).setMaxWidth(800);
jTableComment.getColumnModel().getColumn(1).setMinWidth(97);
jTableComment.getColumnModel().getColumn(1).setMaxWidth(100);
jTableComment.getTableHeader().setFont(
new Font("Dialog", Font.PLAIN, 20));
jTableComment.getTableHeader().setForeground(Color.white);
jTableComment.getTableHeader().setBackground(new Color(102, 102, 102));
jTableComment.setRowHeight(50);
jTableComment.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTableComment.setFont(new Font("Dialog", Font.PLAIN, 18));
return jTableComment;
}
It works perfectly with two separating tables. I wonder if there is some way to combine both of these tables into one table? And how can I customize the table to make it look less-liked a table because my current one is just .. solid-table and my teacher asked me to improve it but I have no idea to do so. And I tried to add button into the table but I realized that I cannot add it from the try statement because that is is retrieve data from database directly. Any guides? Thanks in advance.
You can use SQL join construct and have one table with more columns:
select topic_title,topic_description,topic_by,
reply_content,reply_by
from forumTopics join forumReplies
on (forumTopics.topic_id=forumReplies.topic_id) WHERE topic_id = 1234
then build the model from the five column result set as you are already doing.
But surely if there is more than one reply to a forum topic, the topic part will be repeated in the table.
To make a table not to look like a table, try JTreeTable from Swing Labs maybe, it allows to have tree-like subsections, exactly that is required. It is not part of the system library however, you will need to download it. Some source code on how to just JTreeTable can be found here.
On how JTreeTable looks is Swing Labs, you can see in they web-startable demo. It also shows the code sample automatically.

RavenDB adds integer to ID when ends with a slash

I use RavenDB 1.0.0.426
I just experienced a weird scenario when importing data from an external source into RavenDB:
I chose to use the same unique ID as the external source uses, prefixed with a certain string. But. When I store a document with an ID that ends with a '/', raven automatically adds a number to the end of the ID, causing the document to NOT overwrite existing document stored with the same id.
I have recreated a simple scenario to cause the error:
The type I save:
public class Foo
{
public string Id { get; set; }
public Foo(string id)
{
Id = id;
}
}
Method saving a doc with the same id 10 times and afterwards checks the document count:
public void RunTest(string id)
{
for (int i = 0; i < 10; i++)
{
using (var doc = new DocumentStore() { Url = "http://pc-009:8080/" })
{
doc.Initialize();
using (var session = doc.OpenSession())
{
session.Store(new Foo(id));
session.SaveChanges();
}
}
}
// Wait for the data to be persisted
Thread.Sleep(2000);
using (var doc = new DocumentStore() { Url = "http://pc-009:8080/" })
{
doc.Initialize();
using (var session = doc.OpenSession())
{
var foos = session.Query<Foo>();
int fooCount = foos.Count();
// ASSERT HERE THAT fooCount is 1
}
}
}
Running the method with "id1" successfully overwrites existing documents:
RunTest("id1"); // Works fine
Running method with "id1/" ends up creating 10 documents in the database:
RunTest("id1/"); // Results in multiple documents to be created
I know that if you do not define your own ID, raven will autogenerate doc-ids on the fly, but is the behavior I describe above by design?
Thankyou in advance
Stephan,
This is expected, when your key ends with /, it asks ravendb to use identity generation strategy.
See the docs here:
http://ravendb.net/documentation/docs-api-key-generation
If you want a key that ends with /, you can url encode the keys

EntityFramework, Insert if not exist, otherwise update

I'm having a Entity-Set Countries, reflecting a database table '<'char(2),char(3),nvarchar(50> in my database.
Im having a parser that returns a Country[] array of parsed countries, and is having issues with getting it updated in the right way. What i want is: Take the array of countries, for those countries not already in the database insert them, and those existing update if any fields is different. How can this be done?
void Method(object sender, DocumentLoadedEvent e)
{
var data = e.ParsedData as Country[];
using(var db = new DataContractEntities)
{
//Code missing
}
}
I was thinking something like
for(var c in data.Except(db.Countries)) but it wount work as it compares on wronge fields.
Hope anyone have had this issues before, and have a solution for me. If i cant use the Country object and insert/update an array of them easy, i dont see much benefict of using the framework, as from performers i think its faster to write a custom sql script that inserts them instead of ect checking if an country is already in the database before inserting?
Solution
See answer of post instead.
I added override equals to my country class:
public partial class Country
{
public override bool Equals(object obj)
{
if (obj is Country)
{
var country = obj as Country;
return this.CountryTreeLetter.Equals(country.CountryTreeLetter);
}
return false;
}
public override int GetHashCode()
{
int hash = 13;
hash = hash * 7 + (int)CountryTreeLetter[0];
hash = hash * 7 + (int)CountryTreeLetter[1];
hash = hash * 7 + (int)CountryTreeLetter[2];
return hash;
}
}
and then did:
var data = e.ParsedData as Country[];
using (var db = new entities())
{
foreach (var item in data.Except(db.Countries))
{
db.AddToCountries(item);
}
db.SaveChanges();
}
I would do it straightforward:
void Method(object sender, DocumentLoadedEvent e)
{
var data = e.ParsedData as Country[];
using(var db = new DataContractEntities)
{
foreach(var country in data)
{
var countryInDb = db.Countries
.Where(c => c.Name == country.Name) // or whatever your key is
.SingleOrDefault();
if (countryInDb != null)
db.Countries.ApplyCurrentValues(country);
else
db.Countries.AddObject(country);
}
db.SaveChanges();
}
}
I don't know how often your application must run this or how many countries your world has. But I have the feeling that this is nothing where you must think about sophisticated performance optimizations.
Edit
Alternative approach which would issue only one query:
void Method(object sender, DocumentLoadedEvent e)
{
var data = e.ParsedData as Country[];
using(var db = new DataContractEntities)
{
var names = data.Select(c => c.Name);
var countriesInDb = db.Countries
.Where(c => names.Contains(c.Name))
.ToList(); // single DB query
foreach(var country in data)
{
var countryInDb = countriesInDb
.SingleOrDefault(c => c.Name == country.Name); // runs in memory
if (countryInDb != null)
db.Countries.ApplyCurrentValues(country);
else
db.Countries.AddObject(country);
}
db.SaveChanges();
}
}
The modern form, using later EF versions would be:
context.Entry(record).State = (AlreadyExists ? EntityState.Modified : EntityState.Added);
context.SaveChanges();
AlreadyExists can come from checking the key or by querying the database to see whether the item already exists there.
You can implement your own IEqualityComparer<Country> and pass that to the Except() method. Assuming your Country object has Id and Name properties, one example of that implementation could look like this:
public class CountryComparer : IEqualityComparer<Country>
{
public bool Equals(Country x, Country y)
{
return x.Name.Equals(y.Name) && (x.Id == y.Id);
}
public int GetHashCode(Country obj)
{
return string.Format("{0}{1}", obj.Id, obj.Name).GetHashCode();
}
}
and use it as
data.Countries.Except<Country>(db, new CountryComparer());
Although, in your case it looks like you just need to extract new objects, you can use var newCountries = data.Where(c => c.Id == Guid.Empty); if your Id is Guid.
The best way is to inspect the Country.EntityState property and take actions from there regarding on value (Detached, Modified, Added, etc.)
You need to provide more information on what your data collection contains i.e. are the Country objects retrieved from a database through the entityframework, in which case their context can be tracked, or are you generating them using some other way.
I am not sure this will be the best solution but I think you have to get all countries from DB then check it with your parsed data
void Method(object sender, DocumentLoadedEvent e)
{
var data = e.ParsedData as Country[];
using(var db = new DataContractEntities)
{
List<Country> mycountries = db.Countries.ToList();
foreach(var PC in data)
{
if(mycountries.Any( C => C.Name==PC.Name ))
{
var country = mycountries.Any( C => C.Name==PC.Name );
//Update it here
}
else
{
var newcountry = Country.CreateCountry(PC.Name);//you must provide all required parameters
newcountry.Name = PC.Name;
db.AddToCountries(newcountry)
}
}
db.SaveChanges();
}
}