Foren

Regd:How to create Expando columns for BlogEntry Table

Srikanth Reddy, geändert vor 13 Jahren.

Regd:How to create Expando columns for BlogEntry Table

Junior Member Beiträge: 53 Beitrittsdatum: 18.06.10 Neueste Beiträge
Hi i am new to liferay ,
So can any one here help me in understanding or creating an Expando column for BlogsEntry table .

I mean for can give any sample example here so .

I have seen some forums ,but i am unable to understand them .

It would be a great help for me

Thanks
Srikanth
thumbnail
Felix Ashirov, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Regular Member Beiträge: 128 Beitrittsdatum: 26.02.10 Neueste Beiträge
The simpliest way to use expando values for any model class is the getExpandoBridge method, which is specified in interface com.liferay.portal.model.BaseModel.

String expandoAttribute1Name = "attr1";
BlogsEntry entry = BlogsEntryLocalServiceUtil.getBlogsEntry(15187);
ExpandoBridge entryExpando = entry.getExpandoBridge();
if(!entryExpando.hasAttribute(expandoAttribute1Name)){
   entryExpando.addAttribute(expandoAttribute1Name);
}
entryExpando.setAttribute(expandoAttribute1Name,"Test");
System.out(entryExpando.getAttribute(expandoAttribute1Name));

However, this method worked unstable for me (Liferay 5.2.3) and
I had to implement my own util-methods for expando values manipulation using ExpandoTableLocalServiceUtil and ExpandoValueLocalServiceUtil:

    public static ExpandoTable getExpandoTable(String className) throws Exception {
        ExpandoTable et;
        if (versionAPI.equals(LiferayConstants.LIFERAY_API_52)) {
            Method mGetDefaultTable = ReflectionUtils.findMethod(ExpandoTableLocalServiceUtil.class, "getDefaultTable", new Class[]{String.class});
            Method mAddDefaultTable = ReflectionUtils.findMethod(ExpandoTableLocalServiceUtil.class, "addDefaultTable", new Class[]{String.class});
            try {
                et = (ExpandoTable) mGetDefaultTable.invoke(null, className);
            } catch (Exception e) {
                if (e instanceof NoSuchTableException) {
                    et = (ExpandoTable) mAddDefaultTable.invoke(null, className);
                } else {
                    throw e;
                }
            }
        } else if (versionAPI.equals(LiferayConstants.LIFERAY_API_60)) {
            Method mGetDefaultTable = ReflectionUtils.findMethod(ExpandoTableLocalServiceUtil.class, "getDefaultTable", new Class[]{long.class, String.class});
            Method mAddDefaultTable = ReflectionUtils.findMethod(ExpandoTableLocalServiceUtil.class, "addDefaultTable", new Class[]{long.class, String.class});
            try {
                et = (ExpandoTable) mGetDefaultTable.invoke(null, CompanyThreadLocal.getCompanyId(), className);
            } catch (Exception e) {
                if (e instanceof NoSuchTableException) {
                    et = (ExpandoTable) mAddDefaultTable.invoke(null, CompanyThreadLocal.getCompanyId(), className);
                } else {
                    throw e;
                }
            }
        } else {
            throw new ExceptionUnsupportedLiferayVersion();
        }
        return et;
    }

    public static ExpandoValue getExpandoValue(String className, String columnKey, long classPK) throws Exception {
        ExpandoTable expTable = getExpandoTable(className);
        ExpandoValue expValue;
        if (versionAPI.equals(LiferayConstants.LIFERAY_API_52)) {
            Method mGetValue = ReflectionUtils.findMethod(ExpandoValueLocalServiceUtil.class, "getValue", new Class[]{String.class, String.class, String.class, long.class});
            expValue = (ExpandoValue) mGetValue.invoke(null, className, expTable.getName(), columnKey, classPK);
        } else if (versionAPI.equals(LiferayConstants.LIFERAY_API_60)) {
            Method mGetValue = ReflectionUtils.findMethod(ExpandoValueLocalServiceUtil.class, "getValue", new Class[]{long.class, String.class, String.class, String.class, long.class});
            expValue = (ExpandoValue) mGetValue.invoke(null, CompanyThreadLocal.getCompanyId(), className, expTable.getName(), columnKey, classPK);
        } else {
            throw new ExceptionUnsupportedLiferayVersion();
        }
        return expValue;
    }

    public static ExpandoValue updateExpandoValue(String className, String columnKey, long classPK, String stringValue) throws Exception {
        ExpandoTable expTable = getExpandoTable(className);
        ExpandoValue expValue = getExpandoValue(className, columnKey, classPK);
        if (versionAPI.equals(LiferayConstants.LIFERAY_API_52)) {
            if (expValue != null) {
                ExpandoValueLocalServiceUtil.deleteExpandoValue(expValue);
            }
            expValue = (ExpandoValue) ReflectionUtils.findMethod(ExpandoValueLocalServiceUtil.class, "addValue", new Class[]{String.class, String.class, String.class, long.class, String.class}).
                    invoke(null, className, expTable.getName(), columnKey, classPK, stringValue);
        } else if (versionAPI.equals(LiferayConstants.LIFERAY_API_60)) {
            if(expValue != null){
                expValue.setString(stringValue);
                expValue = ExpandoValueLocalServiceUtil.updateExpandoValue(expValue);
            }else{
                expValue = (ExpandoValue) ReflectionUtils.findMethod(ExpandoValueLocalServiceUtil.class, "addValue", new Class[]{long.class,String.class, String.class, String.class, long.class, String.class}).
                    invoke(null, CompanyThreadLocal.getCompanyId(), className, expTable.getName(), columnKey, classPK, stringValue);
            }
        } else {
            throw new ExceptionUnsupportedLiferayVersion();
        }
        return expValue;
    }

These methods work both for Liferay 5.2.3 and Liferay 6.0 by org.springframework.util.ReflectionUtils. The usage sample:

String expandoAttribute1Name = "attr1";
BlogsEntry entry = BlogsEntryLocalServiceUtil.getBlogsEntry(15187);
updateExpandoValue(BlogsEntry.class.getName(),expandoAttribute1Name,entry.getEntryId,"Test");
System.out(getExpandoValue(BlogsEntry.class.getName(),expandoAttribute1Name,entry.getEntryId));

Hope this will help
Srikanth Reddy, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Junior Member Beiträge: 53 Beitrittsdatum: 18.06.10 Neueste Beiträge
Thanks Felix Ashirov,

I am working in Liferay 6.0.5 version ,so i think it should work ,let me try this.
Once i am done ,i ll update the status.Hope it works in liferay 6 .x.x.

Any way Thank you very much
regards
Srikanth
Oliver Bayer, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Liferay Master Beiträge: 894 Beitrittsdatum: 18.02.09 Neueste Beiträge
Hi,

you can also take a look at this wiki which describes the use of expando and delivers a good example.

HTH
Srikanth Reddy, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Junior Member Beiträge: 53 Beitrittsdatum: 18.06.10 Neueste Beiträge
Hi oliver Bayer,
Thanks for Your Reply.Actually i saw that wiki,but as i dont have much idea on VM files ,
so didnt follow it properly. But know i think i understand it well.
and One more thing ,is every variable in VM files declared starting with "$" sign.

Regards
Srikanth
thumbnail
Felix Ashirov, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Regular Member Beiträge: 128 Beitrittsdatum: 26.02.10 Neueste Beiträge
and One more thing ,is every variable in VM files declared starting with "$" sign.

Yes, that's right. VM file is Apache Velocity template engine file. But this files can be used only in layout templates and themes.

In my opinion it's harder to study Liferay's API, develop and debug your software in the Velocity template enviroment.
Oliver Bayer, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Liferay Master Beiträge: 894 Beitrittsdatum: 18.02.09 Neueste Beiträge
Hi Srikanth,

as Felix said each vm variable is starting with the $-sign. You can use the same methods from the vm wiki example in your java code so the transfer shouldn't be to hard. If you have any additional questions feel free to ask.

Greets Oli
Srikanth Reddy, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Junior Member Beiträge: 53 Beitrittsdatum: 18.06.10 Neueste Beiträge
Yes more more question ,
Is there any other way of creating expando columns for Blogs ,without using the UI.ie,
I wanted to crate 3 columns for blogs ,from an action class and also i dont want that code
to be executed only once ..so that those columns are created in database ,without any help
from the User

generally as we know,an expando column is retrieved or set the value as follows


ExpandoBridge expand = ExpandoBridgeFactoryUtil.getExpandoBridge(BlogsEntry.class.getName());
if(expand != null) {
try {
if(!expand.hasAttribute("comments")) {
expand.addAttribute("comments");
}
} catch(PortalException pe) {
LOGGER.error(pe);
}
blogsEntryObject.getExpandoBridge().setAttribute("comments", "new content");
}


But what i want is that ,when the blogs portlet starts loading ,i ll overWrite its action class and call my method to create these 3 columns ..Is there any method to create columns in DB in Expando table ,without the control panel Ui.


Thanks
Srikanth
thumbnail
Felix Ashirov, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Regular Member Beiträge: 128 Beitrittsdatum: 26.02.10 Neueste Beiträge
Srikanth Reddy:
Yes more more question ,
Is there any other way of creating expando columns for Blogs ,without using the UI.ie,
I wanted to crate 3 columns for blogs ,from an action class and also i dont want that code
to be executed only once ..so that those columns are created in database ,without any help
from the User

generally as we know,an expando column is retrieved or set the value as follows


ExpandoBridge expand = ExpandoBridgeFactoryUtil.getExpandoBridge(BlogsEntry.class.getName());
if(expand != null) {
try {
if(!expand.hasAttribute("comments")) {
expand.addAttribute("comments");
}
} catch(PortalException pe) {
LOGGER.error(pe);
}
blogsEntryObject.getExpandoBridge().setAttribute("comments", "new content");
}


But what i want is that ,when the blogs portlet starts loading ,i ll overWrite its action class and call my method to create these 3 columns ..Is there any method to create columns in DB in Expando table ,without the control panel Ui.


Thanks
Srikanth


Well actually your code should work. The ExpandoBridge's addAttribute method should add the column in expando table for class BlogsEntry.

However, as I mentioned earlier, in my practice ExpandoBridge works unstable, sometimes it does not add column or returns null instead of defined expando value. I've realized that ExpandoBridge depends on permissions of current user

So in your case i recomment to use ExpandoColumnLocalServiceUtil.addColumn(long companyId, long tableId, String className, int type). This sample code should work for Liferay 6.0:

long companyId = CompanyThreadLocal.getCompanyId();
int type = ExpandoColumnConstants.STRING;
String className = BlogsEntry.class.getName();
ExpandoTable expTable;
try{
  expTable = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, className);
}catch(NoSuchTableException e){
  expTable = ExpandoTableLocalServiceUtil.addDefaultTable(companyId, className);
}
ExpandoColumnLocalServiceUtil.addColumn(expTable.getTableId(), className, type);

Don't forget to add a check for existance of a column using ExpandoColumnLocalServiceUtil.getColumn method.

Good luck
Srikanth Reddy, geändert vor 13 Jahren.

RE: Regd:How to create Expando columns for BlogEntry Table

Junior Member Beiträge: 53 Beitrittsdatum: 18.06.10 Neueste Beiträge
Thank you so much Felix Ashirov,
It was so kind of you to not only provide code snippet ,but also explain .
Thanks and regards
Srikanth Reddy