/*
 * Copyright 2003 - 2018 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.efaps.esjp.pos.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.Response;

import org.efaps.admin.datamodel.Status;
import org.efaps.admin.program.esjp.EFapsApplication;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.db.MultiPrintQuery;
import org.efaps.db.QueryBuilder;
import org.efaps.db.store.Resource;
import org.efaps.db.store.Store;
import org.efaps.esjp.ci.CIPOS;
import org.efaps.esjp.pos.util.Pos;
import org.efaps.pos.dto.CategoryDto;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO comment!
 *
 * @author The eFaps Team
 */
@EFapsUUID("f1c816e2-1543-4975-b69a-799b4809802b")
@EFapsApplication("eFapsApp-POS")
public abstract class Category_Base
    extends AbstractRest
{
    /** The Constant LOG. */
    private static final Logger LOG = LoggerFactory.getLogger(Category.class);

    /**
     * Gets the categories.
     *
     * @return the categories
     * @throws EFapsException the eFaps exception
     */
    public Response getCategories(final String _identifier)
        throws EFapsException
    {
        checkAccess(_identifier);
        LOG.debug("Responding to request for Categories for {}", _identifier);
        final List<CategoryDto> categories = new ArrayList<>();
        final QueryBuilder queryBldr = new QueryBuilder(CIPOS.Category);
        queryBldr.addWhereAttrEqValue(CIPOS.Category.Status, Status.find(CIPOS.CategoryStatus.Active));
        final MultiPrintQuery multi = queryBldr.getPrint();
        multi.addAttribute(CIPOS.Category.Name, CIPOS.Category.Weight);
        multi.execute();
        while (multi.next()) {
            String imageOid = null;
            if (Pos.CATEGORY_ACIVATEIMAGE.get()) {
                final Resource resource = Store.get(multi.getCurrentInstance().getType().getStoreId())
                                .getResource(multi.getCurrentInstance());
                if (resource.exists()) {
                    imageOid = multi.getCurrentInstance().getOid();
                }
            }

            categories.add(CategoryDto.builder()
                .withOID(multi.getCurrentInstance().getOid())
                .withName(multi.getAttribute(CIPOS.Category.Name))
                .withWeight(multi.getAttribute(CIPOS.Category.Weight))
                .withImageOid(imageOid)
                .build());
        }
        final Response ret = Response.ok()
                        .entity(categories)
                        .build();
        return ret;
    }

}

@@ -865,16 +865,46 @@
 psUUID;%0A
+import org.efaps.db.Instance;%0A
 import o
@@ -962,24 +962,59 @@
 eryBuilder;%0A
+import org.efaps.db.SelectBuilder;%0A
 import org.e
@@ -1102,16 +1102,56 @@
 .CIPOS;%0A
+import org.efaps.esjp.db.InstanceUtils;%0A
 import o
@@ -2260,24 +2260,164 @@
 getPrint();%0A
+        final var selParentInst = SelectBuilder.get().linkto(CIPOS.Category.ParentLink).instance();%0A        multi.addSelect(selParentInst);%0A
         mult
@@ -2937,16 +2937,240 @@
       %7D%0A
+            final Instance parentInst = multi.getSelect(selParentInst);%0A            String parentOid = null;%0A            if (InstanceUtils.isValid(parentInst)) %7B%0A                parentOid = parentInst.getOid();%0A            %7D
 %0A       
@@ -3451,16 +3451,58 @@
 ageOid)%0A
+                .withParentOid(parentOid)%0A
         

Update Category endpoint