How to find employee of system user - axapta

I have a form that must be triggered from the Info class startupPost method, as it has to verify a condition, and if the condition is true (if it's the user's birthday), then trigger my form.
The only way I found to verify the BDay of the user is to take it from the EmplTable. Also, I need to relate something between the EmplTable and UserInfo or SysUserInfo tables in order to give me the user ID.
The problem is that I didn't find a proper way to relate the tables, because in my EmplTable I have a DEL_userId field wich is NOT populated and not used.
In conclusion my if block from the following chunk of code does not get executed.
Also I left The emplTable variable intentionally not declared, as I am not sure what's the best way to declare it.
Thanks in advance for any ideas guys!
void startupPost()
{
EmplTable emplTable ; //intentionally left undeclared
int dayOfBirth = dayOfMth(emplTable.BirthDate);
int monthOfBirth = mthOfYr(emplTable.BirthDate);
int dayOfMonth = dayOfMth(systemdateget());
int monthOfYear = mthOfYr(systemdateget());
str emplName = emplTable.name();
Args args = new Args(); //formrun
FormRun formrun;
;
if (dayOfBirth == dayOfMonth && monthOfBirth == monthOfyear )
{
info("ok"); //just to verify if my loop is working
new MenuFunction(menuitemDisplayStr(NET_PALIN_001_HBDay), MenuItemType::Display).run();
// args.name(formStr(NET_PALIN_001_HBDay));
// formRun = classFactory.formRunClass(args);
// formRun.init();
// formrun.run();
}
}

Have a look at the table SysCompanyUserInfo which maps system users from UserInfo to employees of EmplTable.
Below is an example to find the associated record of EmplTable (if it exists) for the current user
static void FindCurUserEmplTable(Args _args)
{
EmplId emplId;
EmplTable emplTable;
;
emplId = EmplTable::userId2EmplId(curuserid());
if (emplId != '')
{
emplTable = EmplTable::find(emplId);
// ... your birthday logic
}
}

Related

Dapper question. Getting values from returned object

Just started learning Dapper. I have an ADO.NET background. Using a demo I downloaded, I can insert/delete data from a webform into a MySql table just fine. This, however, I have searched all morning on.
In retrieving a single row from the db by ID, it doesn't return a LIST<>, it seems to be just an object (using code from the demo I downloaded). The query works, I get the object back. It has the fields: "ProductID, Description and Price".
The only way I could get the values to those three fields was like this:
System.Reflection.PropertyInfo pi = Product.GetType().GetProperty("ProductID");
System.Reflection.PropertyInfo desc = Product.GetType().GetProperty("Description");
System.Reflection.PropertyInfo price = Product.GetType().GetProperty("Price");
int _ProductID = (int)(pi.GetValue(Product, null));
string _Description = (string)(desc.GetValue(Product, null));
decimal _Price = (decimal)(price.GetValue(Product, null));
This works and gets the correct values for the three fields.
I'm used to looping through DataTables, but I just think there is probably a better way to get those values.
Is this the correct way to do this or am I missing something? I did actually read documentation and mess with this all morning before asking, too.
Some of the things I looked at seem to be very complex. I thought Dapper was supposed to simplify things.
OK, Thanks Marc. It was difficult for me to see what was supposed to be in the Dapper class files and what was supposed to be in my code behind. The original demo way of getting a product by ID had the query as .FirstOrDefault();
I changed everything to return a List<> and it all worked. I'm sure my ADO.NET is showing, but this works. In Dapper class files:
public List<Product> ProductAsList(int Id)
{
return this._db.Query<Product>("SELECT * FROM Cart_product WHERE ProductID=#Id", new { Id = Id }).**ToList()**;
}
This is just getting one row that matched the ProductID.
In page codebehind:
protected void CartItemAdd(string ProductId) // passing it the selected ProductID
{
var results = cartservice.ProductAsList(Convert.ToInt32(ProductId));
// returns that one row using Dapper ProductAsList(ProductId)
int _ProductId = 0;
string Description = string.Empty;
decimal Price = 0;
// Loop through the list and get the value of each item:
foreach (Product obj in results)
{
_ProductId = obj.ProductID;
Description = obj.Description;
Price = obj.Price;
}
// Using Dapper to insert the selected product into the shopping cart (table):
String UserName = "jbanks";
cartitem = new CartItem();
cartitem.ProductID = _ProductId;
cartitem.Quantity = 1;
cartitem.Description = Description;
cartitem.Price = Price;
cartitem.Created = DateTime.Now;
cartitem.CreatedBy = UserName;
result = cartservice.AddCartItem(cartitem);
if (result)
{
lblMessage.Text = string.Empty;
lblMessage.Text = "Successfully added a cart item";
}
}
}
It does indeed look up the product from one table and insert a selected item into another table.
Thanks again!
The main Query<T> API returns an IEnumerable<T>, which often will be a List<T>; the AsList<T>() extension method can get it back to a list without a copy, but either way: they are just T, for whatever T you asked for. If you asked for Query<Product>, then: they should be Product instances:
var results = connection.Query<Product>(someSql, someArgs); // perhaps .AsList()
foreach (Product obj in results) { // "var obj" would be fine here too
// now just use obj.ProductID, obj.Description and obj.Price
}
If that didn't work: check that you used the <T> version of Query. There is a non-generic variant too, which returns dynamic. Frankly, you should almost always use the <T> version.
Note: I'm assuming that somewhere you have something like
class Product {
public int ProductID {get;set;}
public string Description {get;set;}
public decimal Price {get;set;}
}

OrientDB execute script asynchronously and fetch records in a lazy fashion

Currently, we are using the Document API in OrientDB version 2.2. Let us suppose we have a class Company and a class Employee. Let's suppose we are interested in all Companies with at least one employee having a name from an arbitrary list. Employees are defined as LINKEDLISTs in our Company schema.
Our query would look smth like this:
select from Company where employees in (select from Employee where name in ["John", "Paul"])
Currently we have defined the following two indexes:
Company.employees (index on the employee links (their #rid)) -> dictionary hash index and
Employee.name -> notunique index
When executing the above query with explain we see that only the second index Employee.name is used, since we did not define the above indexes as a compound index. AS far as I could understand compound indexes across different classes like in our case are not supported in Orient 2.x.
Queries like this:
select from Company let $e = select from employees where name in ["John", "Paul"] where employees in $e
do not solve our problem either.
Searching across different blogs revealed two suggestions so far:
trying to define a compound index via inheritance by introducing a parent class on employee and company and defining the above two indexes on that
https://github.com/orientechnologies/orientdb/issues/5069
bundle the two queries in a batch scrip like this:
https://github.com/orientechnologies/orientdb/issues/6684
String cmd = "begin\n";
cmd += "let a = select from Employees where name " + query + "\n";
cmd += "let b = select from Company where employees in $a\n";
cmd += "COMMIT\n";
cmd += "return $b";
Suggestion 1 did not work for us.
Suggestion 2. worked. Both indexes have been used in each separate query, but then we ran into the next limitation of Orient. Batch scripts seem to be executed only synchronously, meaning that we can only get the results as a list all at once and not one by one in a lazy fashion, which in our case is a NO GO due to the memory overhead.
One naive workaround we tried is as follows:
public class OCommandAsyncScript extends OCommandScript implements OCommandRequestAsynch{
public OCommandAsyncScript(String sql, String cmd) {
super(sql, cmd);
}
#Override
public boolean isAsynchronous() {
return true;
}
private void containsAtLeastOne(final #Nonnull ODatabaseDocumentTx documentTx,
final #Nonnull Consumer<Company> matchConsumer,
final #Nonnull String queryText
) throws TimeoutException {
String cmd = "begin\n";
cmd += "let a = select from Employee where name " + queryText + "\n";
cmd += "let b = select from Company where employees in $a\n";
cmd += "COMMIT\n";
cmd += "return $b";
final OCommandHandler resultListener = new OCommandHandler(documentTx, (document -> {
final Company companies = document2model(document);
matchConsumer.accept(company);
}));
OCommandAsyncScript request = new OCommandAsyncScript("sql", cmd);
request.setResultListener(resultListener);
documentTx.command(request).execute();
...
}
}
public class OCommandHandler implements OCommandResultListener {
private final ODatabaseDocumentTx database;
private final Consumer<ODocument> matchConsumer;
public OCommandHandler(
final #Nonnull ODatabaseDocumentTx database,
final #Nonnull Consumer<ODocument> matchConsumer
) {
this.database = database;
this.matchConsumer = matchConsumer;
}
#Override
public boolean result(Object iRecord) {
if (iRecord != null) {
final ODocument document = (ODocument) iRecord;
/*
Result handler might be asynchronous, if document is loaded in a lazy mode,
database will be queries to fetch various fields. Need to activate it on the current thread.
*/
database.activateOnCurrentThread();
matchConsumer.accept(document);
}
return true;
}
...
}
The approach of defining a custom OCommandAsyncScript did not work unfortunately. When debugging the OStorageRemote class of Orient it seems that no partial results could be read, Here the respective extract from the source code:
public Object command(final OCommandRequestText iCommand) {
....
try {
OStorageRemote.this.beginResponse(network, session);
List<ORecord> temporaryResults = new ArrayList();
boolean addNextRecord = true;
byte status;
if(asynch) {
while((status = network.readByte()) > 0) {
ORecord record = (ORecord)OChannelBinaryProtocol.readIdentifiable(network);
if(record != null) {
switch(status) {
case 1:
if(addNextRecord) {
addNextRecord = iCommand.getResultListener().result(record);
database.getLocalCache().updateRecord(record);
}
break;
case 2:
if(record.getIdentity().getClusterId() == -2) {
temporaryResults.add(record);
}
database.getLocalCache().updateRecord(record);
}
}
}
}
}
Network.readbyte() is always null, hence no records could be fetched at all.
Is there any other workaround how we could execute a sql script in asynchronus mode and retrieve results in a lazy fashion preventing the generation of large lists on our application side?

retrieve a row of data from documentum via DQL

I have a list of users in my list view which is populated by retrieving data from documentum . If I click on any row of this least (each row represent one user) I should be able to see all of their information listed down .(This is my problem )
public void selectedItemFromListView(){
selected = lwAllUserGrp.getSelectionModel().getSelectedItem();
System.out.println(selected);
String query =" select * from dm_user where user_name = '#aclName'" ;
String test = query.replace("#aclname", selected);
GetDataWithDqlProfile(_session , query , "user_login_name" , "user_address" , "user_state" );
System.out.println(user.getAddress());
System.out.println(user.getState());
System.out.println(user.getUsername());
}
if I click on a row of list view I can successfully see who is selected and I need to retrieve all the other attributes of that username (same person) from documentum via DQL .
private void GetDataWithDqlProfile(IDfSession session, String Query, String username , String address , String state ) {
try {
IDfQuery UpdateQuery = new DfQuery();
UpdateQuery.setDQL(Query);
IDfCollection col = UpdateQuery.execute(_session, IDfQuery.DF_QUERY);
user.setAddress(col.getString(username));
user.setUsername(col.getString(address));
user.setState(col.getString(state));
col.close();
} catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.ERROR, e.getMessage());
alert.showAndWait();
Logs.WriteLog(LoginController.Repository + ",User:" + LoginController.Username, "DQL Query", e.toString());
e.getStackTrace();
}
and my output is :
User's name
null
null
null
I've tried the DQL query in DQL tester and it works well
In order to fetch rows from IDfCollection you have to call next() on the collection object. This method both advances to the next row and returns a boolean if successful. Use a boolean test (e.g., while or if) to iterate, like this:
IDfCollection col = UpdateQuery.execute(_session, IDfQuery.DF_QUERY);
if (col.next()) {
user.setAddress(col.getString(username));
user.setUsername(col.getString(address));
user.setState(col.getString(state));
}
col.close();
The iteration is necessary even if the collection contains only one row. In other words, you need to manually advance to the first row.
1) As #eiviamu already mentioned, you have to call IDfCollection.next() to get the next row.
2) Your code, among other problems, has one documentum-related: closing of collection must happen always in finally block.
Otherwise you can get unclosed collection which might lead to memory leaks and weird application behavior (e.g. if I'm not mistaken there are 9 simultaneous open collections are allowed for one DCTM session by default, and if you exceed this limit an exception will be thrown)
For those of you referring to this question later here is how I solved the problem :
public ArrayList<User> GetDataWithDqlpro(IDfSession session, String Query, String username , String state , String address) {
try {
IDfQuery UpdateQuery = new DfQuery();
UpdateQuery.setDQL(Query);
IDfCollection col = UpdateQuery.execute(_session, IDfQuery.DF_QUERY);
while (col.next()) {
list.add( new User(col.getString(username),col.getString(address) , col.getString(state)));
}
col.close();
}catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.ERROR, e.getMessage());
alert.showAndWait();
Logs.WriteLog(LoginController.Repository + ",User:" + LoginController.Username, "DQL Query", e.toString());
e.getStackTrace();
}
return (ArrayList<User>) list;
}
public void selectedItemFromListView(){
selected = lwAllUserGrp.getSelectionModel().getSelectedItem();
System.out.println(selected);
String Query = "select user_login_name , user_state , user_address from dm_user where user_login_name ='#aclname'";
Query = Query.replace("#aclname",selected );
ArrayList<User> allUserNames = GetDataWithDqlpro(_session, Query, "user_login_name","user_address","user_state");
for (int i = 0 ; i <= allUserNames.size()-1 ; i++ ){
if (selected.compareToIgnoreCase(allUserNames.get(i).getUsername() ) == 0){
System.out.println(allUserNames.get(i).getState() );
System.out.println(allUserNames.get(i).getAddress() );
System.out.println(allUserNames.get(i).getUsername() );
}
}
}
Worth mentioning that I have a class called User with constructor and get and set methods
I hope it will help some one :)

QSqlRelationalTableModel insertRecord doesn't work if call setRelation

editlistofcustomers.h
QSqlRelationalTableModel *RelationalModel;
TreeModel *CategoriesModel;
QSqlRecord Record;
editlistofcustomers.cpp
EditListOfCustomers::EditListOfCustomers // constructor
RelationalModel = new QSqlRelationalTableModel(this, *SupportObj->GetDataBase());
RelationalModel->setTable(SupportObj->GetCustomersTableName());
RelationalModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
RelationalModel->select();
RelationalModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Название/имя покупателя"));
Record = RelationalModel->record();
RelationalModel->setRelation(2, QSqlRelation(SupportObj->GetCategoriesOfCustomersTableName(),
SupportObj->GetCategoriesOfCustomersPattern()[0].GetName(), // ID.
SupportObj->GetCategoriesOfCustomersPattern()[2].GetName())); // Name.
CategoriesModel = new TreeModel(QObject::tr("Категории"),
SupportObj->GetCategoriesOfCustomersTableName(),
SupportObj, this);
//Setup model view delegate.
ui->TV_ListOfCustomers->setModel(RelationalModel);
ui->TV_ListOfCustomers->setItemDelegate(new QSqlRelationalDelegate(ui->TV_ListOfCustomers));
ui->TV_ListOfCustomers->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->TV_ListOfCustomers->setSortingEnabled(true);
ui->TV_CategoryOfCustomer->setModel(CategoriesModel);
SupportObj->GetDataBase()->transaction()
EditListOfCustomers::AddCustomer
QString customerName = ui->LE_CustomerName->text();
if(!customerName.isEmpty())
{
Record.setValue(SupportObj->GetCustomersPattern()[1].GetName(), QVariant(customerName)); // Name.
int categoryID = CategoriesModel->GetItemID(ui->TV_CategoryOfCustomer->currentIndex());
Record.setValue(SupportObj->GetCustomersPattern()[2].GetName(), QVariant(categoryID)); // Category ID.
Record.setValue(SupportObj->GetCustomersPattern()[3].GetName(), QVariant(ui->LE_CustomerTelephoneNumbers->text())); // Telephone numbers.
Record.setValue(SupportObj->GetCustomersPattern()[4].GetName(), QVariant(ui->LE_CustomerAddress->text())); // Address.
Record.setValue(SupportObj->GetCustomersPattern()[5].GetName(), QVariant(ui->TE_CustomerComment->toPlainText())); // Comment.
RelationalModel->insertRecord(-1, Record);
if(!RelationalModel->submitAll())
{
QMessageBox::warning(this, "CategoriesEditor::CategoriesEditor", SupportObj->GetDataBase()->lastError().text());
}
// Clear fields
ui->LE_CustomerName->clear();
ui->TV_CategoryOfCustomer->setCurrentIndex(QModelIndex());
ui->LE_CustomerTelephoneNumbers->clear();
ui->LE_CustomerAddress->clear();
ui->TE_CustomerComment->clear();
ui->LE_CustomerName->setFocus();
ui->TV_ListOfCustomers->sortByColumn(0, Qt::AscendingOrder);
}
If comment "RelationalModel->setRelation( ... )" everything work fine: transaction, adding, deleting, commit or rollback. If uncomment nothing work, but show everything right(ID replaced by category name), but I can't insert new row using "insertRecord". I try this
suggestion, but unsuccessfully.
Any suggestion?
The problem was in the column name. setRelation changes column name category_of_customer_id to category_of_customers_name_2.
The solution use void setValue(int index, const QVariant &val) instead of void setValue(const QString & name, const QVariant &val).
QSqlRecord record = RelationalModel->record();
record.setValue(1, QVariant(customerName)); // Name.
int categoryID = CategoriesModel->GetItemID(ui->TV_CategoryOfCustomer->currentIndex());
record.setValue(2, QVariant(categoryID)); // Category ID.
record.setValue(3, QVariant(ui->LE_CustomerTelephoneNumbers->text())); // Telephone numbers.
record.setValue(4, QVariant(ui->LE_CustomerAddress->text())); // Address.
record.setValue(5, QVariant(ui->TE_CustomerComment->toPlainText())); // Comment.
RelationalModel->insertRecord(-1, record);
if(!RelationalModel->submitAll())
{
QMessageBox::warning(this, "CategoriesEditor::CategoriesEditor", SupportObj->GetDataBase()->lastError().text());
}
Had the same issue. This only needs to happen for the field that has a setRelation. Here's the solution for PyQt:
def insertRow(self):
rec = self.record()
rec.setValue("id", "new id")
rec.setValue("type", 1)
rec.setValue("title", "new title")
return self.insertRecord(-1, rec)
self.submit()
Apologies. Wanted to comment, but don't yet have a reputation of 50+.

X++ passing current selected records in a form for your report

I am trying to make this question sound as clear as possible.
Basically, I have created a report, and it now exists as a menuitem button so that the report can run off the form.
What I would like to do, is be able to multi-select records, then when I click on my button to run my report, the current selected records are passed into the dialog form (filter screen) that appears.
I have tried to do this using the same methods as with the SaleLinesEdit form, but had no success.
If anyone could point me in the right direction I would greatly appreciate it.
Take a look at Axaptapedia passing values between forms. This should help you. You will probably have to modify your report to use a form for the dialog rather than using the base dialog methods of the report Here is a good place to start with that!
Just wanted to add this
You can use the MuliSelectionHelper class to do this very simply:
MultiSelectionHelper selection = MultiSelectionHelper::createFromCaller(_args.caller());
MyTable myTable = selection.getFirst();
while (myTable)
{
//do something
myTable = selection.getNext();
}
Here is the resolution I used for this issue;
Two methods on the report so that when fields are multi-selected on forms, the values are passed to the filter dialog;
private void setQueryRange(Common _common)
{
FormDataSource fds;
LogisticsControlTable logisticsTable;
QueryBuildDataSource qbdsLogisticsTable;
QueryBuildRange qbrLogisticsId;
str rangeLogId;
set logIdSet = new Set(Types::String);
str addRange(str _range, str _value, QueryBuildDataSource _qbds, int _fieldNum, Set _set = null)
{
str ret = _range;
QueryBuildRange qbr;
;
if(_set && _set.in(_Value))
{
return ret;
}
if(strLen(ret) + strLen(_value) + 1 > 255)
{
qbr = _qbds.addRange(_fieldNum);
qbr.value(ret);
ret = '';
}
if(ret)
{
ret += ',';
}
if(_set)
{
_set.add(_value);
}
ret += _value;
return ret;
}
;
switch(_common.TableId)
{
case tableNum(LogisticsControlTable):
qbdsLogisticsTable = element.query().dataSourceTable(tableNum(LogisticsControlTable));
qbrLogisticsId = qbdsLogisticsTable.addRange(fieldNum(LogisticsControlTable, LogisticsId));
fds = _common.dataSource();
for(logisticsTable = fds.getFirst(true) ? fds.getFirst(true) : _common;
logisticsTable;
logisticsTable = fds.getNext())
{
rangeLogId = addrange(rangeLogId, logisticsTable.LogisticsId, qbdsLogisticsTable, fieldNum(LogisticsControlTable, LogisticsId),logIdSet);
}
qbrLogisticsId.value(rangeLogId);
break;
}
}
// This set the query and gets the values passing them to the range i.e. "SO0001, SO0002, SO000£...
The second methods is as follows;
private void setQueryEnableDS()
{
Query queryLocal = element.query();
;
}
Also on the init method this is required;
public void init()
{
;
super();
if(element.args() && element.args().dataset())
{
this.setQueryRange(element.args().record());
}
}
Hope this helps in the future for anyone else who has the issue I had.

Resources