Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, January 3, 2014

Unable to reference local variable with JsonBuilder

I have the following code

    AddTicketCommand addTicketCommand = new AddTicketCommand(request.JSON)    JsonBuilder jsonBuilder = new JsonBuilder()    jsonBuilder {        ticket {            subject addTicketCommand.subject            requester {                name currentUser?.name                email currentUser?.emailAddress            }            comment {                body addTicketCommand.comment            }            custom_fields {                [                        {                            id 21857727                            value addTicketCommand.zenRequestType                        },                        {                            id 21854146                            value addTicketCommand.zenProductId                        }                ]            }        }    }

The addTicketCommand object is not null on line 2 yet is undefined within the JsonBuilder closure. Is it not possible to access local variables in groovy from within closure?

You should be able to access addTicketCommand inside the closure as below. Mark the use of “parenthesis” instead of “curly” braces.

AddTicketCommand addTicketCommand = new AddTicketCommand(request.JSON)JsonBuilder jsonBuilder = new JsonBuilder()    jsonBuilder {        ticket {            subject addTicketCommand.subject            requester {                name currentUser?.name                email currentUser?.emailAddress            }            comment {                body addTicketCommand.comment            }            custom_fields ([ //Note the use of parenthesis                {                    id 21857727                    value addTicketCommand.zenRequestType                },                {                    id 21854146                    value addTicketCommand.zenProductId                }            ]) ////Note the use of parenthesis        }    }

Tuesday, October 22, 2013

Download only email subject in JavaMail & imaps

I’m trying to download only subject of emails, because it should take less time (downloading ~10 emails with photos take approximately 10 min :/).

Code that I’m now using is:

    try {        Store store = session.getStore("imaps");        store.connect(...);        Folder folder = store.getFolder(folderName);        folder.open(Folder.READ_ONLY);        message = folder.getMessages();        for (Message m : message) {            System.out.println(m.getSubject());        }        folder.close(false);        store.close();    } catch (MessagingException e) {        ...    } catch (IOException e) {        ...    }

it seems to me that you should look into prefetching the messages with:

FetchProfile fp = new FetchProfile();fp.add(FetchProfile.Item.ENVELOPE);fp.add("Subject");folder.fetch(message, fp);

Friday, October 11, 2013

LinearLayout dynamically display depending on a given number

I want to create a method that can dynamically display LinearLayout in a LinearLayout moreover that is located in a specified area in my Android application, the number of this linear layout changes depending on a given number.

Also, in each LinearLayout I want to display a Button & below it a TextView.

Here is the method I have already created:

public void putLinearLayout(double number){       int mButtonHeight = 100;        int mButtonWidth = 80;         LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1);        for(int i=1;i<=number;i++)        {            LinearLayout L = new LinearLayout(this);            Button b= new Button(this);            TextView tv = new TextView(this);            L.setOrientation(LinearLayout.HORIZONTAL);            b.setWidth(mButtonWidth);            b.setHeight(mButtonHeight);            L.addView(b);            L.addView(tv);            Linear.addView(L);        }    }

you just need to set the layoutparams

public void putLinearLayout(double number){        LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1);               int mButtonHeight = 100;                int mButtonWidth = 80;                for(int i=1;i<=number;i++)                {                    LinearLayout L = new LinearLayout(this);                    L.setBackgroundColor(Color.WHITE);                    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //<--this line , is what you were missing                    L.setLayoutParams(params); //<-- & then this                    Button b= new Button(this);                    b.setText(i+"");                    TextView tv = new TextView(this);                    tv.setText("i am textview number: "+i);                    L.setOrientation(LinearLayout.HORIZONTAL);                    b.setWidth(mButtonWidth);                    b.setHeight(mButtonHeight);                    L.addView(b);                    L.addView(tv);                    Linear .addView(L);                }

}

Tuesday, October 1, 2013

Hibernate: How to get full sql query for session.save method?

I’m saving new object via Hibernate. What I would like to obtain is full sql query which was use. In debug Im getting query with “?” .

 ... values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

How to obtain same query with values.

K.

This is from my log4j configuration (Hibernate 3.6.8).

log4j.logger.org.hibernate.SQL = DEBUG, C

log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder = TRACE, C

log4j.appender.C = org.apache.log4j.ConsoleAppender

log4j.appender.C.layout = org.apache.log4j.PatternLayout

log4j.appender.C.layout.ConversionPattern = [%d{dd MMM yyyy HH:mm:ss,SSS}] %-5p %C.%M : %m%n

SQL has the logger for the SQL statement (you need to set it to DEBUG level) & BasicBinder the logger for the actual parameters (TRACE level)

Monday, September 30, 2013

JFrame doesn't add TabbedPane

My JFrame is not adding the JTabbedPane & I don’t know if the crash is some sort of bug of my eclipse. There are no syntaxes errors or anything that seems to be to me wrong. Could anyone else try to run it & see if it works? The code is already ready to run. Thanks in advance

public class MainScreen extends JFrame implements ActionListener {    JMenuBar bar;    JMenu file, register;    JMenuItem close, search;    ImageIcon logo= new ImageIcon("rsc/img/sh-logo.jpg");    ImageIcon worldIcon= new ImageIcon("rsc/img/world-icon.png");    JLabel lbImage1;    JTabbedPane tabbedPane = new JTabbedPane();    JPanel entrance = new JPanel();    public MainScreen()    {        JFrame mainFrame = new JFrame();        lbImage1= new JLabel(logo, JLabel.CENTER);        entrance.add(lbImage1);        tabbedPane.addTab("SHST", worldIcon, entrance);        mainFrame.add( tabbedPane, BorderLayout.CENTER);        bar= new JMenuBar();        file= new JMenu("File");        register= new JMenu("Search");        close= new JMenuItem("Close");        close.addActionListener(this);        search= new JMenuItem("Request Query");        search.addActionListener(this);        //Keyboard Shortcut        register.setMnemonic(KeyEvent.VK_S);        file.setMnemonic(KeyEvent.VK_F);        search.setMnemonic(KeyEvent.VK_R);        //mainFrame Setup        bar.add(file);        bar.add(register);        file.add(close);        register.add(search);        mainFrame.add(bar);        mainFrame.setExtendedState(getExtendedState() | mainFrame.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize());        mainFrame.setTitle("SHST");        mainFrame.setJMenuBar(bar);        mainFrame.setDefaultCloseOperation(0);        mainFrame.setVisible(true);            WindowListener J=new WindowAdapter(){            public void windowClosing(WindowEvent e){            System.exit(0);            }        };         addWindowListener(J);}public void actionPerformed(ActionEvent e){        if(e.getSource()==close){            System.exit(0);        }        }public static void main (String[] args){        MainScreen m= new MainScreen();    }}

You’ve added JMenuBar in Content pane. It is not required.

remove this line in your code mainFrame.add(bar); & mainFrame.setJMenuBar(bar); is already added.