RE: How to "ADDING, UPDATING, AND DELETING ASSETS" in 7.1?

thumbnail
Mirto Silvio Busico, modified 7 Years ago. Regular Member Posts: 240 Join Date: 1/18/12 Recent Posts

Hi all,

I'm trying to create an MVC potlet to manage a Foo entity integrated with the asset framework.

I'm stuck with the 7.1 documentation at: 

https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-1/adding-updating-and-deleting-assets

Trying to write the delete code I see that in the FooLocalServiceBaseImpl.java I have

    @Indexable(type = IndexableType.DELETE)
    @Override
    public Foo deleteFoo(Foo foo) {
        return fooPersistence.remove(foo);
    }

Copied this code in FooLocalServiceImpl.java everything works as expected.

Then I tried to add the code to delete the asset entry data taken from the documentation:

assetEntryLocalService.deleteEntry(
    ENTITY.class.getName(), assetEntry.getEntityId());

Indexer<ENTITY> indexer = IndexerRegistryUtil.nullSafeGetIndexer(ENTITY.class);
indexer.delete(assetEntry);

Changing ENTITY with Foo my code is now:

    @Indexable(type = IndexableType.DELETE)
    @Override
    public Foo deleteFoo(Foo foo) {
        fooPersistence.remove(foo);

        assetEntryLocalService.deleteEntry(Foo.class.getName(), assetEntry.getEntryId());
        Indexer<Foo> indexer = IndexerRegistryUtil.nullSafeGetIndexer(Foo.class);
        indexer.delete(assetEntry);    
        return foo;
    }

This obviously gives the error that assetEntry is not defined. So I tried to add it and the code is now:

    @Indexable(type = IndexableType.DELETE)
    @Override
    public Foo deleteFoo(Foo foo) {
        fooPersistence.remove(foo);
        
        AssetEntry assetEntry = assetEntryLocalService.fetchAssetEntry(foo.getPrimaryKey());
        
        assetEntryLocalService.deleteEntry(Foo.class.getName(), assetEntry.getEntryId());
        Indexer<Foo> indexer = IndexerRegistryUtil.nullSafeGetIndexer(Foo.class);
        indexer.delete(assetEntry);    
        return foo;
    }

But this on the indexer.delete(assetEntry); line gives the error:

error: incompatible types: AssetEntry cannot be converted to Foo

What is the correct code to delete an assetEntry?

 

thumbnail
Jorge Díaz, modified 7 Years ago. Liferay Master Posts: 753 Join Date: 1/9/14 Recent Posts

Hi Mirto,

 

About example of https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-1/adding-updating-and-deleting-assets page, the object "assetEntry" is the object you are deleting of your custom entry.

It is confusing because it seems it is referring to AssetEntry object, but it is referring to "foo"

 

Perhaps it is more easy to understand the code if  you replace "assetEntry" variable with "foo" variable:

assetEntryLocalService.deleteEntry(
    ENTITY.class.getName(), foo.getEntityId());

Indexer&lt;ENTITY&gt; indexer = IndexerRegistryUtil.nullSafeGetIndexer(ENTITY.class);
indexer.delete(foo);

Note: foo.getEntityId()) == similar behavior than ==> foo.getPrimaryKey())

 

thumbnail
Mirto Silvio Busico, modified 7 Years ago. Regular Member Posts: 240 Join Date: 1/18/12 Recent Posts

Well, I'm not sure to do it correctly.

Now this code in FooLocalServiceImpl.java

    @Indexable(type = IndexableType.DELETE)
    @Override
    public Foo deleteFoo(long fooId, ServiceContext serviceContext) throws PortalException {
        Foo foo = fooPersistence.remove(fooId);
        
        assetEntryLocalService.deleteEntry(
            Foo.class.getName(), foo.getFooId());

        Indexer<Foo> indexer = IndexerRegistryUtil.nullSafeGetIndexer(Foo.class);
        indexer.delete(foo);

        return foo;
    }

doesn't generate a compile error.

Sadly I cannot test it because the call in MsbOnePortlet.java

    protected void deleteFoo(ActionRequest actionRequest) throws Exception {
        long fooId = ParamUtil.getLong(actionRequest, "fooId");

        ServiceContext serviceContext = ServiceContextFactory.getInstance(Foo.class.getName(), actionRequest);

        getFooLocalService().deleteFoo(fooId, serviceContext);
    }

to deleteFoo generates a runtime error 

Servlet execution threw an exception

before calling the foo local service.

I'm still debugging/investigating.