Filter assetset by id Webcenter Sites - webcenter

I need filter a assetset by id, my asset type is a flex asset. I never do this by id before and I'm stucked with this.
This is my code:
<publication:load name="pub" field="name" value='<%= ics.GetVar("site") %>'/>
<publication:get name="pub" field="id" output="siteId"/>
<%-- Content:getasset data is a custom tag to obtain attributes form a concrete asset --%>
<content:getassetdata prefix="lov" id='<%= ics.GetVar("assetid") %>' type="LOV" attributes="LOVUser"/>
<listobject:create name="powerObject" columns="power"/>
<c:forEach items="${lov.LOVUser}" var="usu">
<listobject:addrow name="powerObject"><listobject:argument name="power" value="${usu.id}"/></listobject:addrow>
</c:forEach>
<listobject:tolist name="powerObject" listvarname="powerList"/>
<%-- A this time, I have a powerList with all the IDs that I need to get --%>
<searchstate:create name="ssLovElements"/>
<searchstate:addstandardconstraint name="ssLovElements" attribute="id" list="powerList" />
<assetset:setsearchedassets name="asElementLovs" constraint="ssLovElements" assettypes="elementLOV" site='<%= ics.GetVar("siteId") %>' fixedlist="false"/>
<%-- Correct number --%>
<br/> N.Filas powerList: <ics:listget listname="powerList" fieldname="#numRows"/>
<%-- Incorrect number (Because constraint haven't effect --%>
N.Filas aslist: <ics:listget listname="aslist" fieldname="#numRows"/>
First list have 60 elements, second list have 600. I need use this constraint to extrac only wanted elements and get their attributes with multiplevalues, but I dont get working this. Any help? Thanks

I think you should use the Asset API instead of jstl tag. See the doc :
Session ses = SessionFactory.getSession();
AssetDataManager mgr = (AssetDataManager) ses.getManager(AssetDataManager.class.getName());
Condition c = ConditionFactory.createCondition( "id", OpTypeEnum.GREATER_THAN, long);
Query query = new SimpleQuery( "elementLOV", null, c, Arrays.asList( "name", ) );
query.getProperties().setIsBasicSearch( true );

Related

ASP.NET Core MVC Group by query with LINQ

I am trying to query the country column from one of my table and trying to group them so that one country name appears only once.
The controller code is
ViewBag.Countries = (from pT in _context.InfoProducts
group pT by new { pT.Country } into g
select new
{
Country = g.Key.Country
}).ToList();
In my view I am trying to show this list of countries in a dropdown like this
<div class=" form-group">
#Html.Label("Country")
<div class="col-md-12 ">
#Html.DropDownList("Country", new SelectList(ViewBag.Countries, "Country"), "Select Country", new { #class = "col-form-label col-md-12 label-align" })
</div>
</div>
The problem I am facing is though it is returning the expected names of the countries, the result is generating as a key, value pair something like
{Country = USA}
{Country = Canada}
The HTML generated copied from inspect element is given below
<select class="col-form-label col-md-12 label-align" id="Country" name="Country">
<option value="">Select Country</option>
<option>{ Country = USA}</option>
<option>{ Country = Canada}</option>
</select>
How can I get only the country name in the dropdown instead of the result I am currently getting.
Rather than using group by, it sounds like distinct would be a better fit:
_context.InfoProducts.Select(x => x.Country).Distinct().ToList();
This will give you back a list of strings to use, which won't give you the key-value pair problem you're currently having.
One more thing, when you use new SelectList(ViewBag.Countries, "Country") this overload of SelectList's constructor will try to set the value of Country as the selected item, but this doesn't exist in your list, so Select Country will always appear as selected. If you want that to select an actual country, then it would have to look something like:
new SelectList(ViewBag.Countries, "Canada")
where you would fetch Canada from the data for the current product.

Best approach to display query with join in a search container

I have a search page for books. I can search by title or by category (in my model a book is linked to a unique category). When I display the results I would like to display the category name also. Here is what I did:
<liferay-ui:search-container-results>
<%
searchContainer.setTotal(BookLocalServiceUtil.searchBooksCount(title, categoryId));
searchContainer.setResults(BookLocalServiceUtil.searchBooks(title, categoryId));
%>
</liferay-ui:search-container-results>
<liferay-ui:search-container-row
className="example.Book"
keyProperty="bookId"
modelVar="book" escapedModel="<%= true %>"
>
<portlet:renderURL var="editURL">
<portlet:param name="mvcPath" value="/html/book/edit_book.jsp" />
<portlet:param name="bookId" value="<%= String.valueOf(book.getBookId()) %>" />
</portlet:renderURL>
<liferay-ui:search-container-column-text
name="Book"
value="<%= String.valueOf(book.getTitle()) %>"
href="<%= editURL %>"
/>
<liferay-ui:search-container-column-text
name="Category"
value="<%= CategoryLocalServiceUtil.getCategory(book.getCategoryId()).getName() %>"
/>
</liferay-ui:search-container-row>
This is not optimized at all. The best way to do that would be to write a custom SQL with a join but I can't because I have optional parameters in my query (book title and category).
Any idea?
EDIT here is my current query:
public List<book> searchBooks(String title, Long categoryId)
throws SystemException {
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Book.class);
if (Validator.isNotNull(title)) {
dynamicQuery.add(PropertyFactoryUtil.forName("title").eq(title));
}
if (Validator.isNotNull(categoryId)) {
dynamicQuery.add(PropertyFactoryUtil.forName("categoryId").eq(categoryId));
}
List<Book> lst = BookLocalServiceUtil.dynamicQuery(dynamicQuery);
return lst;
}
The easiest way to handle optional parameters is to treat the null as a non filtering value.
As you didn't say which RDBMS are using I'm showing a sample of a predicate with T-SQL syntax:
WHERE (#Title = Book.Title -- filtering
OR #Title IS NULL) -- not filtering f the parameter is null
AND ( -- chain expressions like the expression above
Please, note that this is not optimum. If you have performance problems it has to do with the way severs tretat this query: they usually are not smart enough to realize that, if the parameter is null, they don't need to evaluate the first part of the OR expression

Using text input to search Access number column

I have a simple search page with this text input form
<form name="assetInput" action="assetResult.jsp" method="get">
<input type="text" name="assetNo" />
<input type="submit" value="Submit" />
</form>
which takes you to a results page that will search an Access database by the Asset Number you type in the search page. The column's data type is number.
This is the code to get the input from the text box, and my attempt to convert the String to an int.
<%
String assetNumber = request.getParameter("assetNo");
int assetNum = Integer.parseInt(assetNumber);
%>
My query:
rs = stmt.executeQuery( "SELECT * FROM [Inventory Tracking] WHERE (([Inventory Tracking].AssetNumber) = '"+assetNum+"')");
I'm able to search columns from the database whose data type is text, but I keep getting errors when trying to search a number column with a String.
Thanks in advance!
Numbers do not take a delimiter, only text fields.
rs = stmt.executeQuery( "SELECT * FROM [Inventory Tracking] WHERE [Inventory Tracking].AssetNumber = "+assetNum);
You could avoid a lot of problems with a parameter.

Oracle SQL Error: How to Debug this issue

Using:
select name, id
from "TEST"
where id :2
AND name :1
order by id desc
I am getting ORA: 00904 "TEST"."NAME": invalid identifier error but the wierd part is that I have checked my test table and it does not have NAME field but it has name field, I have also checked all the references which is made from that table and all other constraints but still it gives me same error. I do not know why, is there a way where I can check all the columns of the database for NAME column name or any other debuggind approach recommended would be highly welcomed.
DESC TEST;
Name Null Type
id NOT NULL NUMBER(11)
name VARCHAR2(29)
2 rows selected
EDIT
DESC TEST
Name Null Type
------------------------------ -------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ID NOT NULL NUMBER(11)
NAME VARCHAR2(29)
2 rows selected
Java Code
public String searchExecute(HttpServletRequest req, javax.servlet.ServletContext ctx, String nextPage){
// ESCA-JAVA0266:
System.out.println("This is Awesome");
// ESCA-JAVA0266:
System.out.println("id:"+req.getParameter("s_1985"));
// ESCA-JAVA0266:
System.out.println("name:"+req.getParameter("s_1984"));
boolean bDisplayAll = StringUtils.stringToBoolean(req.getParameter("display_all"));
if(bDisplayAll)
// ESCA-JAVA0034:
req.setAttribute("c_display_all", "Y");
req.setAttribute("c_search_submitted", "Y");
return nextPage;
}
JSP Code
<%# page import="att.leadx.dbutils.AppUtils" %>
<%# page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-
1">
</head>
<link rel=stylesheet type=text/css href="leadx.css">
<body bgcolor="#FFFFFF"><BR>
<form NAME="forma" ACTION="dispatcher" METHOD="post">
<INPUT TYPE="hidden" name="action" value="">
<input type=hidden name="c_master" value="TEST_SEARCH">
<%
request.setAttribute("c_form", "TEST_SEARCH");
request.setAttribute("c_top_title","PROFILE > TEST_SEARCH");
request.setAttribute("c_top_link_image","images/setup_sm.gif");
request.setAttribute("c_top_link_action","user.search.setup");
request.setAttribute("c_top_link_app_func","search_setup");
request.setAttribute("c_top_link_alt_display","Setup search criteria");
%>
<jsp:include page="j_custom_search.jsp" flush="true" />
<table width="94%" border="0" cellspacing="1" cellpadding="3" align=center>
<tr>
<td colspan=4 align=center>
<INPUT TYPE="submit" value="Search" class=prismsbutton onclick="document.forma.action.value ='cep.project.search.execute'">
</td>
<td colspan=4 align=center>
<INPUT TYPE="button" value="Cancel" class=prismsbutton onclick="window.history.back()">
</td>
</tr>
</table>
</form>
<form NAME="formd" ACTION="dispatcher" METHOD="post">
<input type="hidden" name="c_jsp" value="j_test_dynamic_search.jsp">
<%
request.setAttribute("c_master", "TEST_SEARCH");
request.setAttribute("c_html_form","formd");
request.setAttribute("c_list_id","1984");
request.setAttribute("c_search_form","TEST_SEARCH");
Object args[] = att.utils.DataStore.arg(att.leadx.dbutils.AppUtils.getLoggedInUser(request), att.utils.DataStore.TYPE_NUMERIC);
request.setAttribute("c_args", args);
if (att.utils.StringUtils.stringToBoolean((String)request.getAttribute("c_search_submitted"))){
%>
<jsp:include page="j_master_detail_dlist.jsp" flush="true" />
<%
}
%>
</form>
<%!
private void o(String s){
if (att.leadx.dbutils.AppUtils.inDebug())
System.out.println("[J_USER_SEARCH.JSP] " + s);
}
%>
SQL Statement
select name, id from TEST where name = :1 AND id = :2 order by id desc
Error Message
Aug 16, 2010 5:22:21 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.sql.SQLException: ORA-00936: missing expression
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:180)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:543)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1451)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:651)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2110)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2324)
at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:627)
at att.utils.DataStore.retrieve(DataStore.java:724)
at org.apache.jsp.j_005fmaster_005fdetail_005fdlist_jsp._jspService(j_005fmaster_005fdetail_005fdlist_jsp.java:566)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:659)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:565)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:493)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:965)
at org.apache.jsp.j_005ftest_005fdynamic_005fsearch_jsp._jspService(j_005ftest_005fdynamic_005fsearch_jsp.java:102)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:659)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:457)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:395)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311)
at att.leadx.nav.Dispatcher.service(Dispatcher.java:113)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)
Thanks
SELECT *
FROM ALL_TAB_COLS
WHERE UPPER(COLUMN_NAME) = 'NAME';
will show you all columns called NAME
EDIT:
Based on your comment, aren't you missing the operators in your WHERE clause? ie =
select name, id
from "TEST"
where id :2 -- Surely you mean: id = :2
AND name :1 -- Surely you mean: name = :2
order by id desc
EDIT 2:
Based on the SQL*Plus output, it looks like you've created the table with lower-case column names. Whilst this is possible and valid it's usually just hard work. I'd recreate the columns with upper case names. (as Alex said)
EDIT 3:
I think...
SELECT "id", "name"
FROM TEST
WHERE "id" = :1
AND "name" = :2
ORDER BY "id" desc;
should work
It's a good idea not to surround column and table names with double-quotation marks unless you are know what you are doing and confident you need that.
In Oracle, database object names (including table and columns) are not case-sensitive and assumed upper-case unless you include them into double-quotes.
E.g, if a table created as TEST, it can be referred to as test, Test or TesT or "TEST".
But if it is created as "Test", you will only be able to refer to it as "Test" (in double quotation marks). All the other ways will result in "invalid identifier" error.

Selecting items IN a row

I'm developing a website for Tenants to find properties. When they sign up, they can choose the property types that they are interested, for example: Apartment or House.
When a Tenant logs into their account, they can then do a search for properties. The search form is prepopulated with the values that they originally entered on sign up, for example: City, Postcode and so on.
The form also needs to display some checkboxes with the relevant boxes ticked for the Property Types that they selected on sign up. I'm having some problems getting this to work and wondered if there is anyone who could correct the code for me?
I believe I need to use an 'IN' statement so that the relevant checkboxes would be ticked, if the IDs for those properties are found in the CustomerReqPropertyType column. The CustomerReqPropertyType column is varchar(50) and as an example, if a user has selected Apartment and House, it is store in the row as 2, 4 (as there is a separate table with the Property Types.
This is the code I have on the page;
<%
While (NOT rspropertytype.EOF)
%>
<li>
<input type="checkbox" name="txtPropertyType" id="txtPropertyType" value="<%=(rspropertytype.Fields.Item("PropertyTypeID").Value)%>"<% If Not rstenantrequirements.EOF Or Not rstenantrequirements.BOF Then %><%If (Not isNull((rstenantrequirements.Fields.Item("CustomerReqPropertyType").Value))) Then If (CStr(rspropertytype.Fields.Item("PropertyTypeID").Value) = CStr((rstenantrequirements.Fields.Item("CustomerReqPropertyType").Value))) Then Response.Write("")%><% End If ' end Not rstenantrequirements.EOF Or NOT rstenantrequirements.BOF %> />
<label for="txtPropertyType"><%=(rspropertytype.Fields.Item("PropertyTypeTitle").Value)%></label>
</li>
<%
rspropertytype.MoveNext()
Wend
If (rspropertytype.CursorType > 0) Then
rspropertytype.MoveFirst
Else
rspropertytype.Requery
End If
%>
I would be very grateful for any help.
In order for a checkbox to be checked the checked property must equal "checked".
e.g.
<input type="checkbox" name="something" value="somethingElse" checked="checked" />
I suspect that in your code the rspropertytype and rstenantrequirements recordsets could be consolidated into one recordset generated from one SQL statement.
e.g.
SELECT pt.*
, CASE
WHEN ISNULL(tr.CustomerReqPropertyType,0) = 0 THEN 0
ELSE 1 END AS [checked]
FROM propertytype AS [pt]
LEFT JOIN tenantrequirements AS [tr]
ON pt.PropertyTypeID = tr.CustomerReqPropertyType
WHERE ...
Then your ASP code could be simplified as well.
e.g.
<%
While (NOT rs.EOF)
Dim pID : pID = rs("PropertyTypeID")
Dim pTitle : pTitle = rs("PropertyTypeTitle")
Dim checked : checked = "" : If (rs("checked") = 1) Then checked = "checked"
%>
<li>
<input type="checkbox" name="txtPropertyType" id="txtPropertyType<%=pID%>" value="<%=pID%>" checked="<%=checked%>" />
<label for="txtPropertyType<%=pID%>"><%=pTitle%></label>
</li>
<%
rs.MoveNext()
Wend
%>
This is really hard to follow. First I'd break up that line to where your while is you can just set a variable and print that to page. I assume you're trying to set the checkbox checked. What I would do is make sure the value returned isn't null assuming this is a query that just returns the property type and you left joined it with the table that has the descriptions in it when they are set for the property in question. I don't see you ever print checked to the checkbox so it's never going to be checked anyway.