Saturday, 20 August 2016

How to check whether image is exist in the URL or not using java?

This is to tell most of the time when we show images from the different server may not be available due to (HTTP 404 or HTTP 500). To tackle this issue we can have some default images when image is not available



public static boolean isImageExist(String urlName) throws IOException {
    URL url =new URL(urlName);
    try {
        Image image= ImageIO.read(url.openStream());
        return image != null;
    }
    catch ( FileNotFoundException e) {
        return false;
    }
}

To call this method 


try {
    if (isImageExist(imageUrl)){
        return imageUrl;
    }
} catch (IOException e) {
// Here you can display some no image url 
    return defaultImage;
}


Thursday, 16 June 2016

How to download file dynamically using Wicket's DownloadLink with a file generated using Wicket 7?



Html:
<a wicket:id="downloadLink" >
   Download
</a>


Java class:


 IModel<File> fileModel = new AbstractReadOnlyModel<File>() {
                    @Override
                    public File getObject() {
                        return generatedFile();
                    }
                };


DownloadLink downloadLink= new DownloadLink("downloadLink", fileModel);

//This line used to delete once the download is completed
downloadLinkAddress.setDeleteAfterDownload(true);


The above step will make slow if you are showing many record at a time.



I would prefer below this way , ok let's start


In some scenario we might want to create a file and download dynamically when we click download 


IModel<File> fileModel =Model.of((File) null);

DownloadLink downloadLink = new DownloadLink("downloadlink",fileModel ){
    @Override    public void onClick() {
         File downloadFile = genaratedFile(); /**Call some utils to create a file
        IResourceStream resourceStream =
                new FileResourceStream(new org.apache.wicket.util.file.File(file));
/**Should pass full file path
        getRequestCycle().scheduleRequestHandlerAfterCurrent
                (new ResourceStreamRequestHandler(resourceStream).
                        setFileName(file.getName()));

}
};

Thursday, 27 August 2015

Add custom Copy right in Intellij Idea IDE?

1.Open Intellij idea an type ALT+INSERT  key select copy right and below window will show;

copyright

2.Click OK .On open the window  select copyright Profiles. After selecting the the copy right Click Add Icon on the right side.























3.Name your client file and click ok.



4.Click copyright and add the newly created file  click apply  to finish the setting.








Thursday, 20 August 2015

Remove 'Choose one ' option from Wicket DropDownChoice?

Wicket Framework DropDownChoice component has default 'Choose one' value by default. To remove that add a override method following:

List<String>employeeNameList=new ArrayList<String>();
employeeNameList.add("Soora");
employeeNameList.add("padman");
employeeNameList.add("kiran");

 DropDownChoice employeeNameList = new DropDownChoice("employees",employeeNameList) {

  @Override
            protected CharSequence getDefaultChoice(String selectedValue) {
// put return type as empty so that default value will be removed
                return " ";
            }

}

Friday, 24 July 2015

Setting JAVA environment variable in local machine?

This tutorial to explain how to set the java home (JAVA_HOME) in local machine;

1.RightClick Mycomputer in that click Properties

2. After click properties will open new window in that click Advanced settings




















3.On Click on Advanced system settings it will open new window In system properties window click Environment Variables






4.In System Properties window click New  under system variables


5. Add Variable name and value in the text box and click ok.


6.finally in system variable double click path and add the path. 

7.Finally click apply and close the window.

8. By check open a command prompt and type java -version   it will should show the version.





Tuesday, 21 July 2015

Display error messages directly in Vaadin framework 7

In vaadin framework when a validation error occurred, it was not clear what the problem was. None of them thought about hovering their mouse over the error indicator (if they even noticed the indicator) to get the precise error message.I read in the Book of Vaadin that the placement of the error indicator is managed by the layout in which the component is contained. However, it doesn't seem to say anything about directly showing the error message.So i decided to do that.

For example here i am taking as simple example user object to validate

Model:

public class User extends BaseIsisEntity {

    private Integer id;
    private String territoryCode;
    private boolean disabled;
    private String title;
    private String firstName;
    private String middleName;
    private String lastName;
    private String homeTelephone;
    private String workTelephone;
    private String mobile1;
    private String mobile2;
    private String fax;
    private String emailAddress;
    private String website;
//Getter and setter

View:

public class EditUserView extends View{

private BeanFieldGroup<User> userBeanFieldGroup;

private Label errorLbl;



        VerticalLayout userFormLayout=new VerticalLayout();
        errorLbl = new Label("", ContentMode.HTML);
        userFormLayout.addComponent(errorLbl);
        TextField territoryCode = new TextField("Territory Code");
        territoryCode.setRequired(true);
        territoryCode.setRequiredError("Territory Code Required");
        userBeanFieldGroup.bind(territoryCode, "territoryCode");

        TextField firstnameField = new TextField("First Name");
        firstnameField.setRequired(true);
        firstnameField.setRequiredError("First Name Required");
        userBeanFieldGroup.bind(firstnameField, "firstName");
        userFormLayout.addComponent(firstnameField);

       TextField lastNameTxt = new TextField("Last Name");
        lastNameTxt.setRequired(true);
        lastNameTxt.setRequiredError("Last Name Required");
        userBeanFieldGroup.bind(lastNameTxt, "lastName");
        userFormLayout.addComponent(lastNameTxt);

       TextField middleNameTxt = new TextField("Middle Name");
        userBeanFieldGroup.bind(middleNameTxt, "middleName");
        userFormLayout.addComponent(middleNameTxt);

        TextField homeTelephoneText = new TextField("Home Telephone");
        homeTelephoneText.setRequired(true);
        homeTelephoneText.setRequiredError("Home Telephone is required");
        userBeanFieldGroup.bind(homeTelephoneText, "homeTelephone");
        userFormLayout.addComponent(homeTelephoneText);

       TextField workTelePhoneTxt = newTextField("Work Telephone");
        workTelePhoneTxt.setRequired(true);
        workTelePhoneTxt.setRequiredError("Work Telephone is required");
        userBeanFieldGroup.bind(workTelePhoneTxt, "workTelephone");
        userFormLayout.addComponent(workTelePhoneTxt);

        TextField mobile1Txt = new TextField("Mobile1");
        mobile1Txt.setRequired(true);
        mobile1Txt.setRequiredError("Mobile1 is required");
        userBeanFieldGroup.bind(mobile1Txt, "mobile1");
        userFormLayout.addComponent(mobile1Txt);

        TextField mobile2Txt = new TextField("Mobile2");
        mobile2Txt.setRequired(true);
        mobile2Txt.setRequiredError("Mobile2 is required");
        userBeanFieldGroup.bind(mobile2Txt, "mobile2");
        userFormLayout.addComponent(mobile2Txt);


        TextField emailAddressTxt = new TextField("Email Address");
        emailAddressTxt.setRequired(true);
        emailAddressTxt.setRequiredError("Email Address is required");
        userBeanFieldGroup.bind(emailAddressTxt, "emailAddress");
        userFormLayout.addComponent(emailAddressTxt);
         HorizontalLayout   buttonLayout=new HorizontalLayout();
        buttonLayout.addComponent(new Button("Save", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {

                try {
                    userBeanFieldGroup.setBuffered(true);
                    userBeanFieldGroup.commit();
                    if (editObj.getId() == null) {
                       userService.add(editObj);

                    }
                } catch (FieldGroup.CommitException e) {
//This line is very important
                                                   errorLbl.setValue(ErrorUtils.showComponentErrors(userBeanFieldGroup.getFields()));
                }

            }
        }));
}

Util class:

public class ErrorUtils {

public static String showComponentErrors(final AbstractComponent[] componentArray) {
        List<String> errorList = ErrorUtils.getComponentError(componentArray);
        String error = StringUtils.join(errorList, "\n");
        return error;
    }

    public static String showComponentErrors(
            final Collection<?> componentCollection) {
        AbstractComponent[] componentArray = componentCollection
                .toArray(new AbstractComponent[] {});

        return ErrorUtils.showComponentErrors(componentArray);
    }
}

Thanks guys,

If you have any issue please don't hesitate to contact me. I am always happy to help....