cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

How do I get all promotion requests in JAVA?

VladiSlav
17-Peridot

How do I get all promotion requests in JAVA?

Hello everyone
I want to get all the promotion requests that have been approved for this month
I need this to create a custom report that will display WTPart + the number of details it contains;
WTDocument and EPMDocument for which I will get the format and number of sheets from the representation

But I do not know how to get all requests in java

ACCEPTED SOLUTION

Accepted Solutions

    public static QueryResult getPromotionRequests(DateRange dateRange) throws WTException {
        String startDateStr = dateRange.getDateFromStr();
        String endDateStr = dateRange.getDateToStr();

        QuerySpec querySpec = new QuerySpec(PromotionNotice.class);

        String createStampAttrName = "thePersistInfo.modifyStamp";
        Timestamp startDate = new Timestamp(Date.valueOf(startDateStr).getTime());
        Timestamp endDate = new Timestamp(Date.valueOf(endDateStr).getTime());

        SearchCondition startDateCondition = new SearchCondition(PromotionNotice.class, createStampAttrName, SearchCondition.GREATER_THAN_OR_EQUAL, startDate);
        querySpec.appendWhere(startDateCondition, new int[]{0});

        SearchCondition endDateCondition = new SearchCondition(PromotionNotice.class, createStampAttrName, SearchCondition.LESS_THAN, endDate);
        querySpec.appendAnd();
        querySpec.appendWhere(endDateCondition, new int[]{0});

        SearchCondition approvedStateCondition = new SearchCondition(PromotionNotice.class, "state.state", SearchCondition.EQUAL, "APPROVED");
        querySpec.appendAnd();
        querySpec.appendWhere(approvedStateCondition, new int[]{0});

        return PersistenceHelper.manager.find(querySpec);
    }

View solution in original post

10 REPLIES 10

I found the following code that receives all requests. Now it is not possible to get with the condition on the date

QuerySpec qs = new QuerySpec(PromotionNotice.class);
QueryResult qr = PersistenceHelper.manager.find(qs);

while (qr.hasMoreElements()) {
    PromotionNotice pn = (PromotionNotice) qr.nextElement();
    // Do something with the PromotionNotice
}
jbailey
17-Peridot
(To:VladiSlav)

Could you provide a little more detail on what you are trying to report on?

avillanueva
22-Sapphire II
(To:VladiSlav)

Any reason you need it in Java? This seems easier to do in Query Builder. From there you can download straight to Excel, Java or some other application with a web call.

 

I need to retrieve all promotion requests that have been approved this month. Query Builder doesn't work for me because I also need to retrieve all components, documents, and drawings from each request. For each document and drawing, I need to retrieve the page format and the number of pages, which can only be done in Java. Can I use Java in Query Builder to count pages?

avillanueva
22-Sapphire II
(To:VladiSlav)

I know that the first hurdle will be creating a report that includes different object types. In the past, I have created 3 queries to get Parts, Docs and CAD Docs then joined them together with a union (documented on the community site). Not sure about page format but in our case, we looked for the associated Creo format linked to the drawing and pulled from that. I am not sure how you would get the page count but you are right that it might be Java? Are you reading the actually binary content? That might be very slow. Suggest combining efforts. Use a query builder to quickly get the data then pipe into your functions to retrieve the rest of the data.

In the end, a solution was found.

To complete the task, you need to make the date dynamic and add a condition to retrieve only the promotion requests that are in the "Approved" state.

import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.maturity.PromotionNotice;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;

import java.sql.Date;
import java.sql.Timestamp;

public class Test {
    public static void main(String[] args) throws WTException {
        int count = 0;
        QuerySpec querySpec = new QuerySpec(PromotionNotice.class);

        // Condition for createStamp
        String createStampAttrName = "thePersistInfo.createStamp";
        String dateFromStr = "2023-03-01";
        String dateToStr = "2023-04-01";
        Timestamp dateFrom = new Timestamp(Date.valueOf(dateFromStr).getTime());
        Timestamp dateTo = new Timestamp(Date.valueOf(dateToStr).getTime());

        SearchCondition createStampCondition = new SearchCondition(PromotionNotice.class, createStampAttrName, SearchCondition.GREATER_THAN_OR_EQUAL, dateFrom);
        querySpec.appendWhere(createStampCondition, new int[]{0});

        // Condition for state
        SearchCondition stateCondition = new SearchCondition(PromotionNotice.class, createStampAttrName, SearchCondition.LESS_THAN, dateTo);
        querySpec.appendAnd();
        querySpec.appendWhere(stateCondition, new int[]{0});

        QueryResult queryResult = PersistenceHelper.manager.find(querySpec);

        while (queryResult.hasMoreElements()) {
            PromotionNotice promotionNotice = (PromotionNotice) queryResult.nextElement();
            System.out.print(count++ + "\t" + promotionNotice.getNumber() + "\t" + promotionNotice.getCreateTimestamp() + "<br>");
        }
    }
}

That’s not going to work.

Your code is using the create Timestamp.

You don’t care when it was created.

When the Promotion Request was created has nothing to do with when it was approved.

 

d_graham, I agree with you. I didn't take this issue into account. Could you please tell me how to modify the code to use the date for approval?

    public static QueryResult getPromotionRequests(DateRange dateRange) throws WTException {
        String startDateStr = dateRange.getDateFromStr();
        String endDateStr = dateRange.getDateToStr();

        QuerySpec querySpec = new QuerySpec(PromotionNotice.class);

        String createStampAttrName = "thePersistInfo.modifyStamp";
        Timestamp startDate = new Timestamp(Date.valueOf(startDateStr).getTime());
        Timestamp endDate = new Timestamp(Date.valueOf(endDateStr).getTime());

        SearchCondition startDateCondition = new SearchCondition(PromotionNotice.class, createStampAttrName, SearchCondition.GREATER_THAN_OR_EQUAL, startDate);
        querySpec.appendWhere(startDateCondition, new int[]{0});

        SearchCondition endDateCondition = new SearchCondition(PromotionNotice.class, createStampAttrName, SearchCondition.LESS_THAN, endDate);
        querySpec.appendAnd();
        querySpec.appendWhere(endDateCondition, new int[]{0});

        SearchCondition approvedStateCondition = new SearchCondition(PromotionNotice.class, "state.state", SearchCondition.EQUAL, "APPROVED");
        querySpec.appendAnd();
        querySpec.appendWhere(approvedStateCondition, new int[]{0});

        return PersistenceHelper.manager.find(querySpec);
    }

@VladiSlav 

That code is still not what you want. It might sometimes return correct data because you have included the state in the search but that’s not guaranteed to be correct as the someone with modify access at state Approved (such as an Administrator) could modify it well after it was Approved.

Announcements

Top Tags