Foren

Custom field default value programmatically.

Amit Shukla, geändert vor 11 Jahren.

Custom field default value programmatically.

Junior Member Beiträge: 93 Beitrittsdatum: 07.06.12 Neueste Beiträge
Hi all,

I want to create some custom field with default values (these values will come through database table) for document library .How can I do this programmatically.


Please tell me step so that i can do this task.

Thanks a lot
Amit
thumbnail
Jignesh Vachhani, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Liferay Master Beiträge: 803 Beitrittsdatum: 10.03.08 Neueste Beiträge
follow this steps :

1) Make TestData table dump in liferay DB
2) create service.xml with te column name you have in your table
and put service.xml in your cutome portlet location (test-portlet/docroot/WEB-INF)

<!--?xml version="1.0"?-->


<service-builder package-path="com.liferay.portlet.journal">
	<namespace>test</namespace>
	<entity name="TestData" uuid="true" local-service="true" remote-service="true">
		<!-- PK fields -->
		<column name="id" type="long" primary="true" />
		<column name="fname" type="String" />
		<column name="lname" type="String" />
	</entity>

</service-builder>


3) Now build the service using ant build-service command from portlet
4) Now it will generate some classes and you can check that TestDataLocalServiceImpl class has been generated or not .
5) Now define your method in that class to get data all data even you can get data by promary key and other parameter as well.

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.journal.service.impl;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portlet.journal.model.TestData;
import com.liferay.portlet.journal.service.base.TestDataLocalServiceBaseImpl;
import com.liferay.portlet.journal.service.persistence.TestDataUtil;

public class TestDataLocalServiceImpl extends TestDataLocalServiceBaseImpl {
	public java.util.List<testdata> getData() throws SystemException{
		java.util.List<testdata> dataList = TestDataUtil.findAll();
		return dataList;		
	}
}
</testdata></testdata>


Now in jsp you can write to get all data :

<%=TestDataLocalServiceUtil.getData()%>
Amit Shukla, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Junior Member Beiträge: 93 Beitrittsdatum: 07.06.12 Neueste Beiträge
Thanks Jignesh,

I followed the same step but getting the error.

An error occurred at line: 372 in the jsp file: /html/portlet/document_library/edit_file_entry.jsp

TestDataLocalServiceUtil cannot be resolved

Please look into this and suggest me what i have to do?

Thanks
Amit
thumbnail
Jignesh Vachhani, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Liferay Master Beiträge: 803 Beitrittsdatum: 10.03.08 Neueste Beiträge
Please import below line in jsp:

<%@page import="com.liferay.portlet.journal.service.TestDataLocalServiceUtil"%>
Amit Shukla, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Junior Member Beiträge: 93 Beitrittsdatum: 07.06.12 Neueste Beiträge
Hi Jignesh,
I have already imported the page
--------------------------------------com.liferay.test.cp.service.TestDataLocalServiceUtil ( i am using a hook for make changes in edit_file_entry.jsp)
now i am getting the error
Only a type can be imported. com.liferay.test.cp.service.TestDataLocalServiceUtil resolves to a package

Please u have any idea about it.

Thanks
Amit
thumbnail
Jignesh Vachhani, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Liferay Master Beiträge: 803 Beitrittsdatum: 10.03.08 Neueste Beiträge
provide your table entity here with all columns so that i can give you exact code here .
Here whatever code i putted is working in my local so you would be following something different way i think.
Amit Shukla, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Junior Member Beiträge: 93 Beitrittsdatum: 07.06.12 Neueste Beiträge
Hi Jignesh,
I have a table---- Siteinfo which have two coloumn(Siteid,sitedescription).
Siteid----sitedescription
123----->heritage site
345----->iucn information

I have requirement on uploading data each documents have a site id associated with it.

so we have made some changing in edit _file_entry.jsp for this.

Please help me .

Thanks
Amit
thumbnail
Jignesh Vachhani, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Liferay Master Beiträge: 803 Beitrittsdatum: 10.03.08 Neueste Beiträge
Amit,
IF your entity name is Siteino then you may have class name SiteInfoLocalServiceUtil instead of TestDataLocalServiceUti.
If your service builder has generated all classes proper then you can easily find with your eclipse IDE.
SO follow steps properly which i mentioned in my previous post .
thumbnail
Jay Patel, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Regular Member Beiträge: 118 Beitrittsdatum: 24.02.10 Neueste Beiträge
Amit Shukla:
Hi Jignesh,
I have already imported the page
--------------------------------------com.liferay.test.cp.service.TestDataLocalServiceUtil ( i am using a hook for make changes in edit_file_entry.jsp)
now i am getting the error
Only a type can be imported. com.liferay.test.cp.service.TestDataLocalServiceUtil resolves to a package

Please u have any idea about it.

Thanks
Amit


Hi Amit,

If you have created service.xml in some test-portlet under Plugin-SDK then your Util classes by default will not be accessible to your JSP in Hook as those are in different class loader.

If that is the path you want to follow, then you will have to copy your service jar generated by service-builder & paste in to the Global lib directory of your application server ([tomcat]/lib/ext) if you are using Tomcat , remove it from your portlet's lib directory & restart the server.

-Jay.
Amit Shukla, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Junior Member Beiträge: 93 Beitrittsdatum: 07.06.12 Neueste Beiträge
Hi Jignesh,

Can you provide consultancy for my project? plesae let me know.

Thanks
Amit
thumbnail
Pranay R Patadiya, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Regular Member Beiträge: 177 Beitrittsdatum: 23.02.10 Neueste Beiträge
Hi,
Expando is also a good solution for you problem.

More on expando : Expando Usage
thumbnail
Jignesh Vachhani, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Liferay Master Beiträge: 803 Beitrittsdatum: 10.03.08 Neueste Beiträge
Pranay, I think Amit has data already in table and he wants to reuse that table data if i am not wrong.
So in that case use of Expando could not be suitable solution as per my knowledge.
thumbnail
Nikhil Nishchal, geändert vor 11 Jahren.

RE: Custom field default value programmatically.

Regular Member Beiträge: 176 Beitrittsdatum: 22.06.12 Neueste Beiträge
You can try as (for liferay 6.1) :
FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(userId, repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog, bytes, serviceContext);
fileEntry.getExpandoBridge().setAttributeDefault("custon-aatibute", defaultValue);


defaultValue can be given by DB.